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_subnet: Handle read-after-create eventual consistency #18392

Merged
merged 2 commits into from
Mar 25, 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
3 changes: 3 additions & 0 deletions .changelog/18392.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_subnet: Handle EC2 eventual consistency errors on creation
```
1 change: 1 addition & 0 deletions aws/internal/service/ec2/waiter/waiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ func SecurityGroupCreated(conn *ec2.EC2, id string, timeout time.Duration) (*ec2
}

const (
SubnetPropagationTimeout = 2 * time.Minute
SubnetAttributePropagationTimeout = 5 * time.Minute
)

Expand Down
55 changes: 44 additions & 11 deletions aws/resource_aws_subnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"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/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/ec2/finder"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/ec2/waiter"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource"
)

func resourceAwsSubnet() *schema.Resource {
Expand Down Expand Up @@ -232,23 +235,53 @@ func resourceAwsSubnetRead(d *schema.ResourceData, meta interface{}) error {
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

resp, err := conn.DescribeSubnets(&ec2.DescribeSubnetsInput{
SubnetIds: []*string{aws.String(d.Id())},
})
var subnet *ec2.Subnet

if err != nil {
if isAWSErr(err, "InvalidSubnetID.NotFound", "") {
log.Printf("[WARN] Subnet (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
err := resource.Retry(waiter.SubnetPropagationTimeout, func() *resource.RetryError {
var err error

subnet, err = finder.SubnetByID(conn, d.Id())

if d.IsNewResource() && tfawserr.ErrCodeEquals(err, "InvalidSubnetID.NotFound") {
return resource.RetryableError(err)
}
return err

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

if d.IsNewResource() && subnet == nil {
return resource.RetryableError(&resource.NotFoundError{
LastError: fmt.Errorf("EC2 Subnet (%s) not found", d.Id()),
})
}

return nil
})

if tfresource.TimedOut(err) {
subnet, err = finder.SubnetByID(conn, d.Id())
}
if resp == nil {

if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, "InvalidSubnetID.NotFound") {
log.Printf("[WARN] EC2 Subnet (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

subnet := resp.Subnets[0]
if err != nil {
return fmt.Errorf("error reading EC2 Subnet (%s): %w", d.Id(), err)
}

if subnet == nil {
if d.IsNewResource() {
return fmt.Errorf("error reading EC2 Subnet (%s): not found after creation", d.Id())
}

log.Printf("[WARN] EC2 Subnet (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
Comment on lines +245 to +284
Copy link
Contributor

Choose a reason for hiding this comment

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

If finder.SubnetByID() returns a resource.NotFoundError when the subnet is not found, we'd have to do a lot less checking for different not found cases at this level.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In general, this is likely a good move, but it is not the current standard, I am not immediately sure of all the implications, and I don't feel comfortable implementing it my own without discussing it first. I have created two tracking issues for following up to this:


d.Set("vpc_id", subnet.VpcId)
d.Set("availability_zone", subnet.AvailabilityZone)
Expand Down