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_eip_association: fix eventual consistency issue when associating EIP #16808

Merged
merged 2 commits into from
Dec 18, 2020
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 aws/internal/service/ec2/waiter/waiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
const (
// Maximum amount of time to wait for EC2 Instance attribute modifications to propagate
InstanceAttributePropagationTimeout = 2 * time.Minute

// General timeout for EC2 resource creations to propagate
PropagationTimeout = 2 * time.Minute
)

const (
Expand Down
35 changes: 32 additions & 3 deletions aws/resource_aws_eip_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"fmt"
"log"
"net"
"time"

"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/service/ec2/waiter"
)

func resourceAwsEipAssociation() *schema.Resource {
Expand Down Expand Up @@ -94,7 +94,7 @@ func resourceAwsEipAssociationCreate(d *schema.ResourceData, meta interface{}) e
log.Printf("[DEBUG] EIP association configuration: %#v", request)

var resp *ec2.AssociateAddressOutput
err := resource.Retry(2*time.Minute, func() *resource.RetryError {
err := resource.Retry(waiter.PropagationTimeout, func() *resource.RetryError {
var err error
resp, err = conn.AssociateAddress(request)

Expand Down Expand Up @@ -157,7 +157,36 @@ func resourceAwsEipAssociationRead(d *schema.ResourceData, meta interface{}) err
return err
}

response, err := conn.DescribeAddresses(request)
var response *ec2.DescribeAddressesOutput
err = resource.Retry(waiter.PropagationTimeout, func() *resource.RetryError {
var err error
response, err = conn.DescribeAddresses(request)

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

if d.IsNewResource() && (response.Addresses == nil || len(response.Addresses) == 0) {
return resource.RetryableError(&resource.NotFoundError{})
}

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

return nil
})

if isResourceTimeoutError(err) {
response, err = conn.DescribeAddresses(request)
}
dohoangkhiem marked this conversation as resolved.
Show resolved Hide resolved

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

if err != nil {
return fmt.Errorf("Error reading EC2 Elastic IP %s: %#v", d.Get("allocation_id").(string), err)
}
Expand Down