Sub topic 9
Permissions: resource policies and execution roles
This chapter assumes you can read IAM policy JSON, know what an IAM role and a trust policy are, and have created an EventBridge rule with a target. It covers who is allowed to call EventBridge, and what EventBridge is allowed to call.
Two directions of permission
EventBridge permissions run in two directions.
- Inbound: who can send events to a bus or manage its rules. An identity-based policy on the caller or a resource-based policy on the bus controls this.
- Outbound: what a rule is allowed to do when it delivers an event to a target. An execution role or a resource-based policy on the target controls this.
Most "my rule matches but nothing happens" problems come from the outbound side.
Outbound: how a rule gets permission to call a target
How you grant access depends on the target type.
| Target type | Mechanism |
|---|---|
| Lambda, SNS, SQS, CloudWatch Logs | Resource-based policy on the target. The principal is `events.amazonaws.com`. |
| Step Functions, Kinesis, ECS, API destinations, another bus | IAM execution role that the rule assumes. |
Resource policy on the target
For an SQS queue, add a statement that lets EventBridge send messages, limited to your rule with aws:SourceArn:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowEventBridgeRule",
"Effect": "Allow",
"Principal": { "Service": "events.amazonaws.com" },
"Action": "sqs:SendMessage",
"Resource": "arn:aws:sqs:us-east-1:123456789012:order-alerts",
"Condition": {
"ArnEquals": {
"aws:SourceArn": "arn:aws:events:us-east-1:123456789012:rule/orders-bus/high-value-orders"
}
}
}
]
}The same pattern works for a Lambda function. Use aws lambda add-permission with --principal events.amazonaws.com and --source-arn set to the rule ARN. If you create the target in the console, this policy is added for you. With the CLI or infrastructure as code, you must add it yourself.
Execution role
For targets such as Step Functions, create a role that EventBridge can assume. The trust policy names the EventBridge service:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "events.amazonaws.com" },
"Action": "sts:AssumeRole"
}
]
}Then attach a permissions policy that allows only the needed action, for example states:StartExecution on one state machine. Pass the role's ARN in the RoleArn field of the target:
[
{
"Id": "start-fulfilment",
"Arn": "arn:aws:states:us-east-1:123456789012:stateMachine:fulfilment",
"RoleArn": "arn:aws:iam::123456789012:role/eventbridge-start-fulfilment"
}
]Inbound: controlling who can use the bus
A caller needs events:PutEvents on the bus ARN. For callers in your own account, an IAM policy is enough. For another account, the bus needs a resource-based policy that names that account. Cross-account delivery builds on this and is covered in its own chapter.
A bus policy that lets one account publish looks like this:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowPartnerAccountPut",
"Effect": "Allow",
"Principal": { "AWS": "111122223333" },
"Action": "events:PutEvents",
"Resource": "arn:aws:events:us-east-1:123456789012:event-bus/orders-bus"
}
]
}Attach it with aws events put-permission --event-bus-name orders-bus --policy file://bus-policy.json.
Common errors
- Rule matches but the target never runs. Check the
TriggeredRulesandFailedInvocationsmetrics. A missing target policy or execution role is the usual cause, and the failed event goes to the dead-letter queue if you configured one. - Role exists but is not assumed. The trust policy must name
events.amazonaws.com. A trust policy that nameslambda.amazonaws.comor your own account does not work. - Policy is too broad. A
Principalof*without aConditionlets any rule or account invoke the target. Always addaws:SourceArnoraws:SourceAccount. - The user running
put-targetscannot attach the role. That user needsiam:PassRoleon the execution role.
Exercise
Exercise
You attach an SQS queue as a target of a rule using the CLI. The rule matches events, but no messages arrive and FailedInvocations increases. What is the most likely cause, and how do you fix it?
Show answer
The queue has no resource-based policy that lets events.amazonaws.com call sqs:SendMessage, because the CLI does not add one automatically. Add a statement to the queue policy that allows sqs:SendMessage for the principal events.amazonaws.com, with an aws:SourceArn condition set to the rule's ARN.