Skip to content

events: using fromObject does not maintain keys in inputTransformer #33845

Open
@kenmacf2

Description

@kenmacf2

Describe the bug

creating a rule target using fromObject does not preserve the key names, but rather generates a key from the values in the input paths. using L1 CfnRule works as expected

import * as cdk from "aws-cdk-lib";
import * as events from "aws-cdk-lib/aws-events";
import * as targets from "aws-cdk-lib/aws-events-targets";
import * as sqs from "aws-cdk-lib/aws-sqs";
import { Construct } from "constructs";

export class BugfixEventbridgeStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    let inputPathsMap = {
      myName: events.EventField.fromPath("$.detail.name"),
      myUUID: events.EventField.fromPath("$.detail.uuid"),
    };

    const queue = new sqs.Queue(this, "myQueue");

    new events.CfnRule(this, "myCFNrule", {
      name: "myCFNrule",
      eventPattern: {
        source: ["aws.ec2"]
      },
      state: "ENABLED",
      targets: [
        {
          arn: queue.queueArn,
          id: "shared-sqs-cfn",
          inputTransformer: {
            inputTemplate: JSON.stringify(inputPathsMap),
            inputPathsMap: inputPathsMap,
          },
        },
      ],
    });

    const cdkRule = new events.Rule(this, "myCDKrule", {
      ruleName: 'myCDKrule',
      eventPattern: {
        source: ["aws.ec2"]
      }
    });
    cdkRule.addTarget(new targets.SqsQueue(queue, {
      message: events.RuleTargetInput.fromObject(inputPathsMap)
    }));

  }
}


Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Version

No response

Expected Behavior

InputPathsMap is the same for myCFNrule and myCDKrule, using the key/value pairs provided in inputPathsMap

Current Behavior

using the L2 construct (fromObject), keys provided in inputPathsMap are overwritten with system-generated keys. from cdk synth:

  myCFNrule:
    Type: AWS::Events::Rule
    Properties:
      EventPattern:
        source:
          - aws.ec2
      Name: myCFNrule
      State: ENABLED
      Targets:
        - Arn:
            Fn::GetAtt:
              - myQueue4FDFF71C
              - Arn
          Id: shared-sqs-cfn
          InputTransformer:
            InputPathsMap:
              myName: $.detail.name
              myUUID: $.detail.uuid
            InputTemplate: '{"myName":"$.detail.name","myUUID":"$.detail.uuid"}'
    Metadata:
      aws:cdk:path: BugfixEventbridgeStack/myCFNrule
  myCDKruleB18813AA:
    Type: AWS::Events::Rule
    Properties:
      EventPattern:
        source:
          - aws.ec2
      Name: myCDKrule
      State: ENABLED
      Targets:
        - Arn:
            Fn::GetAtt:
              - myQueue4FDFF71C
              - Arn
          Id: Target0
          InputTransformer:
            InputPathsMap:
              detail-name: $.detail.name
              detail-uuid: $.detail.uuid
            InputTemplate: '{"myName":<detail-name>,"myUUID":<detail-uuid>}'
    Metadata:
      aws:cdk:path: BugfixEventbridgeStack/myCDKrule/Resource

Reproduction Steps

run the code provided in issue description

Possible Solution

No response

Additional Information/Context

same issue occurs whether using events.RuleTargetInput.fromObject(inputPathsMap) or events.RuleTargetInput.fromText(JSON.stringify(inputPathsMap))

CDK CLI Version

2.1004.0 (build f0ad96e)

Framework Version

No response

Node.js Version

v23.9.0

OS

Windows

Language

TypeScript

Language Version

[email protected], [email protected]

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    @aws-cdk/aws-eventsRelated to CloudWatch EventsbugThis issue is a bug.effort/mediumMedium work item – several days of effortp1

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions