If I am to understand your setup correctly, you have configured your S3 bucket to send notifications to an SNS topic. Then, you subscribed your Lambda function to that SNS topic. If that is the case, then the problem is that the "bucket" object doesn't exist in the event.record[0] and therefore when the Lambda function tries to access it, it throws an error.
If you set up your S3 bucket to send notifications directly to your Lambda function, then it will have the bucket object (under the s3 object). I prove this, I did the following:
- Created a Lambda function that simply dumps the event structure.
- Created an S3 bucket and had it send its notifications to the Lambda function. Here is the output:
{
'Records': [{
'eventVersion': '2.1',
'eventSource': 'aws:s3',
'awsRegion': 'us-west-2',
'eventTime': '2024-01-08T17:43:33.045Z',
'eventName': 'ObjectCreated:Put',
'userIdentity': {'principalId': 'REDACTED'},
'requestParameters': {'sourceIPAddress': 'REDACTED'},
'responseElements': {
'x-amz-request-id': 'REDACTED',
'x-amz-id-2': 'REDACTED'
},
's3': {
's3SchemaVersion': '1.0',
'configurationId': 'test-s3-post',
'bucket': {
'name': 'test-s3-notification',
'ownerIdentity': {
'principalId': 'REDACTED'
},
'arn': 'arn:aws:s3:::test-s3-notification'
},
'object': {
'key': 'testfile',
'size': 16,
'eTag': '02bcabffffd16fe0fc250f08cad95e0c',
'sequencer': '00659C3444F0B67C19'
}
}
}]
}
Note the "s3" object, with "bucket" object exist there.
Next, I created an SNS topic and had the S3 bucket send notifications to it instead of the Lambda function. Finally, I setup the Lambda function to subscribe to the SNS topic. Here is the "event" structure from that call:
{
'Records': [{
'EventSource': 'aws:sns',
'EventVersion': '1.0',
'EventSubscriptionArn': 'arn:aws:sns:us-west-2:759995470648:test-s3-trigger:0ae1af08-0301-457d-88e0-c4f8f7c9b1ca',
'Sns': {
'Type': 'Notification',
'MessageId': 'b6b4faf5-aed8-5d3e-ba04-a4e3e21fd356',
'TopicArn': 'arn:aws:sns:us-west-2:759995470648:test-s3-trigger',
'Subject': 'Amazon S3 Notification',
'Message': '{
"Service":"Amazon S3",
"Event":"s3:TestEvent",
"Time":"2024-01-08T19:18:30.998Z",
"Bucket":"test-s3-notification",
"RequestId":"REDACTED",
"HostId":"REDACTED"
}',
'Timestamp': '2024-01-08T19:18:31.025Z',
'SignatureVersion': '1',
'Signature': 'REDACTED',
'SigningCertUrl': 'REDACTED',
'UnsubscribeUrl': 'REDACTED',
'MessageAttributes': {}}
}]
}
So, as you can see, when you receive the message from SNS the bucket information is in a different place, and information regarding the object is lost.