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 Answers
1
Accepted Answer

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
EXPERT
Uri
answered 2 years ago
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
answered 2 years ago
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
answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions