Skip to content
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

resource/aws_sns_topic_subscription: Handle read-after-create eventual consistency, enforce lowercase protocol argument validation #18475

Merged
merged 2 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changelog/18475.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:bug
resource/aws_sns_topic_subscription: Handle read-after-create eventual consistency
```

```release-note:bug
resource/aws_sns_topic_subscription: Enforce lowercase `protocol` argument validation to match API and prevent resource errors
```
1 change: 1 addition & 0 deletions aws/internal/service/sns/waiter/waiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

const (
SubscriptionCreateTimeout = 2 * time.Minute
SubscriptionPendingConfirmationTimeout = 2 * time.Minute
SubscriptionDeleteTimeout = 2 * time.Minute
)
Expand Down
39 changes: 27 additions & 12 deletions aws/resource_aws_sns_topic_subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ import (
"github.com/aws/aws-sdk-go/aws/awsutil"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/structure"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/sns/finder"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/sns/waiter"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource"
)

func resourceAwsSnsTopicSubscription() *schema.Resource {
Expand Down Expand Up @@ -91,7 +93,7 @@ func resourceAwsSnsTopicSubscription() *schema.Resource {
"lambda",
"sms",
"sqs",
}, true),
}, false),
},
"raw_message_delivery": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -169,27 +171,40 @@ func resourceAwsSnsTopicSubscriptionCreate(d *schema.ResourceData, meta interfac
func resourceAwsSnsTopicSubscriptionRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).snsconn

log.Printf("[DEBUG] Loading subscription %s", d.Id())
var output *sns.GetSubscriptionAttributesOutput

input := &sns.ListSubscriptionsByTopicInput{
TopicArn: aws.String(d.Get("topic_arn").(string)),
}
err := resource.Retry(waiter.SubscriptionCreateTimeout, func() *resource.RetryError {
var err error

_, err := conn.ListSubscriptionsByTopic(input)
output, err = finder.SubscriptionByARN(conn, d.Id())

if err != nil {
return resource.NonRetryableError(err)
}

if d.IsNewResource() && output == nil {
return resource.RetryableError(&resource.NotFoundError{
LastError: fmt.Errorf("SNS Topic Subscription Attributes (%s) not found", d.Id()),
})
}

if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, sns.ErrCodeNotFoundException) {
log.Printf("[WARN] SNS Topic Subscription (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
})

if tfresource.TimedOut(err) {
output, err = finder.SubscriptionByARN(conn, d.Id())
}

output, err := finder.SubscriptionByARN(conn, d.Id())
if err != nil {
return fmt.Errorf("getting SNS subscription attributes (%s): %w", d.Id(), err)
return fmt.Errorf("error getting SNS Topic Subscription Attributes (%s): %w", d.Id(), err)
}

if output == nil {
log.Printf("[WARN] SNS subscription attributes (%s) not found, removing from state", d.Id())
if d.IsNewResource() {
return fmt.Errorf("error getting SNS Topic Subscription Attributes (%s): not found after creation", d.Id())
}

log.Printf("[WARN] SNS Topic Subscription Attributes (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
Expand Down