iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Step Functions

AWS Step Functions: serverless workflows. State machines, parallel branches, retries, and the patterns for reliable orchestration.

AWS — Step Functions

EXAMPLE
# ===== What Step Functions is =====
# Serverless state machine service for orchestrating workflows.
# JSON-based language (ASL) or Workflow Studio (visual editor).
# Handles state, retries, error handling, parallel execution.

# Use it instead of: cron jobs, hand-rolled retry loops, Lambda-calling-Lambda chains.

# ===== Two workflow types =====
# Standard: long-running (up to 1 year), 1-step transitions, exactly-once
# Express: high-volume (millions/sec), 5-min max, sync or async, at-least-once

# ===== State types =====
# - Task:     run a Lambda, ECS task, SNS, SQS, DynamoDB, ...
# - Choice:   conditional branching
# - Wait:     pause for N seconds or until timestamp
# - Parallel: run branches concurrently
# - Map:      iterate over an array
# - Pass:     pass-through (transformation)
# - Succeed / Fail: terminal states

# ===== A worked example: order processing =====
{
  "Comment": "Order pipeline",
  "StartAt": "ValidateOrder",
  "States": {
    "ValidateOrder": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:ap-southeast-2:123:function:validate",
      "Retry": [{ "ErrorEquals": ["States.ALL"], "IntervalSeconds": 2, "MaxAttempts": 3, "BackoffRate": 2.0 }],
      "Catch": [{ "ErrorEquals": ["ValidationError"], "Next": "RejectOrder" }],
      "Next": "ChargeCard"
    },
    "ChargeCard": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:ap-southeast-2:123:function:charge",
      "Next": "ChooseFulfillment"
    },
    "ChooseFulfillment": {
      "Type": "Choice",
      "Choices": [
        { "Variable": "$.physical", "BooleanEquals": true, "Next": "ShipPhysical" }
      ],
      "Default": "SendDigital"
    },
    "ShipPhysical": { "Type": "Task", "Resource": "arn:...:ship", "End": true },
    "SendDigital": { "Type": "Task", "Resource": "arn:...:digital", "End": true },
    "RejectOrder": { "Type": "Fail", "Cause": "Validation failed" }
  }
}

# ===== Parallel branches =====
{
  "Type": "Parallel",
  "Branches": [
    { "StartAt": "SendEmail", "States": { "SendEmail": { "Type": "Task", "End": true } } },
    { "StartAt": "UpdateInventory", "States": { "UpdateInventory": { "Type": "Task", "End": true } } }
  ],
  "End": true
}

# ===== Map state (parallel iteration) =====
{
  "Type": "Map",
  "ItemsPath": "$.items",
  "MaxConcurrency": 10,
  "Iterator": {
    "StartAt": "ProcessItem",
    "States": { "ProcessItem": { "Type": "Task", "End": true } }
  },
  "End": true
}

# ===== Retries + Catch =====
"Retry": [
  { "ErrorEquals": ["States.Timeout"], "IntervalSeconds": 5, "MaxAttempts": 3, "BackoffRate": 2.0 },
  { "ErrorEquals": ["States.ALL"], "IntervalSeconds": 2, "MaxAttempts": 2 }
],
"Catch": [
  { "ErrorEquals": ["CustomError"], "Next": "HandleCustom", "ResultPath": "$.error" }
]

# ===== Service integrations =====
# Call AWS services DIRECTLY without Lambda:
"Resource": "arn:aws:states:::dynamodb:putItem",
"Parameters": { "TableName": "orders", "Item": { "id": { "S.$": "$.id" } } }

# Saves Lambda cost + cold start; supports DynamoDB, SQS, SNS, EventBridge, etc.

# ===== Patterns =====
# - Standard for long-running business workflows
# - Express for high-volume event processing
# - Retries with backoff at the state level
# - Catch errors to recovery branches
# - Parallel + Map for fan-out

# ===== Pitfalls =====
# - 25KB state size limit (use S3 for big payloads)
# - Long workflows in dev console are slow to inspect (use AWS console + X-Ray)
# - Forgetting IAM permissions on the state machine role
# - Express duplicate execution semantics (at-least-once) -> idempotency

Why it matters

Step Functions orchestrate serverless workflows: state machines, retries, catches, parallel, map, direct service integrations. Use Standard for business workflows, Express for high-volume. Saves a lot of bespoke Lambda-calls-Lambda glue and gives reliable retries + observability out of the box.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
# Workflow engine — chain Lambdas with retries, parallel branches, wait states.
Try it Yourself »

Discussion

Loading…