如何在AWS Lambda函数中获取eventName?

2024-04-24 08:10:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我对AWS Lambda很陌生。当我的表被修改时,我的Lambda func和DynamoDB表之间有一个触发器设置。Lambda函数成功地将事件打印到云监视日志。我很难弄清楚如何检查事件是dynamoDB中的INSERT还是MODIFY。我想用一个if语句进行检查。当我刚print(event)

{
  "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"
    }
  ]
}

但是,将我的if语句添加到lambda func后,我得到了以下错误

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

Lambda函数:

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

如何获取eventName


Tags: lambdaeventidifdynamodbfuncinsertprint
1条回答
网友
1楼 · 发布于 2024-04-24 08:10:23

Lambda函数中接收到的事件列为“Records”键下的数组。因此,要查找事件名称,您需要执行以下操作:

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

相关问题 更多 >