Sub topic 7
Choosing and configuring targets
A target is the resource a rule invokes when an event matches. This chapter assumes you can create a rule with put-rule and write an event pattern. Here you choose a target type, supply its settings, and attach it with put-targets.
How targets attach to a rule
A rule can have up to five targets. Each target needs an Id that is unique within that rule and an Arn for the resource. EventBridge sends a copy of every matching event to each target independently, so one slow or failing target does not block the others.
Common target types
| Target | Extra settings | How access is granted |
|---|---|---|
| Lambda function | None | Resource-based policy on the function |
| SQS queue | `SqsParameters.MessageGroupId` for FIFO queues | Resource-based policy on the queue |
| SNS topic | None | Resource-based policy on the topic |
| Step Functions state machine | None | `RoleArn` on the target |
| Kinesis stream | `KinesisParameters.PartitionKeyPath` | `RoleArn` on the target |
| ECS task | `EcsParameters` (task definition, network) | `RoleArn` on the target |
| Another event bus | None | `RoleArn` on the target |
Lambda, SQS, and SNS rely on a resource-based policy that allows events.amazonaws.com to invoke or send to the resource. The other types need a RoleArn, an IAM role that EventBridge assumes. The permissions chapter covers both in detail.
Attaching two targets
Save this as targets.json. The first target is a Lambda function that receives the full event. The second starts a Step Functions execution and passes a fixed JSON input instead of the event.
[
{
"Id": "process-order-fn",
"Arn": "arn:aws:lambda:us-east-1:123456789012:function:process-order"
},
{
"Id": "fulfillment-workflow",
"Arn": "arn:aws:states:us-east-1:123456789012:stateMachine:fulfillment",
"RoleArn": "arn:aws:iam::123456789012:role/eventbridge-start-fulfillment",
"Input": "{\"priority\": \"high\"}"
}
]Attach them to the rule on the custom bus:
aws events put-targets --rule high-value-orders --event-bus-name orders-bus --targets file://targets.jsonWithout --event-bus-name, the command looks for the rule on the default bus and fails with a ResourceNotFoundException.
Choosing what the target receives
By default, a target receives the whole matched event. You can change that with one of these fields, and only one of them can be set on a target:
Inputsends a constant JSON text and ignores the event.InputPathsends one part of the event, selected with a JSONPath such as$.detail.InputTransformerbuilds a new payload from selected fields. The next chapter covers it.
Reliability settings per target
Each target can carry its own RetryPolicy and DeadLetterConfig, as in the SQS example from the topic overview. These settings belong to the target, not the rule. If one target in a rule needs a dead-letter queue and another does not, configure them separately.
Verifying the setup
List what is attached with list-targets-by-rule:
aws events list-targets-by-rule --rule high-value-orders --event-bus-name orders-busTo remove a target, use remove-targets with the target Id. Removing a target does not delete the Lambda function, queue, or other resource it pointed to.
Common errors
- A target never receives events because the resource policy or role is missing. This produces failed invocations, not an error at
put-targetstime. - The
Idis reused on the same rule, soput-targetsoverwrites the earlier target. - A sixth target is added to a rule, which exceeds the limit.
- The target is in another Region. Most targets must be in the rule's Region unless you route through another event bus.
Exercise
A rule has a Lambda target and an SQS FIFO queue target. Events reach the Lambda function, but nothing reaches the queue, and put-targets reported no error at the call level. What should you check first?
Show answer
Check FailedEntryCount and the failure details in the put-targets response. A FIFO queue target requires SqsParameters.MessageGroupId, so a missing value is the likely cause. Also confirm the queue's resource policy allows events.amazonaws.com to send messages.