Search code examples
amazon-web-servicesaws-cdkaws-step-functions

AWS Step functions: DistrbituedMap itemReader get bucket and key at runtime from state input in CDK


I'm wondering if is there a way to get the bucket key from the input via CDK?

In the console, we can define:

"ItemReader": {
        "Resource": "arn:aws:states:::s3:getObject",
        "ReaderConfig": {
          "InputType": "JSON"
        },
        "Parameters": {
          "Bucket": "my-bucket",
          "Key.$": "$.key"
        }
      }

But this doesn't seem to be working in CDK:

const map = new sfn.DistributedMap(this, 'Parse the document', {
      itemReader: new sfn.S3JsonItemReader({
        bucket: myBucket,
        key: '$.key',
      }),
    })

Solution

  • Use the CDK's JsonPath.stringAt helper method to use the $.key value from the state machine input:

    key: sfn.JsonPath.stringAt("$.key"),
    

    That will synth the expected "Key.$": "$.key" SDL. Your code outputs "Key": "$.key", which will cause step functions to treat $.key as a literal string.