Skip to content

r/aws_sns_topic_subscription: handle eventually consistent subscription deletion #42093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2025

Conversation

jar-b
Copy link
Member

@jar-b jar-b commented Apr 2, 2025

Description

Previously the Read operation relied solely on the GetSubscriptionAttributes API, which is eventually consistent and may not correctly identify scenarios when the topic or subscription is deleted out of band. This change adds a call to the ListSubscriptionsByTopic API prior to the existing read logic to ensure both the topic and subscription are currently active. When a NotFound error is returned or the subscription is not in the list of returned subscriptions, the resource is removed from state and a re-creation will be triggered on the next apply.

Relations

Closes #41984
Relates #9645 (comment)
Relates #14101 - the ListSubscriptionsByTopic call was added to originally address this in v3.34.0
Relates #18475 - the ListSubscriptionsByTopic call was removed in v3.35.0

Output from Acceptance Testing

% make testacc PKG=sns TESTS=TestAccSNSTopicSubscription_
make: Verifying source code with gofmt...
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go1.23.7 test ./internal/service/sns/... -v -count 1 -parallel 20 -run='TestAccSNSTopicSubscription_'  -timeout 360m -vet=off
2025/04/02 10:45:34 Initializing Terraform AWS Provider...

--- PASS: TestAccSNSTopicSubscription_filterPolicyScope_policyNotSet (3.77s)
--- PASS: TestAccSNSTopicSubscription_email (17.52s)
--- PASS: TestAccSNSTopicSubscription_autoConfirmingEndpoint (42.54s)
--- PASS: TestAccSNSTopicSubscription_disappears_TopicExternal (50.90s)
--- PASS: TestAccSNSTopicSubscription_disappears (51.40s)
--- PASS: TestAccSNSTopicSubscription_disappears_Topic (51.53s)
--- PASS: TestAccSNSTopicSubscription_basic (51.89s)
--- PASS: TestAccSNSTopicSubscription_deliveryPolicy (72.32s)
--- PASS: TestAccSNSTopicSubscription_rawMessageDelivery (72.41s)
--- PASS: TestAccSNSTopicSubscription_filterPolicy (73.24s)
--- PASS: TestAccSNSTopicSubscription_firehose (75.93s)
--- PASS: TestAccSNSTopicSubscription_autoConfirmingSecuredEndpoint (85.98s)
--- PASS: TestAccSNSTopicSubscription_redrivePolicy (106.94s)
--- PASS: TestAccSNSTopicSubscription_filterPolicyScope (154.09s)
PASS
ok      github.com/hashicorp/terraform-provider-aws/internal/service/sns        160.822s

Copy link

github-actions bot commented Apr 2, 2025

Community Guidelines

This comment is added to every new Pull Request to provide quick reference to how the Terraform AWS Provider is maintained. Please review the information below, and thank you for contributing to the community that keeps the provider thriving! 🚀

Voting for Prioritization

  • Please vote on this Pull Request by adding a 👍 reaction to the original post to help the community and maintainers prioritize it.
  • Please see our prioritization guide for additional information on how the maintainers handle prioritization.
  • Please do not leave +1 or other comments that do not add relevant new information or questions; they generate extra noise for others following the Pull Request and do not help prioritize the request.

Pull Request Authors

  • Review the contribution guide relating to the type of change you are making to ensure all of the necessary steps have been taken.
  • Whether or not the branch has been rebased will not impact prioritization, but doing so is always a welcome surprise.

@github-actions github-actions bot added tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure. service/sns Issues and PRs that pertain to the sns service. labels Apr 2, 2025
@jar-b jar-b added the bug Addresses a defect in current functionality. label Apr 2, 2025
@github-actions github-actions bot added size/L Managed by automation to categorize the size of a PR. prioritized Part of the maintainer teams immediate focus. To be addressed within the current quarter. labels Apr 2, 2025
@jar-b jar-b added the regression Pertains to a degraded workflow resulting from an upstream patch or internal enhancement. label Apr 2, 2025
@jar-b jar-b force-pushed the b-sns_subscription-topic-disappears branch 2 times, most recently from 17895e5 to 1a593fa Compare April 2, 2025 17:27
…on deletion

Previously the `Read` operation relied solely on the `GetSubscriptionAttributes` API, which is eventually consistent and may not correctly identify scenarios when the topic or subscription is deleted out of band. This change adds a call to the `ListSubscriptionsByTopic` API prior to the existing read logic to ensure both the topic and subscription are currently active. When a `NotFound` error is returned or the subscription is not in the list of returned subscriptions, the resource is removed from state and a re-creation will be triggered on the next `apply`.

```console
% make testacc PKG=sns TESTS=TestAccSNSTopicSubscription_
make: Verifying source code with gofmt...
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go1.23.7 test ./internal/service/sns/... -v -count 1 -parallel 20 -run='TestAccSNSTopicSubscription_'  -timeout 360m -vet=off
2025/04/02 10:45:34 Initializing Terraform AWS Provider...

--- PASS: TestAccSNSTopicSubscription_filterPolicyScope_policyNotSet (3.77s)
--- PASS: TestAccSNSTopicSubscription_email (17.52s)
--- PASS: TestAccSNSTopicSubscription_autoConfirmingEndpoint (42.54s)
--- PASS: TestAccSNSTopicSubscription_disappears_TopicExternal (50.90s)
--- PASS: TestAccSNSTopicSubscription_disappears (51.40s)
--- PASS: TestAccSNSTopicSubscription_disappears_Topic (51.53s)
--- PASS: TestAccSNSTopicSubscription_basic (51.89s)
--- PASS: TestAccSNSTopicSubscription_deliveryPolicy (72.32s)
--- PASS: TestAccSNSTopicSubscription_rawMessageDelivery (72.41s)
--- PASS: TestAccSNSTopicSubscription_filterPolicy (73.24s)
--- PASS: TestAccSNSTopicSubscription_firehose (75.93s)
--- PASS: TestAccSNSTopicSubscription_autoConfirmingSecuredEndpoint (85.98s)
--- PASS: TestAccSNSTopicSubscription_redrivePolicy (106.94s)
--- PASS: TestAccSNSTopicSubscription_filterPolicyScope (154.09s)
PASS
ok      github.com/hashicorp/terraform-provider-aws/internal/service/sns        160.822s
```
@jar-b jar-b force-pushed the b-sns_subscription-topic-disappears branch from 1a593fa to d40c537 Compare April 2, 2025 17:47
@jar-b jar-b marked this pull request as ready for review April 2, 2025 18:23
@jar-b jar-b requested a review from a team as a code owner April 2, 2025 18:23
Copy link
Contributor

@ewbankkit ewbankkit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀.

@jar-b jar-b merged commit e8201a7 into main Apr 2, 2025
37 checks passed
@jar-b jar-b deleted the b-sns_subscription-topic-disappears branch April 2, 2025 18:56
Copy link

github-actions bot commented Apr 2, 2025

Warning

This Issue has been closed, meaning that any additional comments are much easier for the maintainers to miss. Please assume that the maintainers will not see them.

Ongoing conversations amongst community members are welcome, however, the issue will be locked after 30 days. Moving conversations to another venue, such as the AWS Provider forum, is recommended. If you have additional concerns, please open a new issue, referencing this one where needed.

@github-actions github-actions bot added this to the v5.94.0 milestone Apr 2, 2025
terraform-aws-provider bot pushed a commit that referenced this pull request Apr 2, 2025
Copy link

github-actions bot commented Apr 3, 2025

This functionality has been released in v5.94.0 of the Terraform AWS Provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

@github-actions github-actions bot removed the prioritized Part of the maintainer teams immediate focus. To be addressed within the current quarter. label Apr 3, 2025
@svenfinke
Copy link

svenfinke commented Apr 4, 2025

This requires the extension of existing access policies of remote Topics. This can cause massive issues if you are not in control of the target topic and can't adjust the permissions.

Instead of ListSubscriptionsByTopic, use ListSubscriptions instead and search for the topic. This won't require additional permissions in a different AWS account.

Copy link

github-actions bot commented May 5, 2025

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 5, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Addresses a defect in current functionality. regression Pertains to a degraded workflow resulting from an upstream patch or internal enhancement. service/sns Issues and PRs that pertain to the sns service. size/L Managed by automation to categorize the size of a PR. tests PRs: expanded test coverage. Issues: expanded coverage, enhancements to test infrastructure.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: aws_sns_topic_subscription doesn't use ListSubscriptionsByTopic to make sure the subscription still exists
3 participants