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_instance: Automatically retry instance restart on eventual consistency error during instance_type in-place update #16443

Merged
merged 1 commit into from
Dec 3, 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
4 changes: 4 additions & 0 deletions aws/internal/service/ec2/errors.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package ec2

const (
ErrCodeInvalidParameterValue = "InvalidParameterValue"
)

const (
ErrCodeClientVpnEndpointIdNotFound = "InvalidClientVpnEndpointId.NotFound"
ErrCodeClientVpnAuthorizationRuleNotFound = "InvalidClientVpnEndpointAuthorizationRuleNotFound"
Expand Down
5 changes: 5 additions & 0 deletions aws/internal/service/ec2/waiter/waiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

const (
// Maximum amount of time to wait for EC2 Instance attribute modifications to propagate
InstanceAttributePropagationTimeout = 2 * time.Minute
)

const (
// Maximum amount of time to wait for a LocalGatewayRouteTableVpcAssociation to return Associated
LocalGatewayRouteTableVpcAssociationAssociatedTimeout = 5 * time.Minute
Expand Down
29 changes: 27 additions & 2 deletions aws/resource_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"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/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/hashcode"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
tfec2 "github.com/terraform-providers/terraform-provider-aws/aws/internal/service/ec2"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/ec2/waiter"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/tfresource"
)

func resourceAwsInstance() *schema.Resource {
Expand Down Expand Up @@ -1228,11 +1232,32 @@ func resourceAwsInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
}

log.Printf("[INFO] Starting Instance %q after instance_type change", d.Id())
_, err = conn.StartInstances(&ec2.StartInstancesInput{

input := &ec2.StartInstancesInput{
InstanceIds: []*string{aws.String(d.Id())},
}

// Reference: https://github.com/hashicorp/terraform-provider-aws/issues/16433
err = resource.Retry(waiter.InstanceAttributePropagationTimeout, func() *resource.RetryError {
_, err := conn.StartInstances(input)

if tfawserr.ErrMessageContains(err, tfec2.ErrCodeInvalidParameterValue, "LaunchPlan instance type does not match attribute value") {
return resource.RetryableError(err)
}

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

return nil
})

if tfresource.TimedOut(err) {
_, err = conn.StartInstances(input)
}

if err != nil {
return fmt.Errorf("error starting instance (%s): %s", d.Id(), err)
return fmt.Errorf("error starting EC2 Instance (%s): %w", d.Id(), err)
}

stateConf := &resource.StateChangeConf{
Expand Down