AWS Learning
Compute

AWS Step Functions

Serverless Workflow Orchestration, State Machines, Standard/Express, 200+ AWS Integrations

Tổng quan

AWS Step Functions là dịch vụ serverless orchestration cho phép bạn kết hợp các AWS services thành các workflows (state machines) để xây dựng distributed applications, automate processes, orchestrate microservices, và tạo data/ML pipelines.

┌─────────────────────────────────────────────────────────────────────┐
│                     WHAT IS AWS STEP FUNCTIONS?                     │
│                                                                     │
│   Step Functions = Serverless Workflow Orchestration Service        │
│                                                                     │
│   ┌─────────────────────────────────────────────────────────────┐   │
│   │                                                             │   │
│   │   Event Sources        Step Functions         AWS Services  │   │
│   │                                                             │   │
│   │  ┌───────────┐      ┌─────────────────┐    ┌───────────┐    │   │
│   │  │ API       │      │                 │    │  Lambda   │    │   │
│   │  │ Gateway   │─────▶│  State Machine  │───▶│           │    │   │
│   │  └───────────┘      │                 │    └───────────┘    │   │
│   │                     │  ┌───────────┐  │                     │   │
│   │  ┌───────────┐      │  │  Task     │  │    ┌───────────┐    │   │
│   │  │ Event     │─────▶│  │  Choice   │  │───▶│ DynamoDB  │    │   │
│   │  │ Bridge    │      │  │  Parallel │  │    └───────────┘    │   │
│   │  └───────────┘      │  │  Map      │  │                     │   │
│   │                     │  │  Wait     │  │    ┌───────────┐    │   │
│   │  ┌───────────┐      │  │  Pass     │  │───▶│ ECS/EKS   │    │   │
│   │  │ Schedule  │─────▶│  │  Succeed  │  │    └───────────┘    │   │
│   │  │ (cron)    │      │  │  Fail     │  │                     │   │
│   │  └───────────┘      │  └───────────┘  │    ┌───────────┐    │   │
│   │                     │                 │───▶│ SNS/SQS   │    │   │
│   │  ┌───────────┐      │  Visual Editor  │    └───────────┘    │   │
│   │  │ Lambda    │─────▶│  (Workflow      │                     │   │
│   │  │ (nested)  │      │   Studio)       │    ┌───────────┐    │   │
│   │  └───────────┘      └─────────────────┘───▶│ 200+ AWS  │    │   │
│   │                                            │ Services  │    │   │
│   │                                            └───────────┘    │   │
│   │                                                             │   │
│   │   KEY BENEFITS:                                             │   │
│   ├── ✅ Visual workflows - thiết kế bằng drag-and-drop         │   │
│   ├── ✅ Built-in error handling - Retry, Catch, Fallback       │   │
│   ├── ✅ State management - tự động track state giữa các steps  │   │
│   ├── ✅ 200+ AWS SDK integrations - gọi bất kỳ AWS service     │   │
│   ├── ✅ Parallel & Map - xử lý song song và lặp dynamic        │   │
│   └── ✅ Pay-per-use - chỉ trả cho state transitions thực tế    │   │
│   └─────────────────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────────────────┘

Khi nào dùng Step Functions?

Use CaseVí dụ
Microservice OrchestrationGọi nhiều Lambda/ECS services theo thứ tự
Data Processing / ETLXử lý dữ liệu qua nhiều bước: extract → transform → load
ML PipelineTrain model → evaluate → deploy → monitor
Human Approval WorkflowsOrder processing với bước approve thủ công
IT AutomationSecurity incident response, auto-remediation
Media ProcessingTranscode video → generate thumbnails → publish

Nguồn: AWS Step Functions - What is Step Functions?


Kiến trúc và Core Concepts

┌─────────────────────────────────────────────────────────────────────┐
│                    STEP FUNCTIONS ARCHITECTURE                      │
│                                                                     │
│   ┌─────────────────────────────────────────────────────────────┐   │
│   │                   STATE MACHINE (Workflow)                  │   │
│   │                                                             │   │
│   │   Input ──▶ ┌─────────┐    ┌─────────┐    ┌─────────┐       │   │
│   │             │  State  │───▶│  State  │───▶│  State  │       │   │
│   │             │  (Task) │    │ (Choice)│    │  (Task) │       │   │
│   │             └─────────┘    └────┬────┘    └─────────┘       │   │
│   │                                 │                           │   │
│   │                          ┌──────┴──────┐                    │   │
│   │                          │             │                    │   │
│   │                     ┌────▼────┐  ┌─────▼────┐               │   │
│   │                     │ Branch  │  │ Branch   │               │   │
│   │                     │   A     │  │   B      │               │   │
│   │                     └────┬────┘  └─────┬────┘               │   │
│   │                          │             │                    │   │
│   │                          └──────┬──────┘                    │   │
│   │                                 │                           │   │
│   │                          ┌──────▼──────┐                    │   │
│   │                          │   End       │ ──▶ Output         │   │
│   │                          └─────────────┘                    │   │
│   │                                                             │   │
│   │   EXECUTION = 1 lần chạy state machine                      │   │
│   │   STATE = 1 bước trong workflow                             │   │
│   │   TRANSITION = di chuyển từ state này sang state kế tiếp    │   │
│   └─────────────────────────────────────────────────────────────┘   │
└─────────────────────────────────────────────────────────────────────┘

Core Concepts

ConceptMô tả
State MachineWorkflow definition (JSON/YAML) chứa các states và transitions
StateMột bước trong workflow (Task, Choice, Parallel, Map, Wait, Pass, Succeed, Fail)
ExecutionMột lần chạy (instance) của state machine
TransitionDi chuyển từ state này sang state tiếp theo
Amazon States Language (ASL)JSON-based language để định nghĩa state machines
Workflow StudioVisual editor (drag-and-drop) trong AWS Console
ActivityWorker bên ngoài Step Functions (long-running tasks)
Task TokenToken cho phép external process callback về Step Functions

Workflow Types: Standard vs Express

Step Functions có 2 loại workflow, và không thể thay đổi sau khi tạo.

┌─────────────────────────────────────────────────────────────────────┐
│                    STANDARD vs EXPRESS WORKFLOWS                    │
│                                                                     │
│   ┌──────────────────────────┐   ┌──────────────────────────┐       │
│   │     STANDARD WORKFLOW    │   │     EXPRESS WORKFLOW     │       │
│   │                          │   │                          │       │
│   │  Duration: tối đa 1 NĂM  │   │  Duration: tối đa 5 PHÚT │       │
│   │                          │   │                          │       │
│   │  Execution: exactly-once │   │  Execution: at-least-once│       │
│   │                          │   │  (async) / at-most-once  │       │
│   │  Rate: 2,000/giây        │   │  (sync)                  │       │
│   │                          │   │  Rate: 100,000/giây      │       │
│   │  Billing: per state      │   │                          │       │
│   │  transition              │   │  Billing: per execution  │       │
│   │                          │   │  + duration + memory     │       │
│   │  History: Step Functions │   │                          │       │
│   │  console (90 ngày)       │   │  History: CloudWatch Logs│       │
│   │                          │   │                          │       │
│   │  Use for:                │   │  Use for:                │       │
│   │  • Long-running workflows│   │  • High-volume events    │       │
│   │  • Non-idempotent actions│   │  • IoT data ingestion    │       │
│   │  • Human approval steps  │   │  • Streaming processing  │       │
│   │  • Audit trail needed    │   │  • Idempotent actions    │       │
│   │                          │   │  • API backends          │       │
│   │  Supports:               │   │                          │       │
│   │  ✅ Request Response     │   │  Supports:               │       │
│   │  ✅ Run a Job (.sync)    │   │  ✅ Request Response     │       │
│   │  ✅ Wait for Callback    │   │  ❌ Run a Job (.sync)    │       │
│   │  ✅ Distributed Map      │   │  ❌ Wait for Callback    │       │
│   │  ✅ Activities           │   │  ❌ Distributed Map      │       │
│   │                          │   │  ❌ Activities           │       │
│   └──────────────────────────┘   └──────────────────────────┘       │
└─────────────────────────────────────────────────────────────────────┘

So sánh chi tiết

Tiêu chíStandardExpress
Max duration1 năm5 phút
Execution rate2,000/giây100,000/giây
State transition rate4,000/giây (soft limit)Gần như không giới hạn
Execution semanticsExactly-onceAt-least-once (async) / At-most-once (sync)
PricingPer state transition ($0.000025/transition)Per execution + duration + memory
Execution historyConsole + API (90 ngày)CloudWatch Logs
Run a Job (.sync)
Wait for Callback
Distributed Map
Activities

Express Workflow: Synchronous vs Asynchronous

LoạiMô tảExecution semanticsTrigger
Async ExpressStart workflow, KHÔNG đợi kết quảAt-least-onceEventBridge, nested workflow, StartExecution API
Sync ExpressStart workflow, ĐỢI kết quả trả vềAt-most-onceAPI Gateway, Lambda, StartSyncExecution API

Lưu ý: Workflow type không thể thay đổi sau khi tạo state machine.

Nguồn: Choosing workflow type


Workflow States

Step Functions có 8 loại state chia thành 2 nhóm: Task states (action) và Flow states (điều khiển luồng).

┌─────────────────────────────────────────────────────────────────────┐
│                         WORKFLOW STATES                             │
│                                                                     │
│   ┌─── ACTION STATE ───────────────────────────────────────────┐    │
│   │                                                            │    │
│   │  ┌──────────┐                                              │    │
│   │  │   TASK   │  Thực hiện 1 unit of work                    │    │
│   │  │          │  → Gọi Lambda, DynamoDB, ECS, HTTP, v.v.     │    │
│   │  └──────────┘                                              │    │
│   │                                                            │    │
│   └────────────────────────────────────────────────────────────┘    │
│                                                                     │
│   ┌─── FLOW STATES ────────────────────────────────────────────┐    │
│   │                                                            │    │
│   │  ┌──────────┐  Rẽ nhánh dựa trên điều kiện (if/else)       │    │
│   │  │  CHOICE  │  → Kiểm tra input và chọn branch phù hợp     │    │
│   │  └──────────┘                                              │    │
│   │                                                            │    │
│   │  ┌──────────┐  Chạy nhiều branches SONG SONG               │    │
│   │  │ PARALLEL │  → Tất cả branches phải hoàn thành           │    │
│   │  └──────────┘                                              │    │
│   │                                                            │    │
│   │  ┌──────────┐  Lặp qua từng phần tử trong array            │    │
│   │  │   MAP    │  → Inline (trong state machine)              │    │
│   │  │          │  → Distributed (child executions, lên tới    │    │
│   │  │          │    10,000 parallel)                          │    │
│   │  └──────────┘                                              │    │
│   │                                                            │    │
│   │  ┌──────────┐  Chờ một khoảng thời gian hoặc timestamp     │    │
│   │  │   WAIT   │  → Seconds, Timestamp, SecondsPath,          │    │
│   │  │          │    TimestampPath                             │    │
│   │  └──────────┘                                              │    │
│   │                                                            │    │
│   │  ┌──────────┐  Truyền input → output (có thể transform)    │    │
│   │  │   PASS   │  → Dùng để inject fixed data hoặc debug      │    │
│   │  └──────────┘                                              │    │
│   │                                                            │    │
│   │  ┌──────────┐  Kết thúc workflow THÀNH CÔNG                │    │
│   │  │ SUCCEED  │                                              │    │
│   │  └──────────┘                                              │    │
│   │                                                            │    │
│   │  ┌──────────┐  Kết thúc workflow THẤT BẠI (Error + Cause)  │    │
│   │  │   FAIL   │                                              │    │
│   │  └──────────┘                                              │    │
│   │                                                            │    │
│   └────────────────────────────────────────────────────────────┘    │
└─────────────────────────────────────────────────────────────────────┘

Chi tiết từng State

StateTypeMô tảVí dụ
TaskActionGọi AWS service hoặc activityInvoke Lambda, PutItem DynamoDB
ChoiceFlowRẽ nhánh theo điều kiệnIf order > $100 → approve, else → reject
ParallelFlowChạy nhiều branches đồng thờiGửi email + SMS + push notification cùng lúc
MapFlowLặp qua array itemsXử lý từng file trong S3 bucket
WaitFlowPause executionChờ 30 giây, chờ đến ngày cụ thể
PassFlowPass-through, transform dataInject default values, reshape data
SucceedFlowTerminal state - thành côngKết thúc workflow
FailFlowTerminal state - thất bạiTrả về error code + message

Nguồn: Discovering workflow states


Amazon States Language (ASL)

ASL là JSON-based language để định nghĩa state machines.

Ví dụ: Order Processing Workflow

{
  "Comment": "Order Processing Workflow",
  "StartAt": "ValidateOrder",
  "States": {
    "ValidateOrder": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-1:123456789012:function:ValidateOrder",
      "Next": "CheckInventory",
      "Retry": [
        {
          "ErrorEquals": ["ServiceUnavailable"],
          "IntervalSeconds": 2,
          "MaxAttempts": 3,
          "BackoffRate": 2.0
        }
      ],
      "Catch": [
        {
          "ErrorEquals": ["States.ALL"],
          "Next": "OrderFailed"
        }
      ]
    },
 
    "CheckInventory": {
      "Type": "Task",
      "Resource": "arn:aws:states:::dynamodb:getItem",
      "Parameters": {
        "TableName": "Inventory",
        "Key": {
          "ProductId": {
            "S.$": "$.productId"
          }
        }
      },
      "Next": "IsInStock"
    },
 
    "IsInStock": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.Item.Stock.N",
          "NumericGreaterThan": 0,
          "Next": "ProcessPayment"
        }
      ],
      "Default": "OutOfStock"
    },
 
    "ProcessPayment": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-1:123456789012:function:ProcessPayment",
      "Next": "SendNotifications"
    },
 
    "SendNotifications": {
      "Type": "Parallel",
      "Branches": [
        {
          "StartAt": "SendEmail",
          "States": {
            "SendEmail": {
              "Type": "Task",
              "Resource": "arn:aws:states:::sns:publish",
              "Parameters": {
                "TopicArn": "arn:aws:sns:us-east-1:123456789012:OrderEmail",
                "Message.$": "$.orderId"
              },
              "End": true
            }
          }
        },
        {
          "StartAt": "UpdateDatabase",
          "States": {
            "UpdateDatabase": {
              "Type": "Task",
              "Resource": "arn:aws:states:::dynamodb:putItem",
              "Parameters": {
                "TableName": "Orders",
                "Item": {
                  "OrderId": { "S.$": "$.orderId" },
                  "Status": { "S": "COMPLETED" }
                }
              },
              "End": true
            }
          }
        }
      ],
      "Next": "OrderSucceeded"
    },
 
    "OutOfStock": {
      "Type": "Task",
      "Resource": "arn:aws:lambda:us-east-1:123456789012:function:NotifyOutOfStock",
      "Next": "OrderFailed"
    },
 
    "OrderSucceeded": {
      "Type": "Succeed"
    },
 
    "OrderFailed": {
      "Type": "Fail",
      "Error": "OrderProcessingError",
      "Cause": "Order could not be processed"
    }
  }
}

Map State - Xử lý hàng loạt

{
  "ProcessAllFiles": {
    "Type": "Map",
    "ItemsPath": "$.files",
    "MaxConcurrency": 10,
    "ItemProcessor": {
      "ProcessorConfig": {
        "Mode": "INLINE"
      },
      "StartAt": "ProcessFile",
      "States": {
        "ProcessFile": {
          "Type": "Task",
          "Resource": "arn:aws:lambda:us-east-1:123456789012:function:ProcessFile",
          "End": true
        }
      }
    },
    "Next": "Done"
  }
}

Distributed Map - Xử lý large-scale (Standard Workflow only)

{
  "ProcessLargeDataset": {
    "Type": "Map",
    "ItemProcessor": {
      "ProcessorConfig": {
        "Mode": "DISTRIBUTED",
        "ExecutionType": "EXPRESS"
      },
      "StartAt": "ProcessItem",
      "States": {
        "ProcessItem": {
          "Type": "Task",
          "Resource": "arn:aws:lambda:us-east-1:123456789012:function:ProcessItem",
          "End": true
        }
      }
    },
    "ItemReader": {
      "Resource": "arn:aws:states:::s3:getObject",
      "ReaderConfig": {
        "InputType": "CSV",
        "CSVHeaderLocation": "FIRST_ROW"
      },
      "Parameters": {
        "Bucket": "my-bucket",
        "Key": "data.csv"
      }
    },
    "MaxConcurrency": 1000,
    "Next": "Done"
  }
}

Service Integrations

Step Functions tích hợp với 200+ AWS services qua 9,000+ API actions.

Integration Types

┌─────────────────────────────────────────────────────────────────────┐
│                    SERVICE INTEGRATION PATTERNS                     │
│                                                                     │
│   1️⃣  REQUEST RESPONSE (default)                                    │
│   ┌─────────┐     ┌──────────┐     ┌────────┐                       │
│   │  Step   │────▶│  AWS     │────▶│ Next   │   Gọi service,        │
│   │Functions│     │ Service  │     │ State  │   nhận HTTP response, │
│   └─────────┘     └──────────┘     └────────┘ chuyển ngay next state│
│                                                                     │
│   2️⃣  RUN A JOB (.sync) - Standard only                             │
│   ┌─────────┐     ┌──────────┐                  ┌────────┐          │
│   │  Step   │────▶│  AWS     │  ⏳ đợi job      │ Next   │          │
│   │Functions│     │ Service  │  hoàn thành ────▶│ State  │          │
│   └─────────┘     └──────────┘                  └────────┘          │
│                                                                     │
│   3️⃣  WAIT FOR CALLBACK (.waitForTaskToken) - Standard only         │
│   ┌────────┐     ┌──────────┐                  ┌────────┐           │
│   │  Step  │────▶│  SQS/    │  ⏳ đợi external │ Next   │           │
│   │Functions│     │ Lambda   │  process gọi     │ State │           │
│   │ (pause)│     │ + token  │  SendTaskSuccess └────────┘           │
│   └────────┘     └──────────┘                                       │
└─────────────────────────────────────────────────────────────────────┘

Optimized Integrations (các service phổ biến)

AWS ServiceRequest ResponseRun a Job (.sync)Wait for Callback
LambdaStandard & ExpressStandard
DynamoDBStandard & Express
ECS/FargateStandard & ExpressStandardStandard
EKSStandard & ExpressStandardStandard
SNSStandard & ExpressStandard
SQSStandard & ExpressStandard
AWS BatchStandard & ExpressStandard
GlueStandard & ExpressStandard
SageMakerStandard & ExpressStandard
AthenaStandard & ExpressStandard
BedrockStandard & ExpressStandardStandard
CodeBuildStandard & ExpressStandard
API GatewayStandard & ExpressStandard
EventBridgeStandard & ExpressStandard
Step FunctionsStandard & ExpressStandardStandard

HTTP Task

Step Functions hỗ trợ gọi HTTPS APIs (public hoặc private) trực tiếp từ workflow mà không cần Lambda.

{
  "CallExternalAPI": {
    "Type": "Task",
    "Resource": "arn:aws:states:::http:invoke",
    "Parameters": {
      "ApiEndpoint": "https://api.example.com/orders",
      "Method": "POST",
      "Headers": {
        "Content-Type": "application/json"
      },
      "RequestBody": {
        "orderId.$": "$.orderId"
      },
      "Authentication": {
        "ConnectionArn": "arn:aws:events:us-east-1:123456789012:connection/MyConnection"
      }
    },
    "Next": "ProcessResponse"
  }
}

Nguồn: Integrating services with Step Functions


Error Handling

Step Functions cung cấp built-in error handling với RetryCatch.

Retry

{
  "ProcessOrder": {
    "Type": "Task",
    "Resource": "arn:aws:lambda:us-east-1:123456789012:function:Process",
    "Retry": [
      {
        "ErrorEquals": ["States.TaskFailed"],
        "IntervalSeconds": 3,
        "MaxAttempts": 5,
        "BackoffRate": 2.0,
        "MaxDelaySeconds": 60,
        "JitterStrategy": "FULL"
      },
      {
        "ErrorEquals": ["States.ALL"],
        "IntervalSeconds": 1,
        "MaxAttempts": 2
      }
    ],
    "Next": "Done"
  }
}
FieldMô tảDefault
ErrorEqualsDanh sách error names để match(bắt buộc)
IntervalSecondsThời gian chờ trước retry đầu tiên1
MaxAttemptsSố lần retry tối đa3
BackoffRateMultiplier cho mỗi lần retry2.0
MaxDelaySecondsGiới hạn delay tối đaKhông giới hạn
JitterStrategyThêm random jitter (FULL hoặc NONE)NONE

Catch (Fallback)

{
  "ProcessOrder": {
    "Type": "Task",
    "Resource": "arn:aws:lambda:us-east-1:123456789012:function:Process",
    "Catch": [
      {
        "ErrorEquals": ["CustomValidationError"],
        "Next": "HandleValidationError",
        "ResultPath": "$.error"
      },
      {
        "ErrorEquals": ["States.ALL"],
        "Next": "HandleGenericError",
        "ResultPath": "$.error"
      }
    ],
    "Next": "Done"
  }
}

Built-in Error Types

ErrorMô tả
States.ALLMatch tất cả errors
States.HeartbeatTimeoutTask không gửi heartbeat kịp thời
States.TimeoutTask chạy quá thời gian cho phép
States.TaskFailedTask execution failed
States.PermissionsInsufficient privileges
States.ResultPathMatchFailureResultPath không match được với input
States.IntrinsicFailureIntrinsic function failed

Security

IAM Execution Role

Step Functions cần một IAM execution role để gọi các AWS services.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "lambda:InvokeFunction"
      ],
      "Resource": "arn:aws:lambda:us-east-1:123456789012:function:*"
    },
    {
      "Effect": "Allow",
      "Action": [
        "dynamodb:PutItem",
        "dynamodb:GetItem"
      ],
      "Resource": "arn:aws:dynamodb:us-east-1:123456789012:table/Orders"
    },
    {
      "Effect": "Allow",
      "Action": [
        "sns:Publish"
      ],
      "Resource": "arn:aws:sns:us-east-1:123456789012:OrderNotifications"
    }
  ]
}

Security Best Practices

PracticeMô tả
Least privilegeChỉ cấp quyền cần thiết cho execution role
Resource-level permissionsSpecify exact ARNs thay vì *
EncryptionData at rest được mã hóa bằng AWS-owned key (default) hoặc customer managed KMS key
VPCDùng VPC endpoints để truy cập Step Functions từ private subnets
CloudTrailLogging tất cả API calls cho audit
TaggingTag state machines cho cost allocation và access control

Pricing

Standard Workflows

Hạng mụcChi phí (US East - N. Virginia)
Free Tier4,000 state transitions/tháng (vĩnh viễn, không hết hạn sau 12 tháng)
Per state transition$0.000025

Ví dụ tính giá:

Workflow có 4 states, chạy 100,000 lần/tháng:

  State transitions = 4 × 100,000 = 400,000
  Billable = 400,000 - 4,000 (Free Tier) = 396,000
  Chi phí = 396,000 × $0.000025 = $9.90/tháng

Express Workflows

Hạng mụcChi phí
Requests$1.00 per 1 triệu requests
DurationTính theo GB-giây (giá thay đổi theo memory tier)

Ví dụ tính giá:

1 triệu executions/tháng, mỗi execution 3 giây, dùng 64MB memory:

  Request charges = 1,000,000 × $0.000001 = $1.00
  Duration = 1,000,000 × 3s × (64MB/1024MB) = 187,500 GB-giây
  Duration charges = 187,500 × $0.00001667 ≈ $3.13
  Tổng ≈ $4.13/tháng

Lưu ý: Retry do error cũng tính thêm state transitions (Standard) hoặc thêm duration (Express).

Nguồn: AWS Step Functions Pricing


Quotas và Limits

Account-level Quotas

ResourceDefaultAdjustable
Max registered state machines100,000✅ (lên 150,000)
Max registered activities100,000✅ (lên 150,000)
Max state machine definition size1 MB❌ (hard limit)
Max open executions per account1,000,000✅ (hàng triệu)
Max open Map Runs1,000❌ (hard limit)
Max parallel Map Run child executions10,000❌ (hard limit)

Execution Quotas (Hard Limits)

ResourceLimit
Max execution history size25,000 events
Max execution time (Standard)1 năm (365 ngày)
Max execution time (Express)5 phút
Max input/output data size per state256 KB
Execution history retention (Standard)90 ngày
State machine name length80 ký tự

Throttling Quotas (Soft Limits)

MetricStandardExpress
StateTransition800 bucket / 400 refill/s (default)Không giới hạn
StartExecution1,300 bucket / 300 refill/s6,000 bucket / 6,000 refill/s

Nguồn: Step Functions service quotas


So sánh với các Orchestration Tools khác

┌──────────────────┬──────────────────┬──────────────────┬──────────────────┐
│                  │  Step Functions   │  EventBridge      │  SQS + Lambda  │
│                  │                  │  (Event-driven)    │  (Queue-based) │
├──────────────────┼──────────────────┼──────────────────┼──────────────────┤
│ Pattern          │ Orchestration     │ Choreography      │ Point-to-point │
│                  │ (central control) │ (event routing)   │ (decoupled)    │
├──────────────────┼──────────────────┼──────────────────┼──────────────────┤
│ State            │ ✅ Managed by    │ ❌ Stateless       │ ❌ Stateless   │
│ Management       │ Step Functions    │                 │                  │
├──────────────────┼──────────────────┼──────────────────┼──────────────────┤
│ Visual           │ ✅ Workflow      │ ❌                 │ ❌             │
│ Debugging        │ Studio           │                  │                  │
├──────────────────┼──────────────────┼──────────────────┼──────────────────┤
│ Error Handling   │ ✅ Built-in      │ ⚠️ DLQ + retry     │ ⚠️ DLQ + retry │
│                  │ Retry/Catch      │                  │                  │
├──────────────────┼──────────────────┼──────────────────┼──────────────────┤
│ Human Approval   │ ✅ Callback      │ ❌                 │ ❌             │
│                  │ pattern          │                  │                  │
├──────────────────┼──────────────────┼──────────────────┼──────────────────┤
│ Best for         │ Complex,         │ Event routing,     │ Simple async   │
│                  │ multi-step       │ many consumers     │ processing     │
│                  │ workflows        │                  │                  │
└──────────────────┴──────────────────┴──────────────────┴──────────────────┘

Step Functions vs Amazon MWAA (Managed Airflow)

Tiêu chíStep FunctionsMWAA
InfrastructureServerless, no provisioningCần provision, maintenance windows
AuthoringASL (JSON), Workflow StudioPython DAGs
Cost khi idle$0 (pay-per-use)Vẫn tốn tiền (environment luôn chạy)
Max duration1 nămKhông giới hạn
AWS IntegrationNative (200+ services)Qua Airflow operators/hooks
Best forEvent-driven, serverless workflowsData engineering, ETL pipelines phức tạp

Best Practices

Design

  1. Chọn đúng workflow type

    • Standard cho long-running, non-idempotent, cần audit
    • Express cho high-volume, short-duration, idempotent
  2. Nest Express trong Standard để tối ưu chi phí

    • Standard workflow gọi Express sub-workflow cho high-throughput steps
  3. Dùng Parallel state thay vì sequential khi các tasks độc lập

  4. Distributed Map cho large-scale data processing (thay vì inline Map)

  5. Tránh polling loops - dùng .sync hoặc .waitForTaskToken thay vì loop + Wait

Error Handling

  1. Luôn có Retry với exponential backoff cho transient errors
  2. Luôn có Catch cho mỗi Task state quan trọng
  3. Dùng States.ALL làm catch-all cuối cùng
  4. Set TimeoutSeconds cho mỗi Task để tránh stuck executions

Cost Optimization

  1. Express workflow cho high-volume workloads (rẻ hơn Standard nhiều)
  2. Giảm state transitions - combine logic khi có thể
  3. Dùng SDK integrations trực tiếp thay vì wrap trong Lambda (giảm Lambda cost)
  4. Tránh retry không cần thiết - mỗi retry = thêm state transitions

Monitoring

CloudWatch Metrics quan trọng:
├── ExecutionsStarted      - Số executions bắt đầu
├── ExecutionsSucceeded    - Số executions thành công
├── ExecutionsFailed       - Số executions thất bại
├── ExecutionsTimedOut     - Số executions timeout
├── ExecutionThrottled     - Số executions bị throttle
└── ExecutionTime          - Thời gian chạy trung bình

Hands-on Examples

1. CDK - Order Processing Workflow

import * as cdk from 'aws-cdk-lib';
import * as sfn from 'aws-cdk-lib/aws-stepfunctions';
import * as tasks from 'aws-cdk-lib/aws-stepfunctions-tasks';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
 
export class StepFunctionsStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
 
    // Lambda functions
    const validateFn = new lambda.Function(this, 'ValidateFn', {
      runtime: lambda.Runtime.NODEJS_18_X,
      handler: 'index.handler',
      code: lambda.Code.fromInline(`
        exports.handler = async (event) => {
          if (!event.orderId) throw new Error('Missing orderId');
          return { ...event, validated: true };
        };
      `),
    });
 
    const processFn = new lambda.Function(this, 'ProcessFn', {
      runtime: lambda.Runtime.NODEJS_18_X,
      handler: 'index.handler',
      code: lambda.Code.fromInline(`
        exports.handler = async (event) => {
          return { ...event, status: 'PROCESSED' };
        };
      `),
    });
 
    // DynamoDB table
    const ordersTable = new dynamodb.Table(this, 'OrdersTable', {
      partitionKey: { name: 'orderId', type: dynamodb.AttributeType.STRING },
    });
 
    // Step Functions tasks
    const validateOrder = new tasks.LambdaInvoke(this, 'Validate Order', {
      lambdaFunction: validateFn,
      outputPath: '$.Payload',
      retryOnServiceExceptions: true,
    });
 
    const checkAmount = new sfn.Choice(this, 'Check Order Amount')
      .when(
        sfn.Condition.numberGreaterThan('$.amount', 1000),
        new sfn.Pass(this, 'Require Approval', {
          result: sfn.Result.fromObject({ approved: false }),
          resultPath: '$.approval',
        })
      )
      .otherwise(
        new sfn.Pass(this, 'Auto Approve', {
          result: sfn.Result.fromObject({ approved: true }),
          resultPath: '$.approval',
        })
      );
 
    const processOrder = new tasks.LambdaInvoke(this, 'Process Order', {
      lambdaFunction: processFn,
      outputPath: '$.Payload',
    });
 
    const saveOrder = new tasks.DynamoPutItem(this, 'Save Order', {
      table: ordersTable,
      item: {
        orderId: tasks.DynamoAttributeValue.fromString(
          sfn.JsonPath.stringAt('$.orderId')
        ),
        status: tasks.DynamoAttributeValue.fromString('COMPLETED'),
      },
    });
 
    const orderSucceeded = new sfn.Succeed(this, 'Order Succeeded');
    const orderFailed = new sfn.Fail(this, 'Order Failed', {
      error: 'OrderProcessingError',
      cause: 'Order could not be processed',
    });
 
    // Define workflow
    const definition = validateOrder
      .addCatch(orderFailed)
      .next(checkAmount)
      .afterwards()
      .next(processOrder)
      .addCatch(orderFailed)
      .next(saveOrder)
      .addCatch(orderFailed)
      .next(orderSucceeded);
 
    // Create state machine
    new sfn.StateMachine(this, 'OrderStateMachine', {
      definitionBody: sfn.DefinitionBody.fromChainable(definition),
      stateMachineType: sfn.StateMachineType.STANDARD,
      timeout: cdk.Duration.minutes(30),
      tracingEnabled: true,
      logs: {
        destination: new cdk.aws_logs.LogGroup(this, 'SfnLogs'),
        level: sfn.LogLevel.ALL,
      },
    });
  }
}

2. SAM Template - Express Workflow

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
 
Resources:
  ProcessingStateMachine:
    Type: AWS::Serverless::StateMachine
    Properties:
      DefinitionUri: statemachine/processing.asl.json
      Type: EXPRESS
      Logging:
        Destinations:
          - CloudWatchLogsLogGroup:
              LogGroupArn: !GetAtt StateMachineLogGroup.Arn
        IncludeExecutionData: true
        Level: ALL
      Policies:
        - LambdaInvokePolicy:
            FunctionName: !Ref ProcessFunction
        - DynamoDBCrudPolicy:
            TableName: !Ref DataTable
      Events:
        ApiEvent:
          Type: Api
          Properties:
            Path: /process
            Method: post
 
  ProcessFunction:
    Type: AWS::Serverless::Function
    Properties:
      Runtime: python3.12
      Handler: app.handler
      CodeUri: src/
 
  DataTable:
    Type: AWS::DynamoDB::Table
    Properties:
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      BillingMode: PAY_PER_REQUEST
 
  StateMachineLogGroup:
    Type: AWS::Logs::LogGroup
    Properties:
      RetentionInDays: 14

3. Callback Pattern (Human Approval)

{
  "Comment": "Human Approval Workflow",
  "StartAt": "SendApprovalRequest",
  "States": {
    "SendApprovalRequest": {
      "Type": "Task",
      "Resource": "arn:aws:states:::sqs:sendMessage.waitForTaskToken",
      "Parameters": {
        "QueueUrl": "https://sqs.us-east-1.amazonaws.com/123456789012/ApprovalQueue",
        "MessageBody": {
          "TaskToken.$": "$$.Task.Token",
          "RequestDetails.$": "$.request"
        }
      },
      "TimeoutSeconds": 86400,
      "Next": "CheckApproval",
      "Catch": [
        {
          "ErrorEquals": ["States.Timeout"],
          "Next": "ApprovalTimeout"
        }
      ]
    },
    "CheckApproval": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.approved",
          "BooleanEquals": true,
          "Next": "Approved"
        }
      ],
      "Default": "Rejected"
    },
    "Approved": { "Type": "Succeed" },
    "Rejected": {
      "Type": "Fail",
      "Error": "RequestRejected",
      "Cause": "The request was rejected by approver"
    },
    "ApprovalTimeout": {
      "Type": "Fail",
      "Error": "ApprovalTimeout",
      "Cause": "No approval received within 24 hours"
    }
  }
}

Liên kết


Key Takeaways

  1. Step Functions = Serverless Workflow Orchestration - Kết hợp AWS services thành workflows có visual editor
  2. 2 Workflow Types - Standard (long-running, exactly-once, 1 năm) vs Express (high-volume, 5 phút, at-least-once)
  3. 8 State Types - Task, Choice, Parallel, Map, Wait, Pass, Succeed, Fail
  4. 200+ AWS Integrations - Gọi trực tiếp AWS services không cần Lambda wrapper
  5. 3 Integration Patterns - Request Response, Run a Job (.sync), Wait for Callback
  6. Built-in Error Handling - Retry với exponential backoff + Catch fallback
  7. Distributed Map - Xử lý large-scale data song song (lên tới 10,000 child executions)
  8. Free Tier vĩnh viễn - 4,000 Standard state transitions/tháng (không hết hạn sau 12 tháng)
  9. Dùng Step Functions khi: Cần orchestrate nhiều services, complex workflows, human approval, error handling
  10. Dùng EventBridge khi: Event routing đơn giản, choreography pattern, nhiều consumers