AWS Step Functions Workflow: Order Processing
AWS Step Functions runs a workflow that you describe as a state machine. Each state is a step: it does work, makes a decision, waits or runs several things at once. Step Functions keeps track of where each execution is, passes data from one state to the next and records what happened, so your code does not have to.
This template models order processing. An order is validated and checked against stock. If it can be fulfilled, payment and stock reservation run at the same time, and then the order ships. If the item is out of stock the customer is told, and if a step keeps failing a Catch sends the order to a recovery step.
Scroll sideways to see the whole diagram
Start from this diagram and edit it on your own board.
By continuing, you agree to the Terms of Service and Privacy Policy, including sending images of your strokes, diagram labels and similar data to providers in the United States (Cloudflare, Inc. and TypeSafe AI, Inc.) for AI conversion.
What each part does
- Step Functions
- Runs the state machine. Starting an execution gives it an input document, and each state passes its output to the next. The type of state machine, Standard or Express, is chosen when you create it and cannot be changed afterwards.
- Workflow definition
- The state machine itself, written in the Amazon States Language (JSON). Every box inside it is a state, and the arrows are the transitions between them.
- Validate order
- A Task state. It does one unit of work, here by invoking a Lambda function that checks the order. A Task can have Retry and Catch.
- In stock?
- A Choice state. It compares values in the state's input with rules in order and moves to the first state whose rule is true. Set a Default, because an execution fails if no rule matches and there is no Default.
- Parallel
- A Parallel state. It starts all of its branches with a copy of the input, waits until every branch ends, and returns an array with one result per branch. If any branch fails, the whole state fails unless it has a Catch.
- Charge payment
- A Task in the first branch. Payment services fail in transient ways, so it is a good place for a Retry with backoff.
- Reserve stock
- A Task in the second branch. It runs at the same time as the payment.
- Ship order
- A Task that starts fulfilment once both branches have finished.
- Notify customer
- The Default path of the Choice: tell the customer the item is out of stock.
- Refund and alert
- The fallback state named in a Catch. When the Parallel state fails after its retries, the workflow moves here to undo what was done and tell someone.
How an execution moves
- Something starts an execution of the state machine with an input document, for example the order.
- Validate order runs as a Task and returns its result to the workflow.
- The Choice state checks whether the items are in stock. If the rule is true the workflow continues. Otherwise it follows Default to Notify customer.
- The Parallel state runs Charge payment and Reserve stock at the same time and waits for both.
- If a Task fails with an error named in its Retry, Step Functions waits and tries again. By default the first retry is after 1 second, with up to 3 attempts and a backoff rate of 2.
- If retries run out, or there is no Retry, a matching Catch sends the execution to the fallback state, here Refund and alert. With neither, the whole execution fails.
- When both branches succeed, Ship order runs and the execution ends.
When to use it
- Order, booking and onboarding flows with several steps, a decision and a way back when something fails.
- Coordinating several Lambda functions without writing the retry and wait logic yourself.
- Long-running processes that wait for a person or another system, such as approvals.
Common variations
Standard or Express
Standard workflows can run for up to a year, run each step exactly once unless you add a Retry, keep an execution history you can read through the API for 90 days, and are billed per state transition. Express workflows run for up to five minutes, are billed by executions, duration and memory, and suit high-volume work that is safe to repeat: asynchronous Express runs at least once, synchronous Express at most once. Express does not support the .sync and callback integration patterns.
Wait for a person
In a Standard workflow a Task can pause until something calls back with a task token, which lets the workflow wait for an approval for as long as it needs.
Process a list with a Map state
Use a Map state to run the same steps for every item in an array, such as each line of the order. The Distributed Map variant is only available in Standard workflows.
Start it from an API or an event
Put API Gateway in front to start executions from a request, or let an EventBridge rule start one when an event arrives.
Make it yours
Rename the states after your own steps and replace the rule in the Choice. Keep Retry on the Task states that call other systems.
Opens this diagram as a board you can edit.
By continuing, you agree to the Terms of Service and Privacy Policy, including sending images of your strokes, diagram labels and similar data to providers in the United States (Cloudflare, Inc. and TypeSafe AI, Inc.) for AI conversion.