Sub topic 1
Why orchestrate workflows with Step Functions
AWS Step Functions runs a multi-step process as a state machine. It tracks where each run is, passes data between steps, retries failures, and records every transition. This chapter explains the problem it solves, how it differs from the alternatives, and when it is the wrong tool. The later chapters cover the language and the individual state types.
Prerequisites
This chapter assumes you have:
- Basic familiarity with AWS accounts, IAM roles, and the AWS CLI or console.
- Working knowledge of JSON.
- Experience writing a simple AWS Lambda function.
- A basic understanding of retries, timeouts, and other distributed systems concepts.
You do not need to know Amazon States Language yet. The short examples below are explained as they appear.
The problem with chaining services directly
Suppose an order arrives and you must validate it, charge a card, reserve inventory, and send a confirmation. The first implementation is usually one Lambda function that calls each step in order. It works until you meet these limits:
- Time. A Lambda function runs for at most 15 minutes. A process that waits for a human approval or a slow batch job cannot live inside one invocation.
- Partial failure. If the card is charged and the inventory call then fails, the function must know which steps already succeeded. You end up writing your own state tracking.
- Retry logic. Every call needs its own retry, backoff, and timeout code. That code is tedious to write and easy to get wrong, so it gets copied between functions with small differences.
- Visibility. When a customer asks what happened to order 1001, you have to search logs from several functions and reassemble the story.
- Coupling. If function A directly invokes function B, A must know B exists, wait for it, and pay for the time it spends waiting.
Splitting the work into several functions fixes the 15-minute limit but creates a new question: who decides what runs next, and what happens when a step fails? That question is orchestration.
What an orchestrator does
An orchestrator is a central coordinator that owns the flow of a process. The individual steps do work and know nothing about each other. The orchestrator knows:
- The order of steps and the conditions that choose between paths.
- The current position of each running instance of the process.
- The data each step receives and produces.
- What to do when a step fails or takes too long.
Step Functions is the AWS managed service for this role. You describe the process as a state machine, which is a set of named states and the transitions between them. Each run of the state machine is an execution. The service stores the execution's position and data durably, so a Standard workflow can wait for up to a year without any compute running.
The diagram shows what a single function cannot easily show: the flow is visible as data. The console draws the same picture from the definition, and during an execution it colors each state by outcome.
Orchestration versus choreography
Orchestration is not the only way to coordinate services. In choreography, each service emits events and other services react, usually through Amazon EventBridge, SNS, or SQS. No component owns the whole process.
| Concern | Orchestration (Step Functions) | Choreography (events) |
|---|---|---|
| Where the flow lives | One definition, readable in one place | Spread across the subscriptions of many services |
| Coupling | Orchestrator knows each step | Services know only the event shape |
| Failure handling | Declared per state with Retry and Catch | Each consumer implements its own, plus dead-letter queues |
| Tracing one run | Built-in execution history | Needs correlation IDs and log searches |
| Adding a new reaction | Edit the state machine | Add a new subscriber without touching the producer |
| Best fit | Ordered, conditional, or long-running business processes | Fan-out notifications and loosely related reactions |
The two styles combine well. A common design uses events to start a workflow, for example an EventBridge rule that starts an execution, and a state machine to run the ordered part of the process. When the process finishes, it publishes an event for other teams to consume.
What you get from Step Functions
The service replaces code you would otherwise write yourself.
- Declarative flow. Sequences, branches, loops, parallel work, and waits are written in JSON rather than programmed.
- Built-in retries and error routing. A state can retry specific errors with exponential backoff, and a Catch clause can send the rest to a handler state.
- Direct service integrations. A state can call over 200 AWS services, such as DynamoDB, SQS, SNS, ECS, and Glue, without a Lambda function in between.
- Waiting without compute. A state can pause for a fixed time, until a timestamp, or until some other process returns a task token.
- Execution history. Every state entered, its input, its output, and any error is recorded and viewable per execution.
- Two workflow types. Standard workflows suit long-running, auditable processes. Express workflows suit short, high-volume ones. The choice is covered in a later chapter.
A first state machine
The definition is written in Amazon States Language (ASL), a JSON format. This smallest useful example invokes a Lambda function and then ends.
{
"Comment": "Validate an order, then finish",
"StartAt": "ValidateOrder",
"States": {
"ValidateOrder": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"Parameters": {
"FunctionName": "arn:aws:lambda:us-east-1:123456789012:function:ValidateOrder:$LATEST",
"Payload.$": "$"
},
"Next": "Done"
},
"Done": {
"Type": "Succeed"
}
}
}Read it from the top:
StartAtnames the first state.- Each entry in
Statesis a state with aType.ValidateOrderis aTask, which does work.Doneis aSucceedstate, which ends the execution successfully. Resourceselects the optimized Lambda integration.Parametersbuilds the request. ThePayload.$key, ending in.$, tells Step Functions to take the value from the state's input ($) rather than treat the text as a literal.Nextnames the state to run when this one succeeds.
Every state must either set Next or be terminal ("End": true, Succeed, or Fail). The definition is rejected if it is not valid, which catches many mistakes before anything runs.
To start an execution from the CLI:
aws stepfunctions start-execution \
--state-machine-arn arn:aws:states:us-east-1:123456789012:stateMachine:OrderWorkflow \
--input '{"orderId": "1001"}'The call returns an executionArn and a startDate immediately. The workflow itself runs asynchronously, so you query its status separately with describe-execution.
Costs and limits to keep in mind
Orchestration adds a service to your architecture, so check the trade-offs before adopting it.
- Pricing model. Standard workflows are billed per state transition, including transitions that only pass data. A workflow with a large loop can cost more than the compute it coordinates. Express workflows are billed by request count and duration instead.
- Payload size. The data passed between states is limited to 256 KiB. Large documents belong in Amazon S3, with the state carrying only a reference.
- History size. A Standard execution can record at most 25,000 history events. Very long loops need to be split or use a Map state mode designed for scale.
- Language. ASL is JSON and has no general-purpose computation. Heavy logic belongs in the Lambda functions or services the states call.
The 256 KiB limit is the one that most often surprises new users, so design for it from the start.
When Step Functions is the wrong tool
Pick something else when:
- The process is a single call with one retry. A Lambda function with a destination or a dead-letter queue is simpler.
- Components should react independently and no one needs a single view of the process. Choreography with events is a better fit.
- The workflow is mostly data transformation inside one compute job. A Glue job or an ECS task keeps that logic together.
- You need sub-millisecond latency. Each state transition adds overhead, so a hot request path should not run through a state machine.
Summary
Orchestration moves the questions of ordering, waiting, retrying, and recording out of your application code and into a service built for them. Step Functions expresses the process as a state machine in ASL, runs each execution durably, and shows the full path of every run. The cost is a new service to learn, a per-transition or per-request price, and data size limits. The following chapters build up from the structure of a definition to individual state types, data handling, error handling, and operations.
Exercise
An order process must wait up to three days for a manager's approval before continuing, and then charge a card. A teammate proposes one Lambda function that sleeps until the approval arrives. Give two reasons this design fails, and name the Step Functions feature that addresses the waiting.
Show answer
First, a Lambda function runs for at most 15 minutes, so it cannot wait three days. Second, even for shorter waits you would pay for compute that does nothing, and a crash would lose the position in the process. A Standard workflow can pause for up to a year without running compute. It does this with a Task state that uses the .waitForTaskToken integration pattern, or with a Wait state for a fixed time, and it records the execution state durably.
Exercise
Your payment service and your email service should react whenever an order is placed, and neither needs to know about the other. Would you choose orchestration or choreography for this part, and why?
Show answer
Choreography. Publishing an order-placed event to EventBridge or SNS lets each service subscribe independently, and new subscribers can be added without changing the producer. A state machine would add coupling, because it would have to know about each service. Orchestration fits better when steps must run in a defined order or depend on earlier results.