aws-step-functions
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAWS Step Functions
AWS Step Functions
Overview
概述
AWS Step Functions uses Amazon States Language (ASL) to define state machines as JSON. With AWS Step Functions, you can create workflows, also called state machines, to build distributed applications, automate processes, orchestrate microservices, and create data and machine learning pipelines.
This skill provides comprehensive guidance for writing state machines in ASL, covering:
- ASL structure and JSONata expression syntax
- Details on the eight available workflow states
- The reserved variable
$states - Workflow variables with
Assign - Error handling
- AWS Service integration patterns
- Example code for data transformation and architecture
- Validation and testing of state machines
- How to migrate from JSONPath to JSONata
The AWS MCP server is recommended for sandboxed execution and audit logging when following this skill, but all steps use AWS CLI syntax and work without it.
AWS Step Functions 使用 Amazon States Language (ASL) 以JSON格式定义状态机。借助AWS Step Functions,您可以创建工作流(也称为状态机),以构建分布式应用、自动化流程、编排微服务以及创建数据与机器学习管道。
本技能提供了编写ASL状态机的全面指导,涵盖:
- ASL结构与JSONata表达式语法
- 八种可用工作流状态的详细说明
- 保留变量
$states - 使用定义工作流变量
Assign - 错误处理
- AWS服务集成模式
- 数据转换与架构的示例代码
- 状态机的验证与测试
- 如何从JSONPath迁移至JSONata
遵循本技能操作时,推荐使用AWS MCP服务器进行沙箱执行与审计日志记录,但所有步骤均使用AWS CLI语法,无需MCP服务器也可运行。
When to Load Reference Files
何时加载参考文件
Load the appropriate reference file based on what the user is working on:
- ASL structure, state types, Task, Pass, Choice, Wait, Succeed, Fail, Parallel, Map → see
references/asl-state-types.md - Error handling, troubleshooting, Retry, Catch, fallback, error codes, States.Timeout, States.ALL → see
references/error-handling.md - Service integrations, Lambda invoke, DynamoDB, SNS, SQS, SDK integrations, Resource ARN, sync, async → see
references/service-integrations.md - Migrating from JSONPath to JSONata, migration, JSONPath to JSONata, InputPath, Parameters, ResultSelector, ResultPath, OutputPath, intrinsic functions, Iterator, payload template → see
references/migrating-from-jsonpath-to-jsonata.md - Validation, linting, testing, TestState, test state, mock, mocking, unit test, inspection level, DEBUG, TRACE, validate state, test in isolation → see
references/validation-and-testing.md - Architecture patterns, examples, polling, saga, compensation, scatter-gather, semaphore, lock, human-in-the-loop, escalation, Express to Standard → see
references/architecture-patterns.md - Data transformation, JSONata expressions, filtering, aggregation, string operations, $reduce, $lookup, $toMillis, $partition, $parse, $hash, $uuid → see
references/transforming-data.md - State input/output, $states, Assign, Output, Arguments, variable scope, variable limits, evaluation order, passing data between states → see
references/processing-state-inputs-and-outputs.md
根据用户的工作内容加载相应的参考文件:
- ASL结构、状态类型、Task、Pass、Choice、Wait、Succeed、Fail、Parallel、Map → 查看
references/asl-state-types.md - 错误处理、故障排查、Retry、Catch、回退、错误代码、States.Timeout、States.ALL → 查看
references/error-handling.md - 服务集成、Lambda调用、DynamoDB、SNS、SQS、SDK集成、资源ARN、sync、async → 查看
references/service-integrations.md - 从JSONPath迁移至JSONata、迁移、JSONPath转JSONata、InputPath、Parameters、ResultSelector、ResultPath、OutputPath、内置函数、Iterator、负载模板 → 查看
references/migrating-from-jsonpath-to-jsonata.md - 验证、代码检查、测试、TestState、测试状态、模拟、单元测试、检查级别、DEBUG、TRACE、验证状态、隔离测试 → 查看
references/validation-and-testing.md - 架构模式、示例、轮询、saga、补偿、分散-聚合、信号量、锁、人工介入、升级、Express转Standard → 查看
references/architecture-patterns.md - 数据转换、JSONata表达式、过滤、聚合、字符串操作、$reduce、$lookup、$toMillis、$partition、$parse、$hash、$uuid → 查看
references/transforming-data.md - 状态输入/输出、$states、Assign、Output、Arguments、变量作用域、变量限制、求值顺序、状态间数据传递 → 查看
references/processing-state-inputs-and-outputs.md
Quick Reference
快速参考
Standard vs Express Workflows
Standard与Express工作流对比
| Standard | Express | |
|---|---|---|
| Max duration | 1 year | 5 minutes |
| Execution semantics | Exactly-once | At-least-once (async) / At-most-once (sync) |
| Execution history | Retained 90 days, queryable via API | CloudWatch Logs only |
| Max throughput | 2,000 exec/sec | 100,000 exec/sec |
| Pricing model | Per state transition | Per execution count + duration |
| Supported | Not supported |
| Best for | Auditable, non-idempotent operations | High-volume, idempotent event processing |
Choose Standard for: payment processing, order fulfillment, compliance workflows, anything that must never execute twice.
Choose Express for: IoT data ingestion, streaming transformations, mobile backends, high-throughput short-lived processing.
When recommending Express, the single limitation you must always state — even for fire-and-forget / high-throughput pipelines — is that Express does NOT supportor.sync(no callbacks, no nested.waitForTaskTokenwaits, no human-approval or job-completion waits). Also note: 5-minute max duration, no queryable execution history (CloudWatch Logs only), and at-least-once (async) / at-most-once (sync) execution — so non-idempotent work can run twice. If any of these matter, choose Standard (exactly-once, up to 1 year, full history)..sync
| 特性 | Standard | Express |
|---|---|---|
| 最长持续时间 | 1年 | 5分钟 |
| 执行语义 | 恰好一次 | 至少一次(异步)/至多一次(同步) |
| 执行历史 | 保留90天,可通过API查询 | 仅存储于CloudWatch Logs |
| 最大吞吐量 | 2000次执行/秒 | 100000次执行/秒 |
| 定价模型 | 按状态转换次数计费 | 按执行次数+持续时间计费 |
支持 | 是 | 否 |
| 适用场景 | 可审计、非幂等操作 | 高吞吐量、幂等事件处理 |
选择Standard的场景:支付处理、订单履行、合规工作流等任何绝对不能重复执行的操作。
选择Express的场景:IoT数据采集、流转换、移动后端、高吞吐量短生命周期处理。
推荐Express时必须说明的唯一限制——即便是用于“即发即弃”或高吞吐量管道——Express 不支持或.sync(无回调、无嵌套.waitForTaskToken等待、无人工审批或作业完成等待)。此外需注意:最长持续时间为5分钟,无可查询的执行历史(仅CloudWatch Logs),执行语义为至少一次(异步)/至多一次(同步)——因此非幂等工作可能会运行两次。如果上述任何限制对您的场景有影响,请选择Standard(恰好一次执行、最长1年、完整历史记录)。.sync
Setting the State Machine Query Language
设置状态机查询语言
JSONata is the preferred way to reference and transform data in ASL. It replaces the five JSONPath I/O fields (, , , , ) with just two: (inputs) and .
InputPathParametersResultSelectorResultPathOutputPathArgumentsOutputEnable at the top level to apply to all states:
json
{ "QueryLanguage": "JSONata", "StartAt": "...", "States": {...} }Or per-state to migrate from JSONPath incrementally:
json
{ "Type": "Task", "QueryLanguage": "JSONata", ... }JSONPath is supported and is the default if is omitted — existing state machines do not need to be migrated.
QueryLanguageField mapping (JSONPath → JSONata):
| JSONPath field | JSONata equivalent |
|---|---|
| |
| |
| |
| not needed — reference |
A state uses one query language, not both. Never mix JSONPath fields (/InputPath/Parameters/ResultSelector/ResultPath) with JSONata fields (OutputPath/Arguments) in the same state — this is the most common migration error. SeeOutputfor full details.references/migrating-from-jsonpath-to-jsonata.md
JSONata是ASL中引用和转换数据的首选方式。它将五个JSONPath输入输出字段(、、、、)替换为仅两个字段:(输入)和。
InputPathParametersResultSelectorResultPathOutputPathArgumentsOutput在顶层启用以应用于所有状态:
json
{ "QueryLanguage": "JSONata", "StartAt": "...", "States": {...} }或按状态启用以逐步从JSONPath迁移:
json
{ "Type": "Task", "QueryLanguage": "JSONata", ... }JSONPath受支持,若省略则默认使用JSONPath——现有状态机无需迁移。
QueryLanguage字段映射(JSONPath → JSONata):
| JSONPath字段 | JSONata等效字段 |
|---|---|
| |
| |
| |
| 无需使用——直接引用 |
一个状态仅使用一种查询语言,不可混用。 切勿在同一状态中同时使用JSONPath字段(/InputPath/Parameters/ResultSelector/ResultPath)与JSONata字段(OutputPath/Arguments)——这是最常见的迁移错误。详细信息请查看Output。references/migrating-from-jsonpath-to-jsonata.md
How Assign and Output Are Evaluated (Parallel, Not Sequential)
Assign与Output的求值方式(并行,而非顺序)
Within a single state, and are evaluated at the same time — in parallel — both reading the same data (the state input plus the task result). They are NOT evaluated one after the other. Because they run together, a variable you set in is not visible in that same state's : there is no ordering in which could observe the just-assigned value. The assigned value becomes available only to subsequent states.
AssignOutputAssignOutputOutputSo if you set a variable in and reference it in the same state's , you get the old/undefined value — not because runs "before" , but because both evaluate concurrently from the same snapshot. To use the value immediately, reference it in the next state (variables persist across states); to shape the current state's output from the task result, use directly in .
AssignOutputOutputAssign$states.resultOutput在单个状态内,和同时求值——并行执行——均读取相同的数据(状态输入加上任务结果)。它们并非按顺序求值。由于二者同时运行,您在中设置的变量不会在同一状态的中可见:不存在可以观测到刚赋值的变量的顺序。赋值后的变量仅对后续状态可用。
AssignOutputAssignOutputOutput因此,如果您在中设置变量并在同一状态的中引用它,您将得到旧值/未定义值——并非因为在之前运行,而是因为二者从同一快照并发求值。要立即使用该值,请在下一个状态中引用它(变量在状态间持久化);要根据任务结果调整当前状态的输出,请在中直接使用。
AssignOutputOutputAssignOutput$states.resultUnit Testing a State with TestState
使用TestState对单个状态进行单元测试
Test a single state without deploying the state machine or calling the real service using the TestState API () with . A complete answer covers all four points:
aws stepfunctions test-state--mock- Mock the service response exactly — the
--mockMUST match the target AWS service's API response schema exactly (field names are case-sensitive). For a LambdaresultTask that isinvokeandStatusCode:Payload.--mock '{"result":"{\"StatusCode\":200,\"Payload\":{...}}"}' - All three inspection levels ():
--inspection-level(default —INFO,output,status),nextState(adds data flow:DEBUG,afterArguments,result— use to debug JSONata/data flow),variables(adds raw HTTPTRACE/request, for HTTP Task).response - and
.syncintegrations still require a mock — for.waitForTaskToken, mock the polling API (e.g..sync, not the initial call); forDescribeExecution, also pass.waitForTaskToken.--context '{"Task":{"Token":"..."}}' - No deployment or real invocation is needed — the state is tested in isolation.
See for per-service mock structures and error/retry/Map/Parallel testing.
references/validation-and-testing.md使用带参数的TestState API()测试单个状态无需部署状态机或调用真实服务。完整的测试方案需涵盖以下四点:
--mockaws stepfunctions test-state- 精确模拟服务响应——的
--mock必须完全匹配目标AWS服务的API响应架构(字段名称区分大小写)。对于返回result和StatusCode的LambdaPayload任务:invoke。--mock '{"result":"{\"StatusCode\":200,\"Payload\":{...}}"}' - 三种检查级别():
--inspection-level(默认——包含INFO、output、status)、nextState(添加数据流:DEBUG、afterArguments、result——用于调试JSONata/数据流)、variables(添加原始HTTPTRACE/request,适用于HTTP任务)。response - 和
.sync集成仍需模拟——对于.waitForTaskToken,模拟轮询API(例如.sync,而非初始调用);对于DescribeExecution,还需传递.waitForTaskToken。--context '{"Task":{"Token":"..."}}' - 无需部署或真实调用——状态是隔离测试的。
每个服务的模拟结构以及错误/重试/Map/Parallel测试的详细信息,请查看。
references/validation-and-testing.mdBest Practices
最佳实践
- Set at the top level for new state machines unless the user wants to use JSONPath
"QueryLanguage": "JSONata" - Keep minimal — only include what the state immediately after the current state needs
Output - Use to store variables needed in later states instead of threading it through Output
Assign - Use to reference original state input
$states.input - and
Assignare evaluated in parallel from the state's entry data, NOT sequentially — a variable set inOutputis therefore NOT visible in the same state'sAssign(which still sees the pre-Outputvalues); the new value takes effect only in the next state.Assign - All JSONata expressions must produce a defined value — throws
$data.nonExistentFieldStates.QueryEvaluationError - Use to access the original workflow input from any state
$states.context.Execution.Input - Save state machine definitions with extension when working outside the console
.asl.json - Prefer the optimized Lambda integration () over the SDK integration
arn:aws:states:::lambda:invoke
- 对于新状态机,在顶层设置,除非用户希望使用JSONPath
"QueryLanguage": "JSONata" - 保持简洁——仅包含当前状态的下一个状态立即需要的内容
Output - 使用存储后续状态需要的变量,而非通过Output传递
Assign - 使用引用原始状态输入
$states.input - 和
Assign从状态的初始数据并行求值,而非顺序求值——因此在同一状态的Output中无法看到Output中设置的变量(仍会看到Assign之前的值);新值仅在下一个状态生效。Assign - 所有JSONata表达式必须生成已定义的值——会抛出
$data.nonExistentFieldStates.QueryEvaluationError - 使用从任何状态访问原始工作流输入
$states.context.Execution.Input - 在控制台外工作时,使用扩展名保存状态机定义
.asl.json - 优先选择优化的Lambda集成()而非SDK集成
arn:aws:states:::lambda:invoke
Troubleshooting
故障排查
Common Errors
常见错误
- — JSONata expression failed. Check for type errors, undefined fields, or out-of-range values.
States.QueryEvaluationError - Mixing JSONPath fields with JSONata fields in the same state.
- Using or
$at the top level of a JSONata expression — use$$instead.$states.input - Forgetting delimiters around JSONata expressions — the string will be treated as a literal.
{% %} - Assigning variables in and expecting them in
Assignof the same state — new values only take effect in the next state.Output - Reference references/validation-and-testing.md and references/error-handling.md for detailed troubleshooting information.
- ——JSONata表达式执行失败。检查类型错误、未定义字段或超出范围的值。
States.QueryEvaluationError - 在同一状态中混用JSONPath字段与JSONata字段。
- 在JSONata表达式顶层使用或
$——请改用$$。$states.input - 忘记在JSONata表达式周围添加分隔符——字符串将被视为字面量。
{% %} - 在中赋值变量并期望在同一状态的
Assign中使用——新值仅在下一个状态生效。Output - 详细故障排查信息请查看references/validation-and-testing.md和references/error-handling.md。
Security Considerations
安全注意事项
- Least-privilege execution role. Scope the state machine's IAM role to the specific resources and actions it invokes (specific Lambda/DynamoDB/SQS/SNS ARNs). Avoid policies and
*FullAccesswildcards.service:* - Encryption. Recommend encryption at rest and in transit for every data store a workflow touches: KMS-encrypted DynamoDB tables, server-side encryption () on SQS queues and SNS topics, and TLS for HTTP Tasks.
KmsMasterKeyId - Task tokens and message bodies are sensitive. A token is a credential — treat it as a secret. Do not place PII, financial data, or secrets in SQS/SNS message bodies or notifications; pass a reference ID and have recipients look up details through an authorized channel.
.waitForTaskToken - Validate input and fail fast. Validate required fields at the start of the workflow with a Choice (or Pass) state using and
$exists(), and route invalid input to a Fail state so malformed data never reaches downstream states. Protect downstream services from bursts by setting$type()on Map states and throttling upstream (StartExecution rate limits or EventBridge).MaxConcurrency - Cross-account access. When using the field to assume a role in another account, include condition keys such as
Credentialsoraws:SourceArnin the target role's trust policy to prevent unintended assumption.aws:SourceAccount - External secrets. For HTTP Tasks calling third-party APIs, store API keys and tokens in AWS Secrets Manager (referenced via an EventBridge connection), never embedded in the state machine definition.
- Observability. Enable CloudWatch Logs for executions (log level or
ALL; required for Express workflows, which have no queryable execution history), enable CloudTrail to audit Step Functions API calls, and set CloudWatch Alarms on execution failures. Always encrypt the execution log group with a customer-managed KMS key, since state input/output routinely flows through execution logs.ERROR
- 最小权限执行角色。将状态机的IAM角色权限限定为其调用的特定资源和操作(特定Lambda/DynamoDB/SQS/SNS ARN)。避免使用策略和
*FullAccess通配符。service:* - 加密。建议对工作流涉及的每个数据存储启用静态加密和传输中加密:KMS加密的DynamoDB表、SQS队列和SNS主题的服务器端加密(),以及HTTP任务的TLS加密。
KmsMasterKeyId - 任务令牌和消息体敏感。令牌是一种凭证——请将其视为机密信息。请勿在SQS/SNS消息体或通知中放置PII、财务数据或机密信息;传递参考ID,让接收方通过授权渠道查询详细信息。
.waitForTaskToken - 验证输入并快速失败。在工作流开始时使用Choice(或Pass)状态,通过和
$exists()验证必填字段,并将无效输入路由至Fail状态,确保格式错误的数据永远不会到达下游状态。通过在Map状态上设置$type()并限制上游(StartExecution速率限制或EventBridge)来保护下游服务免受突发流量影响。MaxConcurrency - 跨账户访问。使用字段承担另一个账户中的角色时,在目标角色的信任策略中包含
Credentials或aws:SourceArn等条件键,防止意外的角色承担。aws:SourceAccount - 外部机密。对于调用第三方API的HTTP任务,将API密钥和令牌存储在AWS Secrets Manager中(通过EventBridge连接引用),切勿嵌入状态机定义中。
- 可观测性。为执行启用CloudWatch Logs(日志级别或
ALL;Express工作流必须启用,因为其无可查询的执行历史),启用CloudTrail以审计Step Functions API调用,并为执行失败设置CloudWatch告警。始终使用客户管理的KMS密钥加密执行日志组,因为状态输入/输出通常会流经执行日志。ERROR