Search code examples
amazon-web-servicesaws-cdkaws-step-functionsaws-event-bridge

Eventbridge pipe: Dynamodb stream to step function. Array input


I'm using an EventBridge pipe to pipe a DynamoDB stream to a step function. Although the batch size is set to 1, the input of my step function is always an array:

[
  {
    "eventID": "9b841ccbd84747819eaf12436528d3f0",
    "eventName": "MODIFY",
    "eventVersion": "1.1",
    "eventSource": "aws:dynamodb",
    "awsRegion": "eu-central-1",
    "dynamodb": {
      "ApproximateCreationDateTime": 1689860028,
      "Keys": {
        "sequenceId": {
          "S": "order"
        }
      },
      "NewImage": {
        "prefix": {
          "S": "ORD-"
        },
        "lastSequenceValue": {
          "N": "100001"
        },
        "sequenceId": {
          "S": "order"
        }
      },
      "SequenceNumber": "200000000008043390149",
      "SizeBytes": 61,
      "StreamViewType": "NEW_IMAGE"
    },
    "eventSourceARN": "arn:aws:dynamodb:eu-central-1EDACTED:table/sequence-numbers-sequences/stream/2023-07-20T13:29:55.239"
  }
]

But need a single object instead of a top-level array. How can I do this? I tried to use an input transformer in the pipe ({"payload": <$>} as well as {"payload": <$[0]>}), but it caused the pipe to fail.


Solution

  • You could use the ArrayGetItem intrinsic function to directly target the first element in the array:

    "States.ArrayGetItem($, 0)"
    

    You'd want to be sure that the array length is always exactly one. This seems like a reasonable assumption in your case. An alternate approach that is tolerant of any array length would be to embed your task in a Map state.