Pass JSON to YAML through variable

Viewed 18

I have a YAML file used to create an EventBridge rule. The rule calls a step-function which takes a JSON string as input. When I pass the string in using a variable, AWS CloudFormation reports an error, which seems to indicate that the quotation marks were removed from the JSON.

How do pass the JSON string into the YAML so AWS gets the correct YAML?

JSON:

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

Relevant YAML:

Parameters:
  StepFunctionInput:
    Description: The JSON input to use when triggering the step function.
    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 Error:

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)

Putting the JSON directly in the YAML (instead of passing it in through a variable) works:

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

Turns out it was a PowerShell issue. PowerShell was removing the double quotes from the string before passing it to the AWS CLI. To get past this, I escaped each string with a \ per the AWS documentation.

Before PowerShell sends a command to the AWS CLI, it determines if your command is interpreted using typical PowerShell or CommandLineToArgvW quoting rules. When PowerShell processes using CommandLineToArgvW, you must escape characters with a backslash \

Double quotation marks " " are called expandable strings. Variables can be passed in expandable strings. For double quoted strings you have to escape twice using ``` for each quote instead of only using a backtick. The backtick escapes the backslash, and then the backslash is used as an escape character for the CommandLineToArgvW process.

Related