Home > OS >  Access Map State ( item ) in Step functions
Access Map State ( item ) in Step functions

Time:12-08

I am trying to access item properties which I am iterating over using Map state. I keep getting this error:

Value ($['Map'].snapshot_id.S) for parameter snapshotId is invalid. Expected: 'snap-...'. (Service: Ec2, Status Code: 400, Request ID: 6fc02935-c161-49df-8606-bc6f3e2934a6)

I have gone through the docs, which seems to suggest access using $.Map.snapshot_id.S but doesn't seem to work. Meanwhile, an input item to Map is:

{
  "snapshot_id": {
    "S": "snap-01dd5ee46df84119e"
  },
  "file_type": {
    "S": "bash"
  },
  "id": {
    "S": "64e6893261d94669b7a8ca425233d68b"
  },
  "script_s3_link": {
    "S": "df -h"
  }
}

Map state definition:

    "Map": {
          "Type": "Map",
          "ItemProcessor": {
            "ProcessorConfig": {
              "Mode": "INLINE"
            },
            "StartAt": "Parallel State",
            "States": {
              "Parallel State": {
                "Comment": "A Parallel state can be used to create parallel branches of execution in your state machine.",
                "Type": "Parallel",
                "Branches": [
                  {
                    "StartAt": "CreateVolume",
                    "States": {
                      "CreateVolume": {
                        "Type": "Task",
                        "Parameters": {
                          "AvailabilityZone": "us-east-2b",
                          "SnapshotId": "$$.Map.snapshot_id.S"
                        },
                        "Resource": "arn:aws:states:::aws-sdk:ec2:createVolume",
                        "Next": "AttachVolume"
                      },
                      "AttachVolume": {
                        "Type": "Task",
                        "Parameters": {
                          "Device": "MyData",
                          "InstanceId": "MyData",
                          "VolumeId": "MyData"
                        },
                        "Resource": "arn:aws:states:::aws-sdk:ec2:attachVolume",
                        "End": true
                      }
                    }
                  }
                ],
                "End": true
              }
            }
          },
          "Next": "Final Result",
          "ItemsPath": "$.Items",
          "MaxConcurrency": 40
        },

CodePudding user response:

TL;DR "SnapshotId.$": "$.snapshot_id"

By default, each inline map iteration's input is one item from the ItemsPath array, accessible simply as $.

Your state machine definition implies that $.Items is an array of objects with a snapshot_id key (and other keys). If so, each iteration's snapshot id is at $.snapshot_id.

Finally, adding .$ to the parameter name (SnapshotId.$) tells Step Functions the value is not a literal, but rather a path value to be substituted.

  • Related