Tutorial: Use Input Transformer to Customize What is Passed to the Event Target
Amazon EventBridge is the preferred way to manage your events. CloudWatch Events and EventBridge are the same underlying service and API, but EventBridge provides more features. Changes you make in either CloudWatch or EventBridge will appear in each console. For more information, see Amazon EventBridge. |
You can use the input transformer feature of CloudWatch Events to customize the text that is taken from an event before it is input to the target of a rule.
You can define multiple JSON paths from the event and assign their outputs to
different variables. Then you can use those variables in the input template as
<variable-name
>. The characters < and > cannot be escaped.
If you specify a variable to match a JSON path that does not exist in the event, then that variable is not created and does not appear in the output.
In this tutorial, we extract the instance-id and state of an Amazon EC2 instance from the instance state change event. We use the input transformer to put that data into an easy-to-read message that is sent to an Amazon SNS topic. The rule is triggered when any instance changes to any state. For example, with this rule, the following Amazon EC2 instance state-change notification event produces the Amazon SNS message The EC2 instance i-1234567890abcdef0 has changed state to stopped.
{ "id":"7bf73129-1428-4cd3-a780-95db273d1602", "detail-type":"EC2 Instance State-change Notification", "source":"aws.ec2", "account":"123456789012", "time":"2015-11-11T21:29:54Z", "region":"us-east-1", "resources":[ "arn:aws:ec2:us-east-1:123456789012:instance/ i-1234567890abcdef0" ], "detail":{ "instance-id":" i-1234567890abcdef0", "state":"stopped" } }
We achieve this by mapping the
instance
variable to the $.detail.instance-id
JSON path from the event, and the state
variable to the $.detail.state
JSON path. We then set the input template as "The EC2 instance <instance> has changed state to
<state>."
Create a Rule
To customize instance state change information sent to a target using the input transformer
Open the CloudWatch console at https://console.amazonaws.cn/cloudwatch/
. -
In the navigation pane, choose Events, Create rule.
-
For Event source, do the following:
-
Choose Event Pattern.
-
Choose Build event pattern to match events by service.
-
Choose EC2, EC2 Instance State-change Notification.
-
Choose Any state, Any instance.
-
-
For Targets, choose Add target, SNS topic.
-
For Topic, select the Amazon SNS topic for which to be notified when Amazon EC2 instances change state.
-
Choose Configure input, Input Transformer.
-
In the next box, type {"state" : "$.detail.state", "instance" : "$.detail.instance-id"}
-
In the following box, type "The EC2 instance <instance> has changed state to <state>."
-
Choose Configure details.
-
Type a name and description for the rule, and choose Create rule.