Tutorial: Use Input Transformer to Customize What Is Passed to the Event Target - Amazon EventBridge

Tutorial: Use Input Transformer to Customize What Is Passed to the Event Target

You can use the input transformer feature of EventBridge to customize the text that is taken from an event before it's 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 doesn't exist in the event, that variable isn't created and doesn't 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>."


For more information on the event transformer, see Transforming Target Input

Create a Rule

To customize instance state change information sent to a target using the input transformer

  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 always 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.