Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(aws-events): RuleTargetInput.fromObject generating invalid template during CDK deploy #31851

Closed
1 task
CrypticCabub opened this issue Oct 22, 2024 · 6 comments
Closed
1 task
Assignees
Labels
@aws-cdk/aws-events Related to CloudWatch Events bug This issue is a bug. p2

Comments

@CrypticCabub
Copy link

CrypticCabub commented Oct 22, 2024

Describe the bug

When using RuleTargetInput.fromObject() with EventField.fromPath fields, CDK generates an invalid InputPathsMap by including a trailing comma in the InputsPathMap.

However, this behavior is not consistent. When running cdk synth, the InputsPathMap is generated correctly, but it is not generated correctly when attempting to deploy with cdk deploy or with Template.fromStack() in snapshot tests. (see examples)

Regression Issue

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

Last Known Working CDK Version

N/A

Expected Behavior

When using RuleTargetInput.fromObject(), I expect a valid InputsPathMap to be generated every time

Current Behavior

Running a snapshot test or cdk deploy produces the following:

"InputTransformer": {
  "InputPathsMap": {
    "detail-type": "$.detail-type",
    "f2": "$.",
  },
  "InputTemplate": "{"message":"EventBus received <detail-type> event","module":"EventBus","event":<f2>}",
},

whereas running cdk synth produces the following:

"InputTransformer": {
 "InputPathsMap": {
  "detail-type": "$.detail-type",
  "f2": "$."
 },

The additional comma in the first example makes the stack undeployable and causes the following CloudFormation Error:

12:07:26 PM | CREATE_FAILED        | AWS::Events::Rule                   | EventBusRule4D34F922
Resource handler returned message: "InputPath for target Target0 is invalid. (Service: EventBridge, Status Code: 400, Request ID: 1a2365b2-6d9b-4f00-a1b3-d809f3db94d8)" (RequestToken: 0887dd15-041d-e40a-144c-1d787923e0e5, HandlerErrorCode: GeneralServiceException)

Reproduction Steps

cdk app:

import { Stack, type StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";
import {EventBus, EventField, Rule, RuleTargetInput} from "aws-cdk-lib/aws-events";
import {CloudWatchLogGroup} from "aws-cdk-lib/aws-events-targets";
import {LogGroup} from "aws-cdk-lib/aws-logs";
import * as cdk from "aws-cdk-lib";

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

    const logGroup = new LogGroup(this, "LogGroup", {});

    const eventBus = new EventBus(this, "EventBus", {
      description: "example event bus",
    });


    new Rule(this, "EventBusRule", {
      description: "logs all events that get sent to the event bus",
      eventBus: eventBus,
      targets: [
        new CloudWatchLogGroup(logGroup, {
          installLatestAwsSdk: false,
          retryAttempts: 20,
          logEvent: RuleTargetInput.fromObject({
            message: `EventBus received ${EventField.fromPath("$.detail-type")} event`,
            module: "EventBus",
            event: EventField.fromPath("$."),
          }),
        }),
      ],
      eventPattern: {
        source: [{ prefix: "" }] as any[],
      },
    });
  }
}

const app = new cdk.App();

new BrokenExample(app, "BrokenExample", {})

snapshot test example:

import { describe, expect, it } from "vitest";
import {App} from "aws-cdk-lib";
import {Template} from "aws-cdk-lib/assertions";
//import BrokenExample

describe("Snapshot", () => {
  it("matches the snapshot", () => {
    const app = new App();
    const stack = new BrokenExample(app, "example");
    const template = Template.fromStack(stack);
    expect(template).toMatchSnapshot();
  });
});

Possible Solution

Not sure why the use of cdk synth would have a different result than cdk deploy, but the issue is likely a simple off-by-one error on a for loop somewhere.

Additional Information/Context

No response

CDK CLI Version

2.163.0 (build ad5325a)

Framework Version

No response

Node.js Version

18.18.2

OS

Mac

Language

TypeScript

Language Version

5.6.3

Other information

No response

@CrypticCabub CrypticCabub added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Oct 22, 2024
@github-actions github-actions bot added the @aws-cdk/aws-events Related to CloudWatch Events label Oct 22, 2024
@khushail khushail added needs-reproduction This issue needs reproduction. p2 and removed needs-triage This issue or PR still needs to be triaged. labels Oct 22, 2024
@khushail khushail self-assigned this Oct 22, 2024
@CrypticCabub
Copy link
Author

further debugging on my side makes it appear that the culprit for the deployment error is actually the selector under event: and the extra comma is an artifact of jest snapshots but doesn't make it through to the deployment console.

Am doing some further testing to confirm, but this may be another case of playing hide-and-seek with punctuation

@khushail khushail added investigating This issue is being investigated and/or work is in progress to resolve the issue. and removed needs-reproduction This issue needs reproduction. labels Oct 23, 2024
@khushail
Copy link
Contributor

khushail commented Oct 23, 2024

Hi @CrypticCabub , thanks for reaching out.

I tried to repro the mentioned scenario. However for me, the comma is not added in both the cases when I do cdk synth or cdk deploy but deployment fails in both cases with the same error of GeneralServiceException. I am trying to identify the root cause though.

@khushail khushail added p2 and removed p2 labels Oct 23, 2024
@CrypticCabub
Copy link
Author

I believe the root cause is that "$." is not the correct selector but rather should be just "$". Am still having some problems with permissions not allowing EventBridge to actually write the log to cloudwatch, but deployment isn't failing anymore

@khushail
Copy link
Contributor

khushail commented Oct 29, 2024

@CrypticCabub ,I am still not able to repro the scenario you mentioned initially but did some observations by tweaking the code -

So I tried replacing the event.fromPath("$") with different value and my synth as well as deployment succeeded.
Sharing the code and result -

  1. initial code -
const logGroup = new LogGroup(this, "LogGroup", {});

    const eventBus = new EventBus(this, "EventBus", {
      description: "example event bus",
    });


    new Rule(this, "EventBusRule", {
      description: "logs all events that get sent to the event bus",
      eventBus: eventBus,
      targets: [
        new CloudWatchLogGroup(logGroup, {
          installLatestAwsSdk: false,
          retryAttempts: 20,
          logEvent: RuleTargetInput.fromObject({
            message: `EventBus received ${EventField.fromPath("$.detail-type")} event`,
            module: "EventBus",
            event: EventField.fromPath("$."),
          }),
        }),
      ],
      eventPattern: {
        source: [{ prefix: "" }] as any[],
      },
    });
  }
  1. changed to this -
    const logGroup = new LogGroup(this, "LogGroup", {});

  const eventBus = new EventBus(this, "EventBus", {
    description: "example event bus",
  });


  new Rule(this, "EventBusRule", {
    description: "logs all events that get sent to the event bus",
    eventBus: eventBus,
    targets: [
      new CloudWatchLogGroup(logGroup, {
        installLatestAwsSdk: false,
        retryAttempts: 20,
        logEvent: RuleTargetInput.fromObject({
          message: `EventBus received ${EventField.fromPath("$.detail-type")} event`,
          module: "EventBus",
          event: EventField.fromPath("$.detail-type"),
        }),
      }),
    ],
    eventPattern: {
      source: [{ prefix: "" }] as any[],
    },
  });
}
  1. deploying code 1 resulted in error - "InputPath for target Target0 is invalid. but when I changed the .fromPath("$.detail-type"), the result from cdk diff` -
Screenshot 2024-10-29 at 11 36 49 AM

which means the earlier InputTemplate was wrongly generated due to the path mentioned.

Deploying the changed code succeeded-

Screenshot 2024-10-29 at 12 00 03 PM

AFAIU, the fromPath() should have complete path value which is properly synthesized and deployed. Giving the value as "$." is incorrect (as you also noticed above) as CDK might be expecting complete path here. However I am trying to look for evidence to support my claim :)

@CrypticCabub
Copy link
Author

yes, I can confirm that the issue appears to have been the $. selector, so this looks like a false-positive. Thanks for taking the time to look into this.

Copy link

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 29, 2024
@khushail khushail removed the investigating This issue is being investigated and/or work is in progress to resolve the issue. label Oct 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
@aws-cdk/aws-events Related to CloudWatch Events bug This issue is a bug. p2
Projects
None yet
Development

No branches or pull requests

2 participants