Sub topic 1
Why orchestrate workflows with Step Functions
Prerequisites
This chapter assumes you can do the following:
- Work with AWS accounts, IAM roles, and the AWS CLI or console.
- Read and write JSON.
- Write a simple AWS Lambda function.
- Explain retries and timeouts in a distributed system.
The problem with chaining services
Most real jobs take several steps. An order arrives, you validate it, charge a card, reserve stock, and send a confirmation. The simplest way to connect these steps is to let each one call the next. One Lambda function invokes another, or a function writes to a queue that triggers the next function.
This works for two steps. As the job grows, the logic that controls the sequence ends up scattered across your code:
- Retries and timeouts. Each function needs its own code to retry a failed call and to give up after a while.
- Branching. Decisions such as "if validation fails, skip payment" are buried inside function bodies.
- Waiting. A function that waits for a human approval or a slow batch job is billed while it waits, and Lambda stops it at 15 minutes.
- Visibility. When a job fails, you piece together what happened from several log groups.
- Coupling. Each function must know which function comes next, so changing the order means changing code.
What orchestration changes
Orchestration moves the control logic out of your functions and into one central definition. A coordinator, called the orchestrator, decides which step runs next, passes data between steps, and handles failures. Your functions then do one job each and know nothing about the rest of the workflow.
AWS Step Functions is the AWS orchestrator. You describe the workflow as a state machine, which is a set of named states and the transitions between them. You write it in Amazon States Language (ASL), a JSON format. Chapter 2 covers ASL in detail. For now, treat it as a flowchart that the service can execute.
What Step Functions gives you
- Built-in retries and error routing. You declare them in the definition instead of coding them.
- Branching and parallelism. Choice, Parallel, and Map states express them directly.
- Waiting without compute cost. A workflow can pause for a timer or an external signal for up to a year in a Standard workflow, without running any code.
- Direct service calls. A state can call many AWS services without a Lambda function in between.
- A recorded history. Every execution keeps a step-by-step record of inputs, outputs, and errors.
Running a workflow
An execution is one run of a state machine with a given JSON input. You can start one from the CLI:
aws stepfunctions start-execution \
--state-machine-arn arn:aws:states:us-east-1:123456789012:stateMachine:OrderWorkflow \
--input '{"orderId": "1001"}'The call is asynchronous. It returns an executionArn and a startDate right away, and the workflow continues in the background. You can then follow its progress in the console or with describe-execution.
When not to use it
Orchestration adds a service, a definition to maintain, and a cost per state transition in Standard workflows. Skip it when:
- The job is a single step, or a simple fan-out where a queue or an event bus is enough.
- The steps are independent and need no shared ordering or error handling.
- All the logic lives comfortably in one function and finishes within its timeout.
Exercise
An order process invokes four Lambda functions in sequence. Payment can fail temporarily, and shipping needs a manager's approval that may take two days. Give two reasons to use Step Functions instead of chaining the functions directly.
Show answer
Any two of the following. Retry and failure routing can be declared in the state machine instead of coded in each function. The workflow can wait two days for approval without running or paying for a function, which Lambda's 15-minute limit would not allow. The execution history shows exactly which step failed. The order of steps lives in one place, so functions don't need to know about each other.