AWS Step Function状态机定义报错及CF转HCL/JSON工具咨询
Step Function 状态机定义错误修复方案
一、错误逐一修复说明
所有报错均为不符合Amazon States Language语法规范导致,对应修复逻辑如下:
Retry/Catch/Choices字段要求为数组格式:现有定义中以上字段均为单个对象,需用方括号包裹改为对象数组,例如"Retry": { ... }修改为"Retry": [ { ... } ]End字段要求为布尔值:现有定义中写为字符串格式"End": "True"/"End": "true",需去掉引号改为"End": trueResultPath若需要丢弃结果,不能写字符串"null",需改为JSON原生null值,即"ResultPath": null- 数值类字段需去掉引号:
IntervalSeconds、MaxAttempts、BackoffRate、NumericEquals为数字类型,现有定义加了双引号变为字符串,删除引号即可 - 完成上述语法修复后,
MISSING_END_STATE、Iterator不可达的报错会自动消失,两类报错是前置语法错误导致的解析异常误报
二、修复后的完整状态机JSON定义
{ "StartAt":"StartState", "States":{ "StartState":{ "Type":"Pass", "Next":"GetLastExecutionDateTime" }, "GetLastExecutionDateTime":{ "Type":"Task", "Parameters":{ "Payload.$":"$" }, "Resource":"${aws_lambda_function.read.arn}", "Retry":[{ "ErrorEquals":[ "States.ALL" ], "IntervalSeconds":2, "MaxAttempts":1, "BackoffRate":2 }], "Next":"GetDeltaRecordsNumber", "ResultPath":"$.lastExecutedDateTime", "OutputPath":"$.lastExecutedDateTime.Payload" }, "GetDeltaRecordsNumber":{ "Type":"Task", "Parameters":{ "Payload.$":"$" }, "Resource":"${aws_lambda_function.count.arn}", "Retry":[{ "ErrorEquals":[ "States.ALL" ], "IntervalSeconds":2, "MaxAttempts":6, "BackoffRate":2 }], "Next":"DeltaRecordsNumber", "ResultPath":"$.Payload" }, "DeltaRecordsNumber":{ "Type":"Choice", "Choices":[{ "Variable":"$.Payload.Payload.totalRows", "NumericEquals":0, "Next":"EndState" }], "Default":"Iterator" }, "Iterator":{ "Type":"Map", "Next":"PassNewExecutionDateTime", "Iterator":{ "StartAt":"ExecuteSqlQuery", "States":{ "ExecuteSqlQuery":{ "Type":"Task", "Resource":"${aws_lambda_function.query.arn}", "OutputPath":"$.Payload", "Parameters":{ "Payload.$":"$" }, "End":true } } }, "ItemsPath":"$.Payload.Payload.iterations", "Retry":[{ "ErrorEquals":[ "States.ALL" ], "IntervalSeconds":1, "MaxAttempts":1, "BackoffRate":1 }], "Catch":[{ "ErrorEquals":[ "States.ALL" ], "Next":"EndState", "ResultPath":"$.Payload" }], "ResultPath":null }, "PassNewExecutionDateTime":{ "Type":"Pass", "Next":"StoreNewExecutionDateTIme", "OutputPath":"$.Payload.Payload" }, "StoreNewExecutionDateTIme":{ "Type":"Task", "Resource":"${aws_lambda_function.write.arn}", "OutputPath":"$.Payload", "Parameters":{ "Payload.$":"$" }, "Retry":[{ "ErrorEquals":[ "States.ALL" ], "IntervalSeconds":2, "MaxAttempts":1, "BackoffRate":2 }], "Next":"EndState" }, "EndState":{ "Type":"Pass", "End":true } } }
三、CloudFormation格式Step Function定义转HCL/JSON方案
无需额外工具,CloudFormation中的Step Function定义本身就是标准ASL JSON格式,直接复制定义内容,将其中CloudFormation的变量引用规则替换为Terraform的插值变量规则,即可直接嵌入到Terraform的aws_sfn_state_machine资源的definition参数中使用。
内容的提问来源于stack exchange,提问作者Xcute
相关产品推荐
相关产品推荐

