Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i add Input transformation to atarget using aws cdk for a cloudwatch event rule?

Tags:

aws-cdk

After i create a cloud-watch event rule i am trying to add a target to it but i am unable to add a input transformation. Previously the add target had the props allowed for input transformation but it does not anymore.

codeBuildRule.addTarget(new SnsTopic(props.topic));

The aws cdk page provides this solution but i dont exactly understand what it says

You can add additional targets, with optional input transformer using eventRule.addTarget(target[, input]). For example, we can add a SNS topic target which formats a human-readable message for the commit.

like image 256
rookieCoder Avatar asked Dec 30 '19 07:12

rookieCoder


People also ask

What is input transformer?

You can use the Input transformer in EventBridge to customize text from an event before you send it to the target of a rule. To do this, you define JSON paths from the event and assign their outputs to different variables. Then you can use those variables in the input template. The characters < and > can't be escaped.

How do you create a CloudWatch event rule?

To create a rule that triggers on an event: Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/ . In the navigation pane, choose Events, Create rule. For Event source, do the following: Choose Event Pattern, Build event pattern to match events by service.

How do you test the CloudWatch event rule?

To test your rule Open the CloudWatch console at https://console.aws.amazon.com/cloudwatch/ . In the navigation pane, choose Events, Rules, select the name of the rule that you created, and choose Show metrics for the rule.

What is Aws_cloudwatch_event_target?

Resource: aws_cloudwatch_event_target. Provides an EventBridge Target resource. Note: EventBridge was formerly known as CloudWatch Events. The functionality is identical.


2 Answers

You should specify the message prop and use RuleTargetInput static methods. Some of these methods can use strings returned by EventField.fromPath():

// From a path
codeBuildRule.addTarget(new SnsTopic(props.topic, {
  message: events.RuleTargetInput.fromEventPath('$.detail')
}));

// Custom object
codeBuildRule.addTarget(new SnsTopic(props.topic, {
  message: RuleTargetInput.fromObject({
    foo: EventField.fromPath('$.detail.bar')
  })
}));
like image 189
jogold Avatar answered Oct 09 '22 19:10

jogold


I had the same question trying to implement this tutorial in CDK: Tutorial: Set up a CloudWatch Events rule to receive email notifications for pipeline state changes

I found this helpful as well: Detect and react to changes in pipeline state with Amazon CloudWatch Events

NOTE: I could not get it to work using the Pipeline's class method onStateChange().

I ended up writing a Rule:

const topic = new Topic(this, 'topic', {topicName: 'codepipeline-notes-failure',
});

const description = `Generated by the CDK for stack: ${this.stackName}`;
new Rule(this, 'failed', {
  description: description,
  eventPattern: {
    detail: {state: ['FAILED'], pipeline: ['notes']},
    detailType: ['CodePipeline Pipeline Execution State Change'],
    source: ['aws.codepipeline'],
  },
  targets: [
    new SnsTopic(topic, {
      message: RuleTargetInput.fromText(
        `The Pipeline '${EventField.fromPath('$.detail.pipeline')}' has ${EventField.fromPath(
          '$.detail.state',
        )}`,
      ),
    }),
  ],
});

After implementing, if you navigate to Amazon EventBridge -> Rules, then select the rule, then select the Target(s) and then click View Details you will see the Target Details with the Input transformer & InputTemplate.

Input transformer: {"InputPathsMap":{"detail-pipeline":"$.detail.pipeline","detail-state":"$.detail.state"},"InputTemplate":"\"The Pipeline '<detail-pipeline>' has <detail-state>\""}

like image 26
bravogolfgolf Avatar answered Oct 09 '22 19:10

bravogolfgolf