Skip to content

Commit cf43b03

Browse files
fix(sns): race condition exists between sqs queue policy and sns subscription (aws#21797)
---- Fixes aws#20665 by adding a dependency to sqs policy for sns subscriptions. ### All Submissions: * [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) ### Adding new Unconventional Dependencies: * [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies) ### New Features * [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)? * [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)? *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* This is a follow up to aws#21259, which got closed for failing for too long
1 parent a006b9a commit cf43b03

File tree

9 files changed

+1554
-186
lines changed

9 files changed

+1554
-186
lines changed

packages/@aws-cdk-containers/ecs-service-extensions/test/publish-subscribe.integ.snapshot/aws-ecs-integ.template.json

+1,343
Large diffs are not rendered by default.

packages/@aws-cdk/aws-cloudformation/test/nested-stack.integ.snapshot/nested-stacks-test.template.json

+3
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
},
9292
"SubscriberQueuenestedstackstestNestedStack1topic089C5EB1396F65087": {
9393
"Type": "AWS::SNS::Subscription",
94+
"DependsOn": "SubscriberQueuePolicy25A0799E",
9495
"Properties": {
9596
"Protocol": "sqs",
9697
"TopicArn": {
@@ -109,6 +110,7 @@
109110
},
110111
"SubscriberQueuenestedstackstestNestedStack1topic1150E1A929A2C267E": {
111112
"Type": "AWS::SNS::Subscription",
113+
"DependsOn": "SubscriberQueuePolicy25A0799E",
112114
"Properties": {
113115
"Protocol": "sqs",
114116
"TopicArn": {
@@ -127,6 +129,7 @@
127129
},
128130
"SubscriberQueuenestedstackstestNestedStack1topic209B8719858511914": {
129131
"Type": "AWS::SNS::Subscription",
132+
"DependsOn": "SubscriberQueuePolicy25A0799E",
130133
"Properties": {
131134
"Protocol": "sqs",
132135
"TopicArn": {

packages/@aws-cdk/aws-events-targets/test/codebuild/project-events.integ.snapshot/aws-cdk-codebuild-events.template.json

+1
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@
396396
},
397397
"MyQueueawscdkcodebuildeventsMyTopic550011DCF72DE3ED": {
398398
"Type": "AWS::SNS::Subscription",
399+
"DependsOn": "MyQueuePolicy6BBEDDAC",
399400
"Properties": {
400401
"Protocol": "sqs",
401402
"TopicArn": {

packages/@aws-cdk/aws-events-targets/test/sns/sns-event-rule-target.integ.snapshot/aws-cdk-sns-event-target.template.json

+1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
},
9595
"MyQueueawscdksnseventtargetMyTopicB7575CD87304D383": {
9696
"Type": "AWS::SNS::Subscription",
97+
"DependsOn": "MyQueuePolicy6BBEDDAC",
9798
"Properties": {
9899
"Protocol": "sqs",
99100
"TopicArn": {

packages/@aws-cdk/aws-sns-subscriptions/lib/sqs.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ export class SqsSubscription implements sns.ITopicSubscription {
4040

4141
// add a statement to the queue resource policy which allows this topic
4242
// to send messages to the queue.
43-
this.queue.addToResourcePolicy(new iam.PolicyStatement({
43+
const queuePolicyDependable = this.queue.addToResourcePolicy(new iam.PolicyStatement({
4444
resources: [this.queue.queueArn],
4545
actions: ['sqs:SendMessage'],
4646
principals: [snsServicePrincipal],
4747
conditions: {
4848
ArnEquals: { 'aws:SourceArn': topic.topicArn },
4949
},
50-
}));
50+
})).policyDependable;
5151

5252
// if the queue is encrypted, add a statement to the key resource policy
5353
// which allows this topic to decrypt KMS keys
@@ -77,6 +77,7 @@ export class SqsSubscription implements sns.ITopicSubscription {
7777
filterPolicy: this.props.filterPolicy,
7878
region: this.regionFromArn(topic),
7979
deadLetterQueue: this.props.deadLetterQueue,
80+
subscriptionDependency: queuePolicyDependable,
8081
};
8182
}
8283

0 commit comments

Comments
 (0)