EventBridge and Step Functions are your go-to AWS services for building robust, event-driven applications.
Let’s see EventBridge and Step Functions in action. Imagine an e-commerce order processing system. When a new order comes in, it generates an OrderCreated event. This event can trigger a Step Functions state machine to orchestrate the entire order fulfillment workflow.
Here’s a simplified OrderCreated event:
{
"version": "0",
"id": "a1b2c3d4-e5f6-7890-1234-567890abcdef",
"detail-type": "OrderCreated",
"source": "com.my-ecommerce.orders",
"account": "123456789012",
"time": "2023-10-27T10:00:00Z",
"region": "us-east-1",
"resources": [],
"detail": {
"orderId": "ORD-987654",
"customerId": "CUST-12345",
"items": [
{"productId": "PROD-A", "quantity": 2},
{"productId": "PROD-B", "quantity": 1}
],
"totalAmount": 150.75
}
}
This OrderCreated event is sent to an EventBridge Event Bus. We then set up a rule on this Event Bus to match events with detail-type: OrderCreated and source: com.my-ecommerce.orders. The target for this rule is a Step Functions state machine.
The Step Functions state machine definition (Amazon States Language) might look something like this:
{
"Comment": "E-commerce Order Fulfillment Workflow",
"StartAt": "ValidateOrder",
"States": {
"ValidateOrder": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:123456789012:function:validateOrderFunction",
"Next": "ProcessPayment"
},
"ProcessPayment": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:123456789012:function:processPaymentFunction",
"Next": "UpdateInventory"
},
"UpdateInventory": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:123456789012:function:updateInventoryFunction",
"Next": "ShipOrder"
},
"ShipOrder": {
"Type": "Task",
"Resource": "arn:aws:lambda:us-east-1:123456789012:function:shipOrderFunction",
"End": true
}
}
}
When EventBridge receives the OrderCreated event, it invokes this state machine, passing the event’s detail payload as the state machine’s input. Each state in the state machine then executes a specific task, like invoking a Lambda function.
The core problem this solves is managing complex, multi-step processes that are triggered by asynchronous events. Instead of writing intricate code to poll for events or manage state across multiple services, you declaratively define your workflow in Step Functions. EventBridge acts as the central nervous system, routing events to the appropriate workflows.
Here’s how you configure the EventBridge rule to target the Step Functions state machine:
- Go to the EventBridge console.
- Navigate to "Rules" and click "Create rule."
- Define rule detail:
- Name:
OrderCreatedWorkflowTrigger - Event bus:
default(or your custom event bus)
- Name:
- Define pattern:
- Event source:
My services - Service provider:
com.my-ecommerce.orders - Event type:
OrderCreated - (Alternatively, use a custom pattern JSON:
{"detail-type": ["OrderCreated"], "source": ["com.my-ecommerce.orders"]})
- Event source:
- Select target(s):
- Target type:
AWS service - Service:
Step Functions state machine - State machine:
E-commerceOrderFulfillment(the name of your state machine) - Execution role: Choose or create a role that grants EventBridge permission to start executions for your Step Functions state machine.
- Configure input: Select
Filtered eventand use a JSONPath like$.detailto pass only the order details to Step Functions.
- Target type:
The real power comes from Step Functions’ ability to handle retries, error handling, parallel execution, and human approval steps. For instance, if payment processing fails, Step Functions can automatically retry, send a notification, or even pause the workflow for manual intervention.
The most surprising thing about integrating EventBridge and Step Functions is how seamlessly they abstract away distributed system complexity. You’re not thinking about network latency or eventual consistency between services; you’re thinking about the logical flow of your business process. The state machine visually represents this flow, making it incredibly easy to understand and debug. EventBridge’s filtering capabilities allow a single event bus to fan out events to numerous different Step Functions workflows or other targets simultaneously, creating a highly decoupled and scalable architecture.
The next step you’ll likely encounter is managing different versions of your state machine and routing events to specific versions based on deployment needs.