1

I am very new to AWS Lambda. I have a trigger setup between my Lambda func and a DynamoDB table when my table is modified. The Lambda function successfully prints the event to cloud watch logs. I am having trouble figuring out how to check if the event is an INSERT or MODIFY in dynamoDB. I want to check with an if statement. When I just print(event) I get

{
  "Records": [
    {
      "eventID": "26e6ac4f1c16fc40fd91536430c1ac72",
      "eventName": "MODIFY",
      "eventVersion": "1.1",
      "eventSource": "aws:dynamodb",
      "awsRegion": "us-east-1",
      "dynamodb": {
        "ApproximateCreationDateTime": 1612293148,
        "Keys": {
          "id": {
            "S": "d66ec59b-b807-4db9-96ed-3ebd3638779a450b7a75-6dce-4be9-babf-1077adb84b02"
          }
        },
        "NewImage": {
          "__typename": {
            "S": "Conversation"
          },
          "members": {
            "L": [
              {
                "S": "450b7a75-6dce-4be9-babf-1077adb84b02"
              },
              {
                "S": "d66ec59b-b807-4db9-96ed-3ebd3638779a"
              }
            ]
          },
          "isRead": {
            "BOOL": false
          },
          "recipient": {
            "S": "d66ec59b-b807-4db9-96ed-3ebd3638779a"
          },
          "latestMessage": {
            "S": "Hey man"
          },
          "id": {
            "S": "d66ec59b-b807-4db9-96ed-3ebd3638779a450b7a75-6dce-4be9-babf-1077adb84b02"
          },
          "latestMessageSenderSub": {
            "S": "450b7a75-6dce-4be9-babf-1077adb84b02"
          },
          "updatedAt": {
            "S": "02/02/2021 - 14:12:27"
          }
        },
        "OldImage": {
          "__typename": {
            "S": "Conversation"
          },
          "members": {
            "L": [
              {
                "S": "450b7a75-6dce-4be9-babf-1077adb84b02"
              },
              {
                "S": "d66ec59b-b807-4db9-96ed-3ebd3638779a"
              }
            ]
          },
          "isRead": {
            "BOOL": false
          },
          "recipient": {
            "S": "d66ec59b-b807-4db9-96ed-3ebd3638779a"
          },
          "latestMessage": {
            "S": "Yooooo"
          },
          "id": {
            "S": "d66ec59b-b807-4db9-96ed-3ebd3638779a450b7a75-6dce-4be9-babf-1077adb84b02"
          },
          "latestMessageSenderSub": {
            "S": "450b7a75-6dce-4be9-babf-1077adb84b02"
          },
          "updatedAt": {
            "S": "02/02/2021 - 12:40:09"
          }
        },
        "SequenceNumber": "47015900000000007518411700",
        "SizeBytes": 753,
        "StreamViewType": "NEW_AND_OLD_IMAGES"
      },
      "eventSourceARN": "arn:aws:dynamodb:us-east-1:69639064567851005:table/Conversation-jklhofiuhvouerhoh-dev/stream/2021-01-23T21:25:34.783"
    }
  ]
}

But with my if statements added to my lambda func I get the following error

[ERROR] KeyError: 'eventName'Traceback (most recent call last):  File "/var/task/lambda_function.py", line 6, in lambda_handler    if event['eventName'] == 'MODIFY':

Lambda Function:

import json

def lambda_handler(event, context):
    # TODO implement
    print(event)
    if event['eventName'] == 'MODIFY':
        #Run some code
        print('MODIFY')
    elif event['eventName'] == 'INSERT':
        #Run some code
        print('INSERT')
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

How can I get the eventName?

1 Answer 1

5

The events you're receiving in your Lambda function are listed as an array under the 'Records' key. So to find the event name you'll need to do something like this:

for e in event['Records']:
    if e['eventName'] == 'MODIFY':
        print('MODIFY')
    elif e['eventName'] == 'INSERT':
        #Run some code
        print('INSERT')
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }
3
  • 1
    Bonus point for iterating over the event records. Many people simply assume there will be one, and only one, event and they only process event['Records'][0] and then wonder why some events are not being processed.
    – jarmod
    Feb 2, 2021 at 19:35
  • This solution worked! How do I get the table keys as well such as the id?
    – GBMR
    Feb 2, 2021 at 20:59
  • 1
    @GBMR - list(e['dynamodb']['Keys'].keys()) should get all the keys I think. If you want the key values too then just e['dynamodb']['Keys']
    – zyd
    Feb 2, 2021 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.