How many records can be in S3 put() event lambda trigger?

我的梦境 提交于 2020-01-02 00:17:13

问题


I need to have some lambda function which got this flow:

Triggered S3 put file event -> lambda function -> insert row to DynamoDB

When I'm creating a test using AWS from the lambda screen, I got this example with only 1 record in the Records list:

{
  "Records": [    //  <<<----------------only 1 element 
    {
      "eventVersion": "2.0",
      "eventTime": "1970-01-01T00:00:00.000Z",
      "requestParameters": {
        "sourceIPAddress": "127.0.0.1"
      },
      "s3": {
        "configurationId": "testConfigRule",
        "object": {
          "eTag": "0123456789abcdef0123456789abcdef",
          "sequencer": "0A1B2C3D4E5F678901",
          "key": "HappyFace.jpg",
          "size": 1024
        },
        "bucket": {
          "arn": "arn:aws:s3:::mybucket",
          "name": "roeyg-oregon-s3-bucket",
          "ownerIdentity": {
            "principalId": "EXAMPLE"
          }
        },
        "s3SchemaVersion": "1.0"
      },
      "responseElements": {
        "x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH",
        "x-amz-request-id": "EXAMPLE123456789"
      },
      "awsRegion": "us-east-1",
      "eventName": "ObjectCreated:Put",
      "userIdentity": {
        "principalId": "EXAMPLE"
      },
      "eventSource": "aws:s3"
    }
  ]
}

I tried in several ways to see if it's possible to get a list with more then one element in this list, like using CLI or upload several files together or even a whole folder, in all of these scenarios I got one item per one event.

My question is to know if there can be a scenario which I can get more then one file at one event?

By that, I would change my code to have a loop and not a reference to the first element like AWS suggested.


回答1:


Given your current configuration, you will only get one record per invocation. This is because each distinct S3 Put Event triggers a distinct S3 event notification with your AWS Lambda function as the recipient. Lambda can then process up to 100 concurrent executions by default, a limit that can be automatically raised depending on your incoming event rate.

AWS Lambda receives Records as a collection because it is using S3 Event Notifications to send the event to lambda, and thus using the S3 notification event message structure. In anticipation of event types that may return more than one record, the format exists as a collection. You can see a full list of S3 event notification types here.

This doesn't impact S3 notifications from events like s3:ObjectCreated:Put, so you're fine to leave the function as-is because that is how Put notifications are intended to work -- one event -> one notification -> one invokation.


That said, if you still want your code to be able to handle multiple Records per invokation, there is no harm in writing it to either:

  • process records in a loop (there is an example available in the docs for this: the python example deployment code that AWS provides loops instead of grabbing only the first record). If you go this route, consider adding further logic to filter duplicates if your application can't tolerate them.
  • log an error on >1 Records.



回答2:


  1. Upload files to a different new folder (possibly with timestamp) in S3.
  2. Once the files are uploaded, put a file which contains the path to the above folder in the S3 folder/bucket (which triggers the Lambda).
  3. The Lambda will now be triggered with the key to the file containing the path to the list of uploaded files


来源:https://stackoverflow.com/questions/40765699/how-many-records-can-be-in-s3-put-event-lambda-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!