Tutorial: Use input transformer to customize what EventBridge passes to the event target - Amazon EventBridge

Tutorial: Use input transformer to customize what EventBridge passes to the event target

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. For more information, see Transforming Amazon EventBridge target input

Note

If you specify a variable to match a JSON path that doesn't exist in the event, that variable isn't created and doesn't appear in the output.

In this tutorial, you create a rule that runs when any instance changes state. Youextract the instance-id and state of an Amazon EC2 instance from the event. Then you use the input transformer to put that data into the message that goes to an Amazon SNS topic. The

For example, the rule in this tutorial matches the following Amazon EC2 instance state-change notification event.

{ "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" } }

The input transformer maps the instance variable to the $.detail.instance-id JSON path from the event, and the state variable to the $.detail.state JSON path. Then EventBridge puts the variables intothe input template "The EC2 instance <instance> has changed state to <state>." The result is the following Amazon SNS message.

The EC2 instance i-1234567890abcdef0 has changed state to stopped.

Step 1: Create a rule

Create a rule to use the input transformer to customize the instance state information that goes to a target.

To create a rule

  1. Open the Amazon EventBridge console at https://console.aws.amazon.com/events/.

  2. In the navigation pane, choose Rules.

  3. Choose Create rule.

  4. Type a name and description for the rule.

  5. For Define pattern, do the following:

    1. Choose Event pattern.

    2. Choose Pre-defined pattern by service.

    3. For Service provider, choose AWS.

    4. For Service Name, choose EC2.

    5. For Event type, choose EC2 Instance State-change Notification.

    6. Choose Any state, Any instance.

  6. For Select event bus, choose AWS default event bus. When an AWS; service in your account emits an event, it goes to your account’s default event bus.

  7. For Target, choose SNS topic.

  8. For Topic, select the Amazon SNS topic that is to be notified when Amazon EC2 instances change state.

  9. Choose Configure input, Input Transformer.

  10. For Input Path, enter {"state" : "$.detail.state", "instance" : "$.detail.instance-id"}.

  11. For Input Template, enter "The EC2 instance <instance> has changed state to <state>."

  12. Choose Create.

Step 2: Confirm success

If you see the rule in the list of rules, you've successfully created the rule.

Step 3: Clean up your resources

You can now delete the resources that you created for this tutorial, unless you want to retain them. By deleting AWS resources that you are no longer using, you prevent unnecessary charges to your AWS account.

To delete the EventBridge rule

  1. Open the Rules page of the EventBridge console.

  2. Select the rule that you created.

  3. Choose Delete.

  4. Choose Delete.