How to know which s3 bucket invokes the lambda in a cdk app?

0

Say I have a CDK app deploying the S3 -> EventBridege -> Lambda stack, and there are many s3 buckets that will invoke the same lambda. The lambda handler runs a program differetly based on the source bucket.

Normally, we can read the bucket name from the incoming event to know which bucket invokes the lambda, but since the bucket name might be different each time a new deployment is made, depending on the bucket name seems not reliable.

So, is there a better way to know what the bucket is invoking the lambda in the lambda handler code?

3 個答案
1
已接受的答案

If I understand correctly, you deploy multiple stacks, each with a different bucket, but all with the same Lambda function. You want the bucket name to be determined by CFN, but you need to know in the function the origin bucket, or more specifically, what the bucket is used for, not its name.

If this is the case and your function performs different actions based on the origin bucket, I would recommend creating different functions. This way each stack creates a different S3 bucket with a different function, that knows what to do.

profile pictureAWS
專家
Uri
已回答 2 年前
0

Hi, while creating an S3 bucket using CDK you can manually set the bucketName property: https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-s3.Bucket.html

This will ensure that the bucket has the same name every-time it is deployed, which should address your issue.

Thanks,

Marrick.

Marrick
已回答 2 年前
0

Fair enough! In that case, your best bet is probably to using S3 to publish event messages to SNS which would in-turn trigger your Lambda function: https://docs.aws.amazon.com/AmazonS3/latest/userguide/how-to-enable-disable-notification-intro.html. Specifically, configuring each bucket to send SNS events with a different SNS Topic would allow your Lambda function to determine where the event came from.

Marrick
已回答 2 年前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南