Sub topic 7
Choosing and configuring targets
This chapter assumes you can create a rule on an event bus and write an event pattern, as covered in the earlier chapters. It also assumes you know basic IAM roles and policies, and that you have used at least one of Lambda, SQS, or SNS. Here you attach targets to rules, choose between them, and avoid the usual configuration errors.
What a target is
A target is the resource that EventBridge invokes or sends data to when an event matches a rule. A rule can have up to five targets. When an event matches, EventBridge delivers it to every target on the rule, and each target is delivered independently. A failure at one target does not stop delivery to the others.
Each target definition has these main fields:
Id: a name that is unique within the rule. You use it to update or remove the target.Arn: the ARN of the resource to invoke.RoleArn: an IAM role that EventBridge assumes to call the target. Some targets need it and some do not.Input,InputPath, orInputTransformer: controls what the target receives. Without any of these, the target receives the whole event.RetryPolicyandDeadLetterConfig: control failure handling. The next chapters cover these in detail.
Common target types
EventBridge supports many AWS services as targets. The ones you will use most often are in this table.
| Target | Typical use | How EventBridge is authorized |
|---|---|---|
| Lambda function | Run custom code per event | Resource-based policy on the function |
| SQS queue | Buffer events for a consumer that works at its own pace | Resource-based policy on the queue |
| SNS topic | Fan out to email, SMS, or many subscribers | Resource-based policy on the topic |
| Step Functions state machine | Start a workflow | Execution role (`RoleArn`) |
| Kinesis data stream | Feed a streaming pipeline | Execution role (`RoleArn`) |
| Another event bus | Route to another account or Region | Execution role (`RoleArn`) and a bus policy on the destination |
| API destination | Call an external HTTPS endpoint | Execution role and a connection |
| CloudWatch Logs group | Keep a record of events for debugging | Resource-based policy on the log group |
The authorization column matters more than it looks. It is the most common reason a rule matches but nothing arrives. The Permissions chapter covers it in full. For now, remember this rule of thumb: services that support resource-based policies (Lambda, SQS, SNS, CloudWatch Logs) are authorized on the target, and the others need a role that EventBridge can assume.
How to choose a target
Pick the target by what you need from the consumer, not by what is easiest to wire up.
- Lambda fits when you want to run code immediately for each event. EventBridge invokes Lambda asynchronously, so the invocation returns quickly and Lambda manages its own retries on top of the EventBridge retry policy.
- SQS fits when the consumer can be slower than the producer, or when you need to absorb bursts. The queue holds events until the consumer reads them. A standard queue does not preserve order. For ordering, use a FIFO queue and set
SqsParameterswith aMessageGroupId. - SNS fits when several independent subscribers need the same event. Often a single EventBridge target pointing at a topic is simpler than adding many targets.
- Step Functions fits when handling an event takes several steps, branches, or waits. EventBridge starts an execution and passes the event as its input.
- API destinations fit when the consumer is outside AWS, such as a SaaS webhook. You define a connection that holds the credentials, then an API destination that holds the URL and HTTP method. Each API destination has an invocation rate limit that you set, which protects the external service.
- Another event bus fits when you want to centralize events or cross an account or Region boundary.
Using a queue between EventBridge and a slow or fragile consumer is a common design. It separates delivery from processing, so a consumer outage does not lose events or force retries against the bus.
Create a rule and attach targets
The rule and targets are separate API calls. First create the rule, then attach the targets. This example uses the pattern.json from the earlier chapter on event patterns.
aws events put-rule --name high-value-orders --event-bus-name orders-bus --event-pattern file://pattern.json
aws events put-targets --rule high-value-orders --event-bus-name orders-bus --targets file://targets.jsonTwo details cause frequent mistakes. First, --event-bus-name is required on both calls whenever the rule is on a custom bus. If you leave it out, the CLI uses the default bus, and you get a "rule does not exist" error or, worse, you change a rule on the wrong bus. Second, put-targets can partially fail, and the CLI still exits normally. Always check FailedEntryCount in the response.
A target file with an SQS queue looks like this. It uses an input transformer to send a reduced payload, a retry policy, and a dead-letter queue.
[
{
"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"
}
}
]The Id is any string you choose, up to 64 characters, unique within the rule. Calling put-targets again with an existing Id replaces that target's configuration. This makes the call idempotent, but it also means a typo in the Id adds a new target instead of updating the old one.
Target-specific parameters
Some targets accept an extra parameter block. These are set on the target object, next to Arn.
SqsParameters:MessageGroupIdis required for FIFO queues.KinesisParameters:PartitionKeyPathpicks the event field used as the partition key. Events with the same key go to the same shard.EcsParameters: task definition, launch type, network configuration, and task count for running an ECS task per event.BatchParameters: job definition and job name for AWS Batch.HttpParameters: header, query string, and path parameters for API destinations and API Gateway targets.RunCommandParameters: instance targets for Systems Manager Run Command.
For example, a FIFO queue target needs the group ID, or delivery fails:
[
{
"Id": "fifo-orders",
"Arn": "arn:aws:sqs:us-east-1:123456789012:orders.fifo",
"SqsParameters": {
"MessageGroupId": "orders"
}
}
]Granting EventBridge access
For a Lambda target, add a permission to the function so the EventBridge service can invoke it, limited to your rule:
aws lambda add-permission \
--function-name process-order \
--statement-id allow-eventbridge-high-value \
--action lambda:InvokeFunction \
--principal events.amazonaws.com \
--source-arn arn:aws:events:us-east-1:123456789012:rule/orders-bus/high-value-ordersThe --source-arn is the rule ARN. Without it, any rule in any account could invoke the function. Note that the ARN of a rule on a custom bus includes the bus name, as shown above.
If you create the target through the console, it adds this permission for you. If you use the CLI, CloudFormation, or another tool, you must add it yourself. This is why a rule that works when you build it in the console can fail after you move it to code.
Common errors
- The rule matches, but the target gets nothing. Check the permission first. Then check
FailedInvocationsfor the rule in CloudWatch metrics, and look in the dead-letter queue if you configured one. put-targetsreports a failed entry. The response lists anErrorCodeandErrorMessagefor that entry. Typical causes are a role that EventBridge cannot assume, or a missing required parameter such asMessageGroupId.- A target is added instead of replaced. The
Iddiffers from the existing one. List targets withaws events list-targets-by-ruleand compare. - The rule on the wrong bus. The
--event-bus-nameoption was omitted. - More than five targets are needed. Point the rule at an SNS topic or another bus, or create a second rule with the same pattern.
- A target in the same event matches but gets a different shape than expected. An input transformer or
InputPathis set on the target. Remove it to see the full event.
Removing targets
To remove a target, use remove-targets with the target Id. You must remove all targets before you can delete a rule.
aws events remove-targets --rule high-value-orders --event-bus-name orders-bus --ids order-alerts-queue
aws events delete-rule --name high-value-orders --event-bus-name orders-busExercise
You attach a Lambda function as a target using the AWS CLI. The rule matches test events, but the function never runs and no error appears on the producer side. What is the most likely cause, and how do you fix it?
Show answer
The function's resource-based policy does not allow events.amazonaws.com to invoke it. Console-created targets add this permission automatically, but the CLI does not. Run aws lambda add-permission with --principal events.amazonaws.com and --source-arn set to the rule's ARN, including the bus name for a custom bus.
Exercise
A rule has a target with the Id "order-queue". You run put-targets with the same queue ARN but the Id "order-queue-2". What happens, and how do you check?
Show answer
EventBridge adds a second target, so the queue now receives each matching event twice. Run aws events list-targets-by-rule with the rule name and bus name to see both targets, then remove the unwanted one with remove-targets.