How to Connect S3 Events to Lambda Functions in Separate SAM Templates?

0
5
Asked By CuriousCoder99 On

I'm working with AWS Serverless Application Model (SAM) and have a master stack that contains an S3 bucket. I've also created several child templates for lambda functions. Specifically, I have:

* A master stack that includes an S3 bucket.
* Child lambda template 1 that is supposed to trigger on S3 object creation events.
* Child lambda template 2 that triggers on S3 object deletion events.
* Another child lambda that uses an SNS topic associated with the S3 bucket.

However, I'm running into the error: `S3 events must reference an S3 Bucket in the same template`, which is frustrating because I want to avoid extra work while migrating from deployed resources in the AWS console to infrastructure as code (IaC).

The S3 bucket already has an SNS topic linked to it, and this is in the parent stack template, so I thought other lambdas could utilize that SNS topic. But it seems there's no straightforward way to attach them directly due to the limitation mentioned above. I've read conflicting things about using AWS SDK, EventBridge, or SNS to achieve this. I've even tried using `EventSourceArn` with `EventSourceMapping`, but that failed during deployment.

Is there a feasible way to connect these lambda functions to the S3 events if they're defined in separate templates? I've considered using EventBridge and SNS, but I'm not sure which is better or if there's another solution out there that I've missed.

3 Answers

Answered By LambdaLover42 On

You're right, if you're directly invoking the Lambda function from an S3 bucket, they need to be in the same SAM template. But a great alternative is to use EventBridge! This allows you to decouple resources across different templates or stacks. Check out this example of setting it up in SAM: [S3 EventBridge Example](https://serverlessland.com/patterns/s3-eventbridge). It could simplify your setup.

SolutionSeeker7 -

Using EventBridge also opens up options for triggering additional services, like Step Functions. It's pretty versatile!

CuriousCoder99 -

Thanks for the tip! I got it to work with my setup by setting the NotificationConfiguration after creating the S3 and child template Lambda. I need to discuss with my team, but EventBridge does sound promising.

Answered By S3Mastermind On

Here's how I see it going down: in your master stack, define the S3 bucket and SNS topic, then publish S3 events to that topic. In your child templates, have your lambdas subscribe to the SNS topic using a filter policy for the create and delete events. That way, you keep everything separate but still connected!

Answered By ConfigCrafter99 On

I agree with going the EventBridge route for flexibility. It might feel like more work upfront, but in the long run, it's going to save you headaches with dependencies. It lets you manage things cleanly across different stacks!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.