AWS Step Functions

Difficulty: Advanced

Question

What is AWS Step Functions, and when would you use it instead of chaining Lambda functions directly?

Answer

AWS Step Functions is a serverless orchestration service that runs workflows defined as state machines, coordinating Lambda functions and more than 200 AWS services with built-in retries, error handling, branching, parallelism and waiting. You use it when a business process has several steps and you want its state, failures and history managed for you instead of hand-wiring Lambdas that call each other.

How a state machine is defined

Workflows are written in Amazon States Language (ASL), a JSON document, or built visually in Workflow Studio. The main state types are:

  • Task: do work, such as invoking Lambda, calling DynamoDB, publishing to SNS, or running an ECS task.
  • Choice: branch on input data.
  • Parallel: run fixed branches at the same time.
  • Map: run the same steps for each item in an array. Distributed Map can fan out to thousands of child executions over items in S3.
  • Wait: pause for a duration or until a timestamp.
  • Pass, Succeed, Fail: shape data or end the execution.

Each Task can declare Retry (with backoff) and Catch (route errors to a fallback state). Since late 2024, workflows can use JSONata expressions and variables, which removes much of the old InputPath/ResultPath juggling.

Standard vs Express workflows

StandardExpress
Max durationUp to 1 yearUp to 5 minutes
Execution semanticsExactly-onceAt-least-once (async) or at-most-once (sync)
PricingPer state transitionPer request and duration
HistoryVisual history kept in the consoleSent to CloudWatch Logs
Good forOrder processing, approvals, long ETLHigh-volume event processing, IoT, API backends

Integration patterns

  • Request Response: call the service and move on.
  • Run a Job (.sync): start a Glue job, ECS task or another state machine and wait for it to finish.
  • Wait for Callback (.waitForTaskToken): pause until an external system returns a task token, ideal for human approval or a third-party webhook.

Why not just chain Lambdas

If Lambda A calls Lambda B directly, you pay for A while it waits, you have to write your own retry and timeout logic, and when step 3 of 5 fails you have no clear record of what happened. Step Functions gives you:

  • Visible execution history for every run.
  • Declarative retries and compensation without code.
  • Long waits (hours or days) without paying for idle compute.
  • Direct SDK integrations, so many steps need no Lambda at all.

A classic example is a saga for a food order: reserve inventory, charge the customer through a payment gateway, assign a delivery partner. If assignment fails, Catch routes to compensating steps that refund the payment and release the inventory.

Gotchas

  • The payload passed between states is limited to 256 KB; store large data in S3 and pass a pointer.
  • Standard workflows charge per transition, so a tight loop with thousands of steps can get expensive; Express or Distributed Map may be cheaper.
  • Express workflows can run a step more than once, so tasks must be idempotent.
Order saga as a state machine. Each step is a Task with retries, and failures route to compensation states.
  1. Reserve inventory: Task writes a reservation to DynamoDB.
  2. Charge payment: Task calls the payment service with retries.
  3. Assign delivery partner: Waits for a callback token from dispatch.
  4. On failure: refund: Catch routes to a compensating refund task.
  5. On failure: release stock: Inventory reservation is undone, then Fail.
  6. Succeed: Order confirmed and execution history stored.

Code examples

ASL snippet with Retry and Catch

{
  "StartAt": "ChargePayment",
  "States": {
    "ChargePayment": {
      "Type": "Task",
      "Resource": "arn:aws:states:::lambda:invoke",
      "Parameters": { "FunctionName": "charge-payment", "Payload.
quot;: "
quot; }, "Retry": [{ "ErrorEquals": ["States.TaskFailed"], "IntervalSeconds": 2, "MaxAttempts": 3, "BackoffRate": 2 }], "Catch": [{ "ErrorEquals": ["States.ALL"], "Next": "ReleaseInventory" }], "Next": "Done" }, "ReleaseInventory": { "Type": "Task", "Resource": "arn:aws:states:::dynamodb:deleteItem", "Parameters": { "TableName": "reservations", "Key": { "orderId": { "S.
quot;: "$.orderId" } } }, "Next": "OrderFailed" }, "OrderFailed": { "Type": "Fail", "Error": "PaymentFailed" }, "Done": { "Type": "Succeed" } } }

The payment task retries with exponential backoff (2s, 4s, 8s); if it still fails, a direct DynamoDB integration releases the reservation without needing another Lambda.

Key points

  • Workflows are state machines written in Amazon States Language.
  • Standard: up to 1 year, exactly-once, priced per transition. Express: up to 5 minutes, priced per request and duration.
  • Retry and Catch give declarative error handling; use them for saga compensation.
  • Integration patterns: request response, .sync and .waitForTaskToken.
  • Payload limit between states is 256 KB; pass S3 references for larger data.

Common follow-up questions

  • When would you pick an Express workflow over a Standard one?
  • How would you implement a human approval step?
  • How does Distributed Map differ from the inline Map state?
  • Step Functions vs EventBridge or SQS-based choreography: how do you choose?

Interview tips

  • Explain orchestration vs choreography and why central state helps debugging.
  • Bring up the saga pattern with compensating steps; it is the example interviewers hope to hear.
  • Mention the Standard vs Express trade-off with durations and semantics instead of just naming them.

Concepts covered

State Machines, Amazon States Language, Standard vs Express, Saga Pattern, Service Integrations, Retry and Catch

More AWS & Cloud Fundamentals questions