Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 70 ยท Serverless and messaging

EventBridge and Step Functions

Route events with EventBridge buses, rules and patterns, then orchestrate work with Step Functions state machines, retries, catches and Express vs Standard.

SQS and SNS move messages from one place to another. Two more services answer the questions that come next. EventBridge decides which consumers should hear about an event, by matching it against rules. Step Functions decides what happens in which order when a job has many steps, and what to do when a step fails.

Together they let you build workflows without writing glue code: "when an order is placed, charge the card, reserve stock, and email the customer, and if the card is declined, cancel the order." This lesson covers both services and gives you two small emulators so you can see the matching and the retry logic work.

EventBridge and Step Functions run inside AWS and cannot run on this site. The JSON, YAML and CLI snippets are read-only. The Python examples are stand-ins that follow the same rules for a useful subset of features, so you can experiment. Each section says what is real and what is simplified.

EventBridge: buses, rules and targets

An event is a small JSON document that says something happened. A bus receives events. A rule on the bus has an event pattern (a filter) and one or more targets (where matching events go).

  • The default bus in every account already receives events from AWS services, such as "an EC2 instance changed state" or "an S3 object was created" (when enabled).
  • Custom buses receive events from your own applications, so you can keep your domain events apart from AWS ones.
  • Partner buses receive events from SaaS providers.
  • Rules can send events to over 20 kinds of targets: Lambda, SQS, SNS, Step Functions, another bus (even in another account), API destinations (HTTPS endpoints), and more.
flowchart LR
    P1["Order service"] --> BUS["Custom bus: shop"]
    P2["AWS service events"] --> DEF["Default bus"]
    BUS --> R1{"Rule: order placed"}
    BUS --> R2{"Rule: large orders"}
    R1 --> T1["Lambda: send email"]
    R2 --> T2["SQS: review queue"]
    R2 --> T3["Step Functions: fraud check"]

Producers put events on a bus. Each rule filters the stream and fans matching events out to its targets.

A producer sends an event with the SDK. The envelope fields source, detail-type and detail are the ones patterns usually look at (read-only):

Terminal
aws events put-events --entries '[{
  "EventBusName": "shop",
  "Source": "shop.orders",
  "DetailType": "OrderPlaced",
  "Detail": "{\"orderId\":\"o-1001\",\"total\":250,\"country\":\"DE\"}"
}]'

Compared with SNS, EventBridge filters are richer, the rules are managed centrally on the bus (not by each subscriber), and it has a built-in schema registry and replay. SNS is lighter and faster for plain high-volume fan-out. A common design uses both.

Event patterns

A pattern is JSON with the same shape as the event. Each field in the pattern lists the acceptable values, as an array. These rules cover most real patterns:

  • The values in an array are OR-ed: "country": ["DE", "FR"] matches either.
  • Different fields are AND-ed: all of them must match.
  • A field that is not in the pattern is ignored.
  • A nested object in the pattern matches the same nested object in the event.
  • If the event field is itself an array, the pattern matches when any element matches.
  • Content filters are objects in the array: {"prefix": "eu-"}, {"suffix": ".png"}, {"anything-but": ["test"]}, {"numeric": [">=", 100]} and {"exists": true} (or false).

A pattern for "large orders in Germany or France":

JSON
{
  "source": ["shop.orders"],
  "detail-type": ["OrderPlaced"],
  "detail": {
    "country": ["DE", "FR"],
    "total": [{ "numeric": [">=", 100] }]
  }
}

The next stand-in implements those rules. It is not the real matching engine: real EventBridge has more operators (equals-ignore-case, wildcard, cidr, $or) and exact rules for numbers and nulls, but the logic you see here is the core of it.

Python An event-pattern matcher

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first event matches. The second fails the numeric rule, the third the country list, the fourth the exists: false rule on coupon, and the fifth the source. Notice how little code the OR-within-a-field and AND-across-fields rule needs.

Schedules and delivery

Rules can also run on a clock instead of an event pattern. EventBridge Scheduler is the newer, dedicated service for this: one-time or recurring schedules with rate(5 minutes), cron(0 8 ? * MON-FRI *) or at(2030-01-01T09:00:00), in a time zone you choose, with millions of schedules per account and a flexible time window. Use it instead of Lambda-based timers.

Delivery to a target is at least once. EventBridge retries failed deliveries for up to 24 hours by default, and a rule's target can have a dead-letter queue (an SQS queue) for events that could not be delivered. Your targets must handle duplicates, which is the same idempotency lesson as SQS and SNS.

Step Functions: workflows as state machines

When a job has several steps, the tempting design is one big Lambda function that calls everything in order. It breaks the moment the job takes longer than 15 minutes, a step needs a retry with backoff, or you want to see where an execution got stuck. Step Functions moves the control flow out of your code and into a state machine written in the Amazon States Language (ASL), a JSON format.

A state machine is a set of named states with a StartAt and transitions (Next, or End). The state types you will use most:

State type What it does
Task Calls a service: a Lambda function, a DynamoDB write, an ECS task, an SQS send, and over 200 other API actions.
Choice Branches on the data, like an if/else.
Wait Pauses for a number of seconds or until a timestamp.
Pass Passes (or injects) data without doing work.
Parallel Runs several branches at once.
Map Runs the same steps for every item in a list.
Succeed / Fail Ends the execution with success or an error.

A small order workflow in ASL (read-only):

JSON
{
  "Comment": "Process one order",
  "StartAt": "ChargeCard",
  "States": {
    "ChargeCard": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "Parameters": { "FunctionName": "charge-card", "Payload.$": "$" },
      "Retry": [
        {
          "ErrorEquals": ["States.TaskFailed"],
          "IntervalSeconds": 2,
          "BackoffRate": 2.0,
          "MaxAttempts": 3
        }
      ],
      "Catch": [
        { "ErrorEquals": ["CardDeclined"], "Next": "CancelOrder" }
      ],
      "Next": "ReserveStock"
    },
    "ReserveStock": { "Type": "Task", "Resource": "arn:aws:states:::lambda:invoke", "End": true },
    "CancelOrder": { "Type": "Fail", "Error": "OrderCancelled", "Cause": "Card was declined" }
  }
}
flowchart TD
    S(["Start"]) --> C["ChargeCard"]
    C -- "success" --> R["ReserveStock"]
    C -- "CardDeclined" --> X["CancelOrder (Fail)"]
    C -- "transient error:<br/>retry with backoff" --> C
    R --> E(["End"])

The retry loop and the catch edge are part of the workflow definition, not of the Lambda code.

Retries and error handling

Two fields make workflows resilient, and both are declared per state:

  • Retry lists error names and how to retry: IntervalSeconds is the first delay, BackoffRate multiplies it each time (2.0 means 2 s, 4 s, 8 s), and MaxAttempts is the limit (the default is 3, and 0 means never retry). Retriers are checked in order, and the first one whose ErrorEquals matches is used. States.ALL matches any error, except that it must be last in the list.
  • Catch says where to go once retries are used up (or when no retrier applies). The error details can be stored in the state's output with ResultPath for the next state to read.

Retry transient failures (timeouts, throttling). Catch business failures (CardDeclined) and route them to compensation steps. A common mistake is retrying a non-idempotent action without an idempotency key, which can charge a card twice.

The runner below is a stand-in for the Step Functions engine. It supports Task, Choice, Pass, Succeed and Fail, with Retry (backoff computed, but no real waiting) and Catch. The task functions are ordinary Python functions.

Python A tiny state machine runner with retry and catch

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Order o-1 takes the straight path. Order o-2 fails twice at the gateway, waits 2 s and then 4 s (simulated), succeeds on the third try, and its total of 1500 sends it through the Review branch. Order o-3 hits the Catch: no retrier lists CardDeclined, so it moves to Cancel immediately.

Standard vs Express workflows

When you create a state machine you choose a type, and it cannot be changed later:

Standard Express
Maximum duration 1 year 5 minutes
Execution semantics Exactly-once for the workflow At-least-once (asynchronous) or at-most-once (synchronous)
Rate Thousands of executions per second Over 100,000 per second
Pricing Per state transition Per number of executions and duration
History Kept for 90 days, visible in the console Sent to CloudWatch Logs
Typical use Long business processes, human approval, anything that must not run twice High-volume, short pipelines: stream processing, mobile back ends

Standard also supports waiting for a human or an external system with the .waitForTaskToken pattern: the workflow pauses with no compute running until your code calls SendTaskSuccess with the token.

Which one when?

  • Use EventBridge to route events from many producers to many independent consumers, with filtering.
  • Use Step Functions to coordinate one process with order, branching, retries and compensation.
  • They combine well: an EventBridge rule starts a state machine as its target, and a state machine can put its own events on a bus.
  • For a simple queue-backed consumer you do not need either. A Lambda behind SQS is enough.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. An EventBridge pattern contains "country": ["DE", "FR"] and "total": [{"numeric": [">=", 100]}]. Which events match?

    Values inside one array are alternatives (OR), but different fields are combined with AND. Fields that the pattern does not mention are ignored.
  2. 2. What is the main job of a Retry block in a Step Functions state?

    Retry re-runs the same state, with an interval and backoff rate, up to MaxAttempts. Moving to another state is the job of Catch.
  3. 3. A payment step fails with a business error, CardDeclined. What is the right way to handle it?

    A declined card will not succeed on retry. Retry is for transient failures such as timeouts, while Catch sends business failures to the right branch.
  4. 4. Which workflow type fits a process that waits several days for a human approval?

    Express workflows stop after five minutes. Standard workflows can run for up to a year and can pause with no compute until a callback arrives.

Summary

  • EventBridge receives events on buses, matches them with rule patterns and delivers them to targets. Delivery is at least once, with retries and optional dead-letter queues.
  • A pattern ORs the values in one array and ANDs the fields. Content filters include prefix, suffix, anything-but, numeric and exists.
  • EventBridge Scheduler runs one-time and recurring schedules with rate, cron and at expressions.
  • Step Functions describes a workflow as a state machine in JSON. Common states are Task, Choice, Wait, Pass, Parallel, Map, Succeed and Fail.
  • Use Retry for transient errors with backoff, and Catch to route to other states. Pick Standard for long, exactly-once work and Express for short, high-volume work.

Next: Elastic Load Balancing