You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过变量传递JSON至YAML使EventBridge规则正常运行?

问题:CloudFormation传递JSON字符串到EventBridge目标时的语法错误

我通过CloudFormation的YAML模板创建EventBridge规则,触发Step Function时用字符串参数传递JSON输入,结果CloudFormation报错,提示JSON里的引号丢失导致语法错误。但直接把JSON写死在YAML里就能正常运行。


相关代码

目标JSON内容

{"Guid1": "d8a47ae8-50c5-4d76-937f-124d353e125f"}

出错的YAML配置

Parameters:
  StepFunctionInput:
    Description: 触发Step Function时使用的JSON输入
    Type: String

ScheduledRule:
  Type: AWS::Events::Rule
  Properties:
    RoleArn: !GetAtt Role.Arn
    ScheduleExpression: !Sub cron(0/${StartupIntervalMinutes} * * * ? *)
    State: ENABLED
    Targets:
      - Arn: !Ref StepFunctionArn
        Id: StepFunctionV1
        Input: !Ref StepFunctionInput

AWS错误信息

JSON syntax error in input for target StepFunctionV1: [Source: (String)"{Guid1: d8a47ae8-50c5-4d76-937f-124d353e125f}"; line: 1, column: 3] (Service: AmazonCloudWatchEvents; Status Code: 400; Error Code: ValidationException; Request ID: 71520ea5-d959-495a-b89f-dbc812f78e59; Proxy: null)

正常运行的写法(直接写死JSON)

Input: '{"Guid1": "d8a47ae8-50c5-4d76-937f-124d353e125f"}'

解决方案

1. 手动转义参数里的JSON引号

传递StepFunctionInput参数时,需要把JSON内部的双引号转义,避免YAML解析时丢失引号。参数值应该写成:

"{\"Guid1\": \"d8a47ae8-50c5-4d76-937f-124d353e125f\"}"

2. 用!Sub函数自动处理字符串

不想手动转义的话,在模板里用单引号包裹!Sub函数的内容,让CloudFormation自动处理JSON结构:

Targets:
  - Arn: !Ref StepFunctionArn
    Id: StepFunctionV1
    Input: !Sub '${StepFunctionInput}'

3. 改用InputTransformer(可选)

如果输入数据来自EventBridge事件的一部分,可以用InputTransformer构造JSON,彻底避免字符串转义问题:

Targets:
  - Arn: !Ref StepFunctionArn
    Id: StepFunctionV1
    InputTransformer:
      InputPathsMap:
        guid: "$.detail.Guid1"
      InputTemplate: '{"Guid1": <guid>}'

内容的提问来源于stack exchange,提问作者Trisped

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 03:40:22