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.
Khi nào dùng Step Functions?
| Use Case | Ví dụ |
|---|---|
| Microservice Orchestration | Gọi nhiều Lambda/ECS services theo thứ tự |
| Data Processing / ETL | Xử lý dữ liệu qua nhiều bước: extract → transform → load |
| ML Pipeline | Train model → evaluate → deploy → monitor |
| Human Approval Workflows | Order processing với bước approve thủ công |
| IT Automation | Security incident response, auto-remediation |
| Media Processing | Transcode video → generate thumbnails → publish |
Kiến trúc và Core Concepts
Core Concepts
| Concept | Mô tả |
|---|---|
| State Machine | Workflow definition (JSON/YAML) chứa các states và transitions |
| State | Một bước trong workflow (Task, Choice, Parallel, Map, Wait, Pass, Succeed, Fail) |
| Execution | Một lần chạy (instance) của state machine |
| Transition | Di chuyển từ state này sang state tiếp theo |
| Amazon States Language (ASL) | JSON-based language để định nghĩa state machines |
| Workflow Studio | Visual editor (drag-and-drop) trong AWS Console |
| Activity | Worker bên ngoài Step Functions (long-running tasks) |
| Task Token | Token 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.
So sánh chi tiết
| Tiêu chí | Standard | Express |
|---|---|---|
| Max duration | 1 năm | 5 phút |
| Execution rate | 2,000/giây | 100,000/giây |
| State transition rate | 4,000/giây (soft limit) | Gần như không giới hạn |
| Execution semantics | Exactly-once | At-least-once (async) / At-most-once (sync) |
| Pricing | Per state transition ($0.000025/transition) | Per execution + duration + memory |
| Execution history | Console + API (90 ngày) | CloudWatch Logs |
| Run a Job (.sync) | ✅ | ❌ |
| Wait for Callback | ✅ | ❌ |
| Distributed Map | ✅ | ❌ |
| Activities | ✅ | ❌ |
Express Workflow: Synchronous vs Asynchronous
| Loại | Mô tả | Execution semantics | Trigger |
|---|---|---|---|
| Async Express | Start workflow, KHÔNG đợi kết quả | At-least-once | EventBridge, nested workflow, StartExecution API |
| Sync Express | Start workflow, ĐỢI kết quả trả về | At-most-once | API 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).
Chi tiết từng State
| State | Type | Mô tả | Ví dụ |
|---|---|---|---|
| Task | Action | Gọi AWS service hoặc activity | Invoke Lambda, PutItem DynamoDB |
| Choice | Flow | Rẽ nhánh theo điều kiện | If order > $100 → approve, else → reject |
| Parallel | Flow | Chạy nhiều branches đồng thời | Gửi email + SMS + push notification cùng lúc |
| Map | Flow | Lặp qua array items | Xử lý từng file trong S3 bucket |
| Wait | Flow | Pause execution | Chờ 30 giây, chờ đến ngày cụ thể |
| Pass | Flow | Pass-through, transform data | Inject default values, reshape data |
| Succeed | Flow | Terminal state - thành công | Kết thúc workflow |
| Fail | Flow | Terminal state - thất bại | Trả 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
Map State - Xử lý hàng loạt
Distributed Map - Xử lý large-scale (Standard Workflow only)
Service Integrations
Step Functions tích hợp với 200+ AWS services qua 9,000+ API actions.
Integration Types
Optimized Integrations (các service phổ biến)
| AWS Service | Request Response | Run a Job (.sync) | Wait for Callback |
|---|---|---|---|
| Lambda | Standard & Express | ❌ | Standard |
| DynamoDB | Standard & Express | ❌ | ❌ |
| ECS/Fargate | Standard & Express | Standard | Standard |
| EKS | Standard & Express | Standard | Standard |
| SNS | Standard & Express | ❌ | Standard |
| SQS | Standard & Express | ❌ | Standard |
| AWS Batch | Standard & Express | Standard | ❌ |
| Glue | Standard & Express | Standard | ❌ |
| SageMaker | Standard & Express | Standard | ❌ |
| Athena | Standard & Express | Standard | ❌ |
| Bedrock | Standard & Express | Standard | Standard |
| CodeBuild | Standard & Express | Standard | ❌ |
| API Gateway | Standard & Express | ❌ | Standard |
| EventBridge | Standard & Express | ❌ | Standard |
| Step Functions | Standard & Express | Standard | Standard |
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.
Error Handling
Step Functions cung cấp built-in error handling với Retry và Catch.
Retry
| Field | Mô tả | Default |
|---|---|---|
| ErrorEquals | Danh sách error names để match | (bắt buộc) |
| IntervalSeconds | Thời gian chờ trước retry đầu tiên | 1 |
| MaxAttempts | Số lần retry tối đa | 3 |
| BackoffRate | Multiplier cho mỗi lần retry | 2.0 |
| MaxDelaySeconds | Giới hạn delay tối đa | Không giới hạn |
| JitterStrategy | Thêm random jitter (FULL hoặc NONE) | NONE |
Catch (Fallback)
Built-in Error Types
| Error | Mô tả |
|---|---|
States.ALL | Match tất cả errors |
States.HeartbeatTimeout | Task không gửi heartbeat kịp thời |
States.Timeout | Task chạy quá thời gian cho phép |
States.TaskFailed | Task execution failed |
States.Permissions | Insufficient privileges |
States.ResultPathMatchFailure | ResultPath không match được với input |
States.IntrinsicFailure | Intrinsic function failed |
Security
IAM Execution Role
Step Functions cần một IAM execution role để gọi các AWS services.
Security Best Practices
| Practice | Mô tả |
|---|---|
| Least privilege | Chỉ cấp quyền cần thiết cho execution role |
| Resource-level permissions | Specify exact ARNs thay vì * |
| Encryption | Data at rest được mã hóa bằng AWS-owned key (default) hoặc customer managed KMS key |
| VPC | Dùng VPC endpoints để truy cập Step Functions từ private subnets |
| CloudTrail | Logging tất cả API calls cho audit |
| Tagging | Tag state machines cho cost allocation và access control |
Pricing
Standard Workflows
| Hạng mục | Chi phí (US East - N. Virginia) |
|---|---|
| Free Tier | 4,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á:
Express Workflows
| Hạng mục | Chi phí |
|---|---|
| Requests | $1.00 per 1 triệu requests |
| Duration | Tính theo GB-giây (giá thay đổi theo memory tier) |
Ví dụ tính giá:
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
| Resource | Default | Adjustable |
|---|---|---|
| Max registered state machines | 100,000 | ✅ (lên 150,000) |
| Max registered activities | 100,000 | ✅ (lên 150,000) |
| Max state machine definition size | 1 MB | ❌ (hard limit) |
| Max open executions per account | 1,000,000 | ✅ (hàng triệu) |
| Max open Map Runs | 1,000 | ❌ (hard limit) |
| Max parallel Map Run child executions | 10,000 | ❌ (hard limit) |
Execution Quotas (Hard Limits)
| Resource | Limit |
|---|---|
| Max execution history size | 25,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 state | 256 KB |
| Execution history retention (Standard) | 90 ngày |
| State machine name length | 80 ký tự |
Throttling Quotas (Soft Limits)
| Metric | Standard | Express |
|---|---|---|
| StateTransition | 800 bucket / 400 refill/s (default) | Không giới hạn |
| StartExecution | 1,300 bucket / 300 refill/s | 6,000 bucket / 6,000 refill/s |
So sánh với các Orchestration Tools khác
Step Functions vs Amazon MWAA (Managed Airflow)
| Tiêu chí | Step Functions | MWAA |
|---|---|---|
| Infrastructure | Serverless, no provisioning | Cần provision, maintenance windows |
| Authoring | ASL (JSON), Workflow Studio | Python DAGs |
| Cost khi idle | $0 (pay-per-use) | Vẫn tốn tiền (environment luôn chạy) |
| Max duration | 1 năm | Không giới hạn |
| AWS Integration | Native (200+ services) | Qua Airflow operators/hooks |
| Best for | Event-driven, serverless workflows | Data engineering, ETL pipelines phức tạp |
Best Practices
Design
-
Chọn đúng workflow type
- Standard cho long-running, non-idempotent, cần audit
- Express cho high-volume, short-duration, idempotent
-
Nest Express trong Standard để tối ưu chi phí
- Standard workflow gọi Express sub-workflow cho high-throughput steps
-
Dùng Parallel state thay vì sequential khi các tasks độc lập
-
Distributed Map cho large-scale data processing (thay vì inline Map)
-
Tránh polling loops - dùng
.synchoặc.waitForTaskTokenthay vì loop + Wait
Error Handling
- Luôn có Retry với exponential backoff cho transient errors
- Luôn có Catch cho mỗi Task state quan trọng
- Dùng
States.ALLlàm catch-all cuối cùng - Set TimeoutSeconds cho mỗi Task để tránh stuck executions
Cost Optimization
- Express workflow cho high-volume workloads (rẻ hơn Standard nhiều)
- Giảm state transitions - combine logic khi có thể
- Dùng SDK integrations trực tiếp thay vì wrap trong Lambda (giảm Lambda cost)
- Tránh retry không cần thiết - mỗi retry = thêm state transitions
Monitoring
Hands-on Examples
1. CDK - Order Processing Workflow
2. SAM Template - Express Workflow
3. Callback Pattern (Human Approval)
Liên kết
- AWS Step Functions - What is Step Functions?
- AWS Step Functions Developer Guide
- AWS Step Functions API Reference
- AWS Step Functions Pricing
- AWS Step Functions Features
- AWS Step Functions Use Cases
- Step Functions Workflow Studio
- The Step Functions Workshop
Key Takeaways
- Step Functions = Serverless Workflow Orchestration - Kết hợp AWS services thành workflows có visual editor
- 2 Workflow Types - Standard (long-running, exactly-once, 1 năm) vs Express (high-volume, 5 phút, at-least-once)
- 8 State Types - Task, Choice, Parallel, Map, Wait, Pass, Succeed, Fail
- 200+ AWS Integrations - Gọi trực tiếp AWS services không cần Lambda wrapper
- 3 Integration Patterns - Request Response, Run a Job (.sync), Wait for Callback
- Built-in Error Handling - Retry với exponential backoff + Catch fallback
- Distributed Map - Xử lý large-scale data song song (lên tới 10,000 child executions)
- Free Tier vĩnh viễn - 4,000 Standard state transitions/tháng (không hết hạn sau 12 tháng)
- Dùng Step Functions khi: Cần orchestrate nhiều services, complex workflows, human approval, error handling
- Dùng EventBridge khi: Event routing đơn giản, choreography pattern, nhiều consumers