Sub topic 8
Reshaping events with input transformers
This chapter assumes you can write an event pattern, create a rule, and attach a target with the AWS CLI. You should also know the envelope fields of an EventBridge event, such as source, detail-type, and detail.
Why reshape an event
A rule delivers the whole event to its target by default. That is often more than the target needs. An SQS consumer may only need an order ID and an amount. A Lambda function may expect a different field layout than the one the producer emits. An input transformer lets you pull out the fields you need and build a new payload before delivery.
An input transformer has two parts:
- InputPathsMap is a map of variable names to JSON paths. Each path selects a value from the original event.
- InputTemplate is the text that is sent to the target. It refers to the variables in angle brackets, such as
<orderId>.
The transformer belongs to the target, not the rule. Two targets on the same rule can receive different payloads.
A worked example
The target definition below is attached to the high-value-orders rule from the earlier chapters. The part that matters here is InputTransformer.
[
{
"Id": "order-alerts-queue",
"Arn": "arn:aws:sqs:us-east-1:123456789012:order-alerts",
"InputTransformer": {
"InputPathsMap": {
"orderId": "$.detail.orderId",
"amount": "$.detail.amount"
},
"InputTemplate": "{\"orderId\": <orderId>, \"amount\": <amount>}"
},
"RetryPolicy": {
"MaximumRetryAttempts": 5,
"MaximumEventAgeInSeconds": 3600
},
"DeadLetterConfig": {
"Arn": "arn:aws:sqs:us-east-1:123456789012:order-alerts-dlq"
}
}
]Suppose the event's detail is {"orderId": "1001", "amount": 250, "status": "new"}. The two paths extract "1001" and 250. The template then produces this payload:
{"orderId": "1001", "amount": 250}Attach it with the command you already know:
aws events put-targets --rule high-value-orders --event-bus-name orders-bus --targets file://targets.jsonThe retry policy and dead-letter queue in the same file are covered in the chapter on retries.
Quoting rules
Quoting is the most common source of confusion. EventBridge inserts the value exactly as the JSON path returns it, and where the quotes come from depends on where the variable sits in the template.
- A variable used as a JSON value in a template that is itself JSON keeps its type. A string value arrives with its own quotes, and a number arrives without any. In the example above,
<orderId>produced"1001"with quotes. - A variable placed inside a quoted string is inserted as plain text. For
"Order <orderId> placed", the result isOrder 1001 placed, and you should not add quotes of your own. - If the target needs a plain-text message, such as an SNS email body, write the template as a string with no JSON structure.
If your downstream consumer rejects the payload, compare the template with the real output before changing the paths.
Predefined variables
Besides the variables you define, a template can use predefined ones such as <aws.events.event.ingestion-time> and <aws.events.rule-name>. You do not list them in InputPathsMap. They are useful for stamping a payload with the rule that produced it.
Common errors
- Path matches nothing. If a path points to a field the event does not contain, the variable is empty. Test with a sample event that includes every field you reference.
- Variable in the template but not in the map. The target configuration is rejected, or the placeholder is left unresolved. Check that every name in the template is defined in
InputPathsMap. - Invalid JSON output. The template is plain text, so EventBridge does not check that the result is valid JSON. A missing comma or a stray quote only shows up when the target fails.
- Wrong escaping in the CLI. The template is a string inside JSON, so its inner quotes need a backslash. Keeping the targets in a file avoids shell escaping on top of that.
Exercise
A rule's target needs the payload {"id": "1001"} built from an event whose detail is {"orderId": "1001", "amount": 250}. Write the InputPathsMap and InputTemplate.
Show answer
InputPathsMap: {"id": "$.detail.orderId"}. InputTemplate: "{\"id\": <id>}". The variable <id> carries the string's own quotes, so the template does not add quotes around it.
Exercise
Your target receives {"orderId": ,"amount": 250}. What is the most likely cause?
Show answer
The path for orderId matched nothing in the event, for example because it points to $.detail.orderID instead of $.detail.orderId, so the variable was empty. Fix the path and test with a sample event that contains the field.