Kgn-web
Kgn-web

Reputation: 7565

AWS - how to pass DynamoDb table data to Lambda function

Below is my customer table in DynamoDb

name: string

I have linked a trigger that would call Lambda function that in turn calls my app Endpoint which would do data transformation & save in SQL DB

Whenever I am adding any record or updating record in the above table, I can see that Lambda function is getting called. but not sure how can I capture the table data.

I need to capture the name value of the customer dynamoDb table via Lambda function which I can pass to my Endpoint.

Newbie to this. So please excuse if it's too simple. But couldn't find the info that could drive this for me.

Thanks!

Upvotes: 1

Views: 1157

Answers (1)

jogold
jogold

Reputation: 7417

You Lambda function will receive a DynamoDB Streams Record Event (see Using AWS Lambda with Amazon DynamoDB for an example event).

You are going to map/loop over the Records key where you will find objects with eventName: INSERT. Inside the dynamodb key you will find the table data that you should process in your Lamdba function's code.

{
  "Records": [
    {
      "eventID": "1",
      "eventVersion": "1.0",
      "dynamodb": {
        "Keys": {
          "Id": {
            "N": "101"
          }
        },
        "NewImage": {
          "Message": {
            "S": "New item!"
          },
          "Id": {
            "N": "101"
          }
        },
        "StreamViewType": "NEW_AND_OLD_IMAGES",
        "SequenceNumber": "111",
        "SizeBytes": 26
      },
      "awsRegion": "us-west-2",
      "eventName": "INSERT",
      "eventSourceARN": eventsourcearn,
      "eventSource": "aws:dynamodb"
    }
  ]
}

In your case, the data should be located at Records[0].dynamodb.NewImage.name.S

If working with Node.js and mixed types in your table, I suggest using AWS.DynamoDB.Converter.unmarshall which converts a DynamoDB record into a JavaScript object. It allows you to do something like this:

const newImage = DynamoDB.Converter.unmarshall(event.Records[0].dynamodb.NewImage);

Upvotes: 3

Related Questions