Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cloudformation Cloudwatch InputTemplate Formatting

I'm attempting to use a cloudformation template to create a cloudwatch event rule that matches a glue event and targets an SNS topic to send a message to, I can create it in the cloudwatch console, but not via a cloud watch template. Here is my event rule:

NotifyEventRule:
  Type: AWS::Events::Rule
  Properties:
    Name: JobNotifyEvent
    Description: Notification event on job status change.
    EventPattern:
      source:
        - aws.glue
      account:
        - !Ref AWS::AccountId
      detail-type:
        - Glue Job State Change
      detail:
        jobName:
          - !Ref GlueJobName
    Targets:
        - 
          Arn: 
              Ref: "JobNotificationTopic"
          Id: 
              Ref: "JobNotificationTopicName"
          InputTransformer:
            InputTemplate: "Job finished in the following state: <state>."
            InputPathsMap: 
              state: "$.detail.state"

The problem is with InputTemplate. The error I get is:

Invalid InputTemplate for target JobNotificationTopic : [Source: (String)"Job finished in the following state: null."; line: 1, column: 10]. (Service: AmazonCloudWatchEvents; Status Code: 400; Error Code: ValidationException; Request ID: 12345678...)

It seems like <state> may be the problem.

like image 334
Tibberzz Avatar asked Aug 28 '18 05:08

Tibberzz


1 Answers

The syntax for InputTemplate is for some reason quite strict in CloudFormation. It is of type string but it does not accept any form of valid YAML string.

In your case, you should use YAML Literal Block Scalar, |, before the input string.

InputTransformer:
    InputPathsMap: 
        state: "$.detail.state"
    InputTemplate: |
        "Job finished in the following state: <state>."

If the input string is multiline, each line has to be enclosed in double quotes.

InputTransformer:
    InputPathsMap:
        state: $.detail.state
        name: $.detail.name
    InputTemplate: |
        "Job <name> has just been run."
        "Job finished in the following state: <state>."

Just to note that your string uses plain flow scalars, which is picky about the : character. Colon cannot appear before a space or newline. See Yaml multiline for further details. However, as I pointed out most of these YAML multiline rules does not apply to InputTemplate.

like image 139
Vikyol Avatar answered Sep 19 '22 12:09

Vikyol