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년 전
profile picture
전문가
검토됨 17일 전
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년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠