AWS Python CDK input transformer

0

Hello, I'm working on adaptation of this article with help of Python CDK. I'm stuck on last point (6) where I must code InputTransformer. I'm using

events.CfnRule.InputTransformerProperty

with below code

        rule_input_transformer = events.CfnRule.InputTransformerProperty(
            input_paths_map={
                "asgname": "$.detail.AutoScalingGroupName",
                "instanceid": "$.detail.EC2InstanceId",
                "lchname": "$.detail.LifecycleHookName"
            },
            input_template=f'\"InstanceId\":[<instanceid>],\"ASGName\":[<asgname>],\"LCHName\":[<lchname>\"],\"automationAssumeRole\":[\"{cw_event_role.role_arn}\"]'
            # input_template="{\"InstanceId\":[<instanceid>],\"ASGName\":[<asgname>],\"LCHName\":[<lchname>],\"automationAssumeRole\":[\"arn:aws:iam::012345678901:role/SSM-automation-Role\"]}"
        )

But I can't get my head around of correct syntax as cloud-formtion is yelling at me:

Invalid InputTemplate for target cw_ssm_event_id : [Source: (String)""InstanceId":[<instanceid>],"ASGName":[<asgname>],"LCHName":[<lchname>"],"automationAssumeRole":["arn:aws:iam::012345678901:role/CWEventRole"]"

Below is synthesized cfn template from cdk synth command:

  cwssmevent:
    Type: AWS::Events::Rule
    Properties:
      Description: Cloudwatch to invoke ssm document during ASG scalein
      EventBusName: default
      EventPattern:
        detail:
          AutoScalingGroupName:
            - ssm_docs_test_asg_maciej
        detailType:
          - EC2 Instance-terminate Lifecycle Action
        source:
          - aws.autoscaling
      Name: RunCodeBeforeEc2Termination
      Targets:
        - Arn: arn:aws:ssm:eu-west-1:012345678901:automation-definition/test-asg-automation:$DEFAULT
          Id: cw_ssm_event_id
          InputTransformer:
            InputPathsMap:
              asgname: $.detail.AutoScalingGroupName
              instanceid: $.detail.EC2InstanceId
              lchname: $.detail.LifecycleHookName
            InputTemplate:
              Fn::Join:
                - ""
                - - '"InstanceId":[<instanceid>],"ASGName":[<asgname>],"LCHName":[<lchname>"],"automationAssumeRole":["'
                  - Fn::GetAtt:
                      - cweventrole2159518F
                      - Arn
                  - '"]'
2 Answers
0
Accepted Answer

Hello ,

From the above problem statement I understand you were facing synatx issue while specifying the property InputTemplate in cloudformation.

The correct syntax would be :

input_template=f'{{"InstanceId": [<instanceid>],"ASGName": [<asgname>],"LCHName": [<lchname>],"automationAssumeRole": ["{cw_event_role.role_arn}"]}}'

Also I would like to provide the full code I tested:

        input_transformer_property = events.CfnRule.InputTransformerProperty(
            input_paths_map={
                "asgname": "$.detail.AutoScalingGroupName",
                "instanceid": "$.detail.EC2InstanceId",
                "lchname": "$.detail.LifecycleHookName"
            },
            input_template=f'{{"InstanceId": [<instanceid>],"ASGName": [<asgname>],"LCHName": [<lchname>],"automationAssumeRole": ["{cw_event_role.role_arn}"]}}'
            
        )

Below is synthesized cfn template from cdk synth command:

JSON :
"MyCfnRule": {
            "Type": "AWS::Events::Rule",
            "Properties": {
                "Description": "My event rule",
                "ScheduleExpression": "cron(0 0 * * ? *)",
                "State": "ENABLED",
                "Targets": [
                    {
                        "Arn": {
                            "Fn::GetAtt": [
                                "HelloHandler2E4FBA4D",
                                "Arn"
                            ]
                        },
                        "Id": "MyTarget",
                        "InputTransformer": {
                            "InputPathsMap": {
                                "asgname": "$.detail.AutoScalingGroupName",
                                "instanceid": "$.detail.EC2InstanceId",
                                "lchname": "$.detail.LifecycleHookName"
                            },
                            "InputTemplate": {
                                "Fn::Join": [
                                    "",
                                    [
                                        "{\"InstanceId\": [<instanceid>],\"ASGName\": [<asgname>],\"LCHName\": [<lchname>],\"automationAssumeRole\": [\"",
                                        {
                                            "Fn::GetAtt": [
                                                "cweventroleid23238EBB2881",
                                                "Arn"
                                            ]
                                        },
                                        "\"]}"
                                    ]
                                ]
                            }
                        }
                    }
                ]
            }
Yaml :

MyCfnRule:
    Type: 'AWS::Events::Rule'
    Properties:
      Description: My event rule
      ScheduleExpression: cron(0 0 * * ? *)
      State: ENABLED
      Targets:
        - Arn: !GetAtt 
            - HelloHandler2E4FBA4D
            - Arn
          Id: MyTarget
          InputTransformer:
            InputPathsMap:
              asgname: $.detail.AutoScalingGroupName
              instanceid: $.detail.EC2InstanceId
              lchname: $.detail.LifecycleHookName
            InputTemplate: !Join 
              - ''
              - - >-
                  {"InstanceId": [<instanceid>],"ASGName":
                  [<asgname>],"LCHName": [<lchname>],"automationAssumeRole": ["
                - !GetAtt 
                  - cweventroleid23238EBB2881
                  - Arn
                - '"]}'
AWS
answered 8 months ago
0

Holy molly, thank you! Couldn't figure it out that I need to add 2nd {} pair

Maciej
answered 8 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions