In this blog, we’ll design a system where:
- Events (e.g., order placements, file uploads) are published to EventBridge.
- SQS queues act as durable buffers for downstream processing.
- Lambda functions consume events and take action (e.g., send notifications, update databases).
Architecture Overview
![EventBridge → SQS → Lambda Architecture]
(Visual: Producers → EventBridge → SQS → Lambda Consumers)
- Event Producers (e.g., API Gateway, S3, custom apps) emit events.
- EventBridge routes events to targets (e.g., SQS queues).
- SQS ensures reliable delivery and decoupling.
- Lambda processes events asynchronously.
Step-by-Step Implementation
1. Set Up an EventBridge Event Bus
Create a custom event bus (or use the default one):
aws events create-event-bus --name MyEventBus
2. Define an Event Rule to Route Events to SQS
Create a rule to forward events matching a pattern (e.g., order_placed) to an SQS queue:
aws events put-rule \
--name "OrderPlacedRule" \
--event-pattern '{"detail-type": ["order_placed"]}' \
--event-bus-name "MyEventBus"
3. Create an SQS Queue and Link It to EventBridge
Create a queue and grant EventBridge permission to send messages:
aws sqs create-queue --queue-name OrderProcessingQueue
Attach the queue as a target to the EventBridge rule:
aws events put-targets \
--rule "OrderPlacedRule" \
--targets "Id"="OrderQueueTarget","Arn"="arn:aws:sqs:us-east-1:123456789012:OrderProcessingQueue" \
--event-bus-name "MyEventBus"
4. Write a Lambda Function to Process SQS Messages
Create a Lambda function (process_order.py) to poll the queue and process orders:
import json
import boto3
def lambda_handler(event, context):
for record in event['Records']:
message = json.loads(record['body'])
order_id = message['detail']['orderId']
print(f"Processing order: {order_id}")
# Add business logic (e.g., update DynamoDB, send SNS notification)
return {"status": "processed"}
5. Configure SQS as a Lambda Trigger
In the AWS Console:
- Go to Lambda → Add Trigger → SQS.
- Select
OrderProcessingQueueand set batch size (e.g., 10 messages per invocation).
6. Test the Flow
Emit a test event to EventBridge:
aws events put-events \
--entries '[{
"EventBusName": "MyEventBus",
"Source": "my.app",
"DetailType": "order_placed",
"Detail": "{ \"orderId\": \"123\", \"amount\": 50 }"
}]'
Verify the flow:
- EventBridge routes the event to SQS.
- Lambda picks up the message and logs:
Processing order: 123
Use Cases
- Order processing (e.g., e-commerce workflows).
- File upload pipelines (e.g., resize images after S3 upload).
- Notifications (e.g., send emails/SMS for system events).
Enjoy
Thank you
Osama
