1
votes

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

1 Answers

5
votes

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!')
    }