Skip to content

Commit a3a0cf0

Browse files
feat(events-targets): enable opt-in to use an IAM role for SnsTopic target (#34259)
### Issue # (if applicable) There's currently no issue for this, but I'd be happy to create it. ### Reason for this change Using an IAM role also fixes potential issues of failed published messages in case the target SNS topic is encrypted with KMS. According to this article, it's recommended to use execution roles for EventBridge targets. https://aws.amazon.com/about-aws/whats-new/2025/03/amazon-eventbridge-iam-execution-role-all-targets/ > We recommend configuring execution roles for all your EventBridge targets to benefit from consistent permissions policies and (...) ### Description of changes The `SnsTopic` target still has the same default behavior. However, it's now possible to opt-in to IAM role usage by setting the `authorizeUsingRole` property to `true`. It's also possible to provide a custom `role` as property. ### Describe any new or updated permissions being added The `sns:Publish` action is allowed on the topicArn in case an IAM role is used. ### Description of how you validated changes Unit tests were updated and added. There was already an integration test for the SnsTopic target without role usage. I've added two more integration tests: one where `authorizeUsingRole` is set to true, and one where a custom role is provided. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent ce88c76 commit a3a0cf0

21 files changed

+1621
-3
lines changed

packages/@aws-cdk-testing/framework-integ/test/aws-events-targets/test/sns/integ.sns-event-rule-target-custom-role.js.snapshot/aws-cdk-sns-event-target.assets.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
{
2+
"Resources": {
3+
"MyTopic86869434": {
4+
"Type": "AWS::SNS::Topic"
5+
},
6+
"EveryMinute2BBCEA8F": {
7+
"Type": "AWS::Events::Rule",
8+
"Properties": {
9+
"ScheduleExpression": "rate(1 minute)",
10+
"State": "ENABLED",
11+
"Targets": [
12+
{
13+
"Arn": {
14+
"Ref": "MyTopic86869434"
15+
},
16+
"DeadLetterConfig": {
17+
"Arn": {
18+
"Fn::GetAtt": [
19+
"MyDeadLetterQueueD997968A",
20+
"Arn"
21+
]
22+
}
23+
},
24+
"Id": "Target0",
25+
"RoleArn": {
26+
"Fn::GetAtt": [
27+
"MyRoleF48FFE04",
28+
"Arn"
29+
]
30+
}
31+
}
32+
]
33+
}
34+
},
35+
"MyQueueE6CA6235": {
36+
"Type": "AWS::SQS::Queue",
37+
"UpdateReplacePolicy": "Delete",
38+
"DeletionPolicy": "Delete"
39+
},
40+
"MyQueuePolicy6BBEDDAC": {
41+
"Type": "AWS::SQS::QueuePolicy",
42+
"Properties": {
43+
"PolicyDocument": {
44+
"Statement": [
45+
{
46+
"Action": "sqs:SendMessage",
47+
"Condition": {
48+
"ArnEquals": {
49+
"aws:SourceArn": {
50+
"Ref": "MyTopic86869434"
51+
}
52+
}
53+
},
54+
"Effect": "Allow",
55+
"Principal": {
56+
"Service": "sns.amazonaws.com"
57+
},
58+
"Resource": {
59+
"Fn::GetAtt": [
60+
"MyQueueE6CA6235",
61+
"Arn"
62+
]
63+
}
64+
}
65+
],
66+
"Version": "2012-10-17"
67+
},
68+
"Queues": [
69+
{
70+
"Ref": "MyQueueE6CA6235"
71+
}
72+
]
73+
}
74+
},
75+
"MyQueueawscdksnseventtargetMyTopicB7575CD87304D383": {
76+
"Type": "AWS::SNS::Subscription",
77+
"Properties": {
78+
"Endpoint": {
79+
"Fn::GetAtt": [
80+
"MyQueueE6CA6235",
81+
"Arn"
82+
]
83+
},
84+
"Protocol": "sqs",
85+
"TopicArn": {
86+
"Ref": "MyTopic86869434"
87+
}
88+
},
89+
"DependsOn": [
90+
"MyQueuePolicy6BBEDDAC"
91+
]
92+
},
93+
"MyDeadLetterQueueD997968A": {
94+
"Type": "AWS::SQS::Queue",
95+
"UpdateReplacePolicy": "Delete",
96+
"DeletionPolicy": "Delete"
97+
},
98+
"MyDeadLetterQueuePolicyCC35D52C": {
99+
"Type": "AWS::SQS::QueuePolicy",
100+
"Properties": {
101+
"PolicyDocument": {
102+
"Statement": [
103+
{
104+
"Action": "sqs:SendMessage",
105+
"Condition": {
106+
"ArnEquals": {
107+
"aws:SourceArn": {
108+
"Fn::GetAtt": [
109+
"EveryMinute2BBCEA8F",
110+
"Arn"
111+
]
112+
}
113+
}
114+
},
115+
"Effect": "Allow",
116+
"Principal": {
117+
"Service": "events.amazonaws.com"
118+
},
119+
"Resource": {
120+
"Fn::GetAtt": [
121+
"MyDeadLetterQueueD997968A",
122+
"Arn"
123+
]
124+
},
125+
"Sid": "AllowEventRuleawscdksnseventtargetEveryMinuteD1FC5963"
126+
}
127+
],
128+
"Version": "2012-10-17"
129+
},
130+
"Queues": [
131+
{
132+
"Ref": "MyDeadLetterQueueD997968A"
133+
}
134+
]
135+
}
136+
},
137+
"MyRoleF48FFE04": {
138+
"Type": "AWS::IAM::Role",
139+
"Properties": {
140+
"AssumeRolePolicyDocument": {
141+
"Statement": [
142+
{
143+
"Action": "sts:AssumeRole",
144+
"Effect": "Allow",
145+
"Principal": {
146+
"Service": "events.amazonaws.com"
147+
}
148+
}
149+
],
150+
"Version": "2012-10-17"
151+
}
152+
}
153+
},
154+
"MyRoleDefaultPolicyA36BE1DD": {
155+
"Type": "AWS::IAM::Policy",
156+
"Properties": {
157+
"PolicyDocument": {
158+
"Statement": [
159+
{
160+
"Action": "sns:Publish",
161+
"Effect": "Allow",
162+
"Resource": {
163+
"Ref": "MyTopic86869434"
164+
}
165+
}
166+
],
167+
"Version": "2012-10-17"
168+
},
169+
"PolicyName": "MyRoleDefaultPolicyA36BE1DD",
170+
"Roles": [
171+
{
172+
"Ref": "MyRoleF48FFE04"
173+
}
174+
]
175+
}
176+
}
177+
},
178+
"Parameters": {
179+
"BootstrapVersion": {
180+
"Type": "AWS::SSM::Parameter::Value<String>",
181+
"Default": "/cdk-bootstrap/hnb659fds/version",
182+
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
183+
}
184+
},
185+
"Rules": {
186+
"CheckBootstrapVersion": {
187+
"Assertions": [
188+
{
189+
"Assert": {
190+
"Fn::Not": [
191+
{
192+
"Fn::Contains": [
193+
[
194+
"1",
195+
"2",
196+
"3",
197+
"4",
198+
"5"
199+
],
200+
{
201+
"Ref": "BootstrapVersion"
202+
}
203+
]
204+
}
205+
]
206+
},
207+
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
208+
}
209+
]
210+
}
211+
}
212+
}

packages/@aws-cdk-testing/framework-integ/test/aws-events-targets/test/sns/integ.sns-event-rule-target-custom-role.js.snapshot/cdk.out

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk-testing/framework-integ/test/aws-events-targets/test/sns/integ.sns-event-rule-target-custom-role.js.snapshot/integ.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)