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_elb_attachment: Retry ELB attachment on InvalidTarget error #8483

Merged
merged 2 commits into from
May 2, 2019
Merged
Changes from 1 commit
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
18 changes: 17 additions & 1 deletion aws/resource_aws_elb_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package aws
import (
"fmt"
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
jbarrick-mesosphere marked this conversation as resolved.
Show resolved Hide resolved
"github.com/aws/aws-sdk-go/service/elb"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -45,7 +47,21 @@ func resourceAwsElbAttachmentCreate(d *schema.ResourceData, meta interface{}) er

log.Printf("[INFO] registering instance %s with ELB %s", instance, elbName)

_, err := elbconn.RegisterInstancesWithLoadBalancer(&registerInstancesOpts)
err := resource.Retry(600*time.Second, func() *resource.RetryError {
jbarrick-mesosphere marked this conversation as resolved.
Show resolved Hide resolved
_, err := elbconn.RegisterInstancesWithLoadBalancer(&registerInstancesOpts)

if err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

There is a pattern we use throughout the code base to help when checking against AWS errors. The non-nested error checks help readability and the helper isAWSErr wraps all the type assertion logic.

err := resource.Retry(10*time.Minute, func() *resource.RetryError {                                                                                                                         
    _, err := elbconn.RegisterInstancesWithLoadBalancer(&registerInstancesOpts)                                                                                                               
                                                                                                                                                                                              
    if isAWSErr(err, "InvalidTarget", "") {                                                                                                                                                   
      return resource.RetryableError(fmt.Errorf("Error attaching instance to ELB, retrying: %s", err))                                                                                        
    }                                                                                                                                                                                         
                                                                                                                                                                                              
    if err != nil {                                                                                                                                                                           
      return resource.NonRetryableError(err)                                                                                                                                                  
    }                                                                                                                                                                                         
                                                                                                                                                                                              
    return nil                                                                                                                                                                                
})   

if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "InvalidTarget" {
return resource.RetryableError(
fmt.Errorf("Error attaching instance to ELB, retrying: %s", err))
}
}
return resource.NonRetryableError(err)
}
return nil
})

if err != nil {
return fmt.Errorf("Failure registering instances with ELB: %s", err)
}
Expand Down