Skip to content

Commit f885f52

Browse files
authored
Merge pull request #42623 from hashicorp/b-aws_instance.RunInstances-retry
r/aws_instance: Improve retry for `RunInstances`
2 parents b6e96c4 + 06b5f83 commit f885f52

File tree

3 files changed

+22
-22
lines changed

3 files changed

+22
-22
lines changed

.changelog/42623.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
resource/aws_instance: Fix `InvalidNetworkInterface.InUse` errors on Create
3+
```

internal/service/ec2/ec2_instance.go

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
3232
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
3333
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
34+
"github.com/hashicorp/terraform-provider-aws/internal/backoff"
3435
"github.com/hashicorp/terraform-provider-aws/internal/conns"
3536
"github.com/hashicorp/terraform-provider-aws/internal/create"
3637
"github.com/hashicorp/terraform-provider-aws/internal/enum"
@@ -1049,34 +1050,30 @@ func resourceInstanceCreate(ctx context.Context, d *schema.ResourceData, meta an
10491050
input.DisableApiStop = instanceOpts.DisableAPIStop
10501051
}
10511052

1052-
log.Printf("[DEBUG] Creating EC2 Instance: %s", d.Id())
1053-
outputRaw, err := tfresource.RetryWhen(ctx, iamPropagationTimeout,
1054-
func() (any, error) {
1055-
return conn.RunInstances(ctx, &input)
1056-
},
1057-
func(err error) (bool, error) {
1058-
// IAM instance profiles can take ~10 seconds to propagate in AWS:
1059-
// http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html#launch-instance-with-role-console
1060-
if tfawserr.ErrMessageContains(err, errCodeInvalidParameterValue, "Invalid IAM Instance Profile") {
1061-
return true, err
1062-
}
1053+
var output *ec2.RunInstancesOutput
1054+
for r := backoff.NewRetryLoop(iamPropagationTimeout); r.Continue(ctx); {
1055+
output, err = conn.RunInstances(ctx, &input)
10631056

1064-
// IAM roles can also take time to propagate in AWS:
1065-
if tfawserr.ErrMessageContains(err, errCodeInvalidParameterValue, " has no associated IAM Roles") {
1066-
return true, err
1067-
}
1057+
// IAM instance profiles can take ~10 seconds to propagate in AWS:
1058+
// http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html#launch-instance-with-role-console
1059+
if tfawserr.ErrMessageContains(err, errCodeInvalidParameterValue, "Invalid IAM Instance Profile") {
1060+
continue
1061+
}
10681062

1069-
return false, err
1070-
},
1071-
)
1063+
// IAM roles can also take time to propagate in AWS:
1064+
if tfawserr.ErrMessageContains(err, errCodeInvalidParameterValue, " has no associated IAM Roles") {
1065+
continue
1066+
}
1067+
1068+
break
1069+
}
10721070

10731071
if err != nil {
10741072
return sdkdiag.AppendErrorf(diags, "creating EC2 Instance: %s", err)
10751073
}
10761074

1077-
instanceId := outputRaw.(*ec2.RunInstancesOutput).Instances[0].InstanceId
1078-
1079-
d.SetId(aws.ToString(instanceId))
1075+
instanceID := output.Instances[0].InstanceId
1076+
d.SetId(aws.ToString(instanceID))
10801077

10811078
instance, err := waitInstanceCreated(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate))
10821079

internal/service/ec2/ec2_instance_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4769,7 +4769,7 @@ func TestAccEC2Instance_CreditSpecificationUnknownCPUCredits_t4g(t *testing.T) {
47694769
Check: resource.ComposeTestCheckFunc(
47704770
testAccCheckInstanceExists(ctx, resourceName, &v),
47714771
resource.TestCheckResourceAttr(resourceName, "credit_specification.#", "1"),
4772-
resource.TestCheckResourceAttr(resourceName, "credit_specification.0.cpu_credits", "unlimited"),
4772+
resource.TestCheckResourceAttr(resourceName, "credit_specification.0.cpu_credits", "standard"),
47734773
),
47744774
},
47754775
{

0 commit comments

Comments
 (0)