AWS Step Functions/CDK问题:无法为Catch捕获器配置输出
解决CDK中Step Functions Catch捕获器outputs不生效的问题
你在使用aws-cdk-lib 2.178.1版本时,通过jsonata方式定义Step Functions的Glue任务步骤,并为其添加Catch捕获器后,发现设置的outputs参数完全没有生效——部署后的CloudFormation模板里不存在该配置,尝试字符串等多种数据类型均无效。这是当前CDK JSONata API的已知bug,可通过手动覆盖CloudFormation属性的方式解决。
解决方案
使用add_property_override方法直接为Catch规则添加输出配置,对应Step Functions CloudFormation定义中Catch数组的Result(固定输出)、ResultPath或OutputPath字段:
修改后的完整代码示例
from aws_cdk import aws_stepfunctions_tasks as tasks from aws_cdk import aws_stepfunctions as sfn states = tasks.GlueStartJobRun.jsonata( self, "Start_job", glue_job_name="{% $states.input %}", ).add_catch( sfn.Fail.jsonata(self, "boom"), errors=["States.ALL"], ) # 手动添加Catch的固定字符串输出,注意字符串需用双引号包裹(符合JSON格式) states.add_property_override("Catch.0.Result", '"a string"') sfn.StateMachine( self, "State_machine", query_language=sfn.QueryLanguage.JSON_PATH, definition_body=sfn.ChainDefinitionBody.from_chainable(states), )
参数说明
- 如果需要将错误结果存储到输入的指定路径,替换为
ResultPath:states.add_property_override("Catch.0.ResultPath", "$.errorDetails") - 如果需要从捕获结果中提取特定数据作为下一步输入,使用
OutputPath:states.add_property_override("Catch.0.OutputPath", "$.errorMessage") - 若存在多个Catch规则,调整索引数字(如
Catch.1)对应不同的捕获器。
内容的提问来源于stack exchange,提问作者Guillaume
相关产品推荐
相关产品推荐

