Feb 5, 2025 - ⧖ 2 min
Introduction
In modern cloud architectures, it's common to have multiple services reacting to the same event. For example, when a file is uploaded to an S3 bucket, you might want to trigger a notification service and also send the file metadata to a data mesh for compliance purposes. However, AWS S3 events are designed to trigger a single Lambda function by default. So, how can we trigger multiple Lambda functions from a single S3 operation?
In this post, we'll explore how to achieve this using AWS EventBridge and Terraform. By the end, you'll have a clear understanding of how to set up this architecture and deploy it using Infrastructure as Code (IaC).
Use Case
To illustrate the situation and solutions, let's consider this use case:
- AWS Lambda 1 - Notification Service: This lambda receives S3::PutObject operations to trigger a notification service for users.
- AWS Lambda 2 - Mesh Service: This lambda sends the S3::PutObject operation object and attributes to a data mesh team for compliance purposes.
- Bucket S3 - active-contracts-bucket: This is the source bucket that triggers both Lambda functions when an object is uploaded.
- EventBridge Notification - multiple-lambda-trigger: This is the event configuration that maps both Lambdas to the S3 operations.
Solution Overview
To trigger multiple Lambda functions from a single S3 operation, we'll use AWS EventBridge. EventBridge allows you to create event-driven architectures by routing events from various sources (like S3) to multiple targets (like Lambda functions). Here's how it works:
- S3 Event Notification: When an object is uploaded to the S3 bucket, it sends an event to EventBridge.
- EventBridge Rule: A rule in EventBridge captures the S3 event and routes it to multiple Lambda functions.
- Lambda Functions: Both Lambda functions are triggered simultaneously, each performing its specific task.
This approach decouples the S3 bucket from the Lambda functions, making the architecture more scalable and maintainable.
Implementation with Terraform
Now, let's implement this solution using Terraform. Below is the step-by-step configuration:
1. S3 Bucket Configuration
First, we'll create the S3 bucket that will trigger the event:
resource "aws_s3_bucket" "active_contracts_bucket" {
bucket = "active-contracts-bucket"
acl = "private"
}
<!-- Content Injected to every content markdown footer -->
[github]: https://github.com/pedro-hbl
Feb 5, 2025 - ⧖ 2 min
Pedro Lopes