Lambda gọi Lambda → callback hell. Thêm retry, timeout, error handling → code phình gấp 3. Step Functions giải quyết: bạn định nghĩa workflow bằng JSON (ASL), nó lo execution, retry, error handling.

Mình từng thấy một Lambda handler 50 dòng code nghiệp vụ, phình lên 200 dòng vì retry/timeout/error handling. Step Functions loại bỏ hết code boilerplate đó, giao cho state machine.

Một lần mình gặp case Step Functions timeout khi Lambda cold start + retry mất 30 giây, vượt task timeout mặc định. Bài học: luôn set timeout Lambda < task timeout, và để lại buffer cho retry.


  flowchart LR
    Start["Start"] --> Validate["Validate Order<br/>Lambda"]
    Validate -->|"valid"| Charge["Charge Payment<br/>Lambda · Retry 3x"]
    Validate -->|"invalid"| Fail["Fail"]
    Charge -->|"success"| Parallel
    Charge -->|"failed"| Refund["Refund"]
    Parallel["Parallel: Email + Push Notify"] --> End["End"]
{
  "StartAt": "Validate Order",
  "States": {
    "Validate Order": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:...",
      "Retry": [
        { "ErrorEquals": ["States.ALL"], "MaxAttempts": 3, "BackoffRate": 2 }
      ],
      "Next": "Charge Payment"
    },
    "Charge Payment": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:...",
      "Catch": [{ "ErrorEquals": ["PaymentFailed"], "Next": "Refund" }],
      "Next": "Parallel Notifications"
    },
    "Parallel Notifications": {
      "Type": "Parallel",
      "Branches": [
        {
          "StartAt": "SendEmail",
          "States": {
            "SendEmail": { "Type": "Task", "Resource": "...", "End": true }
          }
        },
        {
          "StartAt": "PushNotify",
          "States": {
            "PushNotify": { "Type": "Task", "Resource": "...", "End": true }
          }
        }
      ],
      "End": true
    },
    "Refund": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:...",
      "End": true
    }
  }
}
StateUse Case
TaskGọi Lambda, ECS, SNS, SQS, HTTP
ChoiceBranch if/else
ParallelChạy nhiều nhánh đồng thời
MapFor each item xử lý song song — Inline (max 40), Distributed (up to 10,000 concurrent)
WaitDelay N giây hoặc đến timestamp
CallbackPause chờ human approval (task token pattern)

Express vs Standard

StandardExpress
DurationTối đa 1 nămTối đa 5 phút
Rate2,000/s (default service quota, có thể request tăng)100,000/s (default service quota, có thể request tăng)
Cost$25/1M transitions$1/1M transitions
History90 ngày (mặc định, có thể cấu hình tới 1 năm)CloudWatch Logs

Bài sau: Phần 29: Cognito — authentication & authorization