From 57082d1711e77ceb0aa07bd661537b53f9bdfc9f Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Tue, 21 Aug 2018 14:08:24 -0400 Subject: [PATCH] resource/aws_autoscaling_group: Retry creation for eventual consistency with launch template IAM instance profile Previously: ``` --- FAIL: TestAccAWSAutoScalingGroup_LaunchTemplate_IAMInstanceProfile (14.43s) testing.go:527: Step 0 error: Error applying: 1 error occurred: * aws_autoscaling_group.test: 1 error occurred: * aws_autoscaling_group.test: Error creating AutoScaling Group: ValidationError: You must use a valid fully-formed launch template. Value (tf-acc-test-6643732652421074386) for parameter iamInstanceProfile.name is invalid. Invalid IAM Instance Profile name ``` After code changes: ``` --- PASS: TestAccAWSAutoScalingGroup_LaunchTemplate_IAMInstanceProfile (59.25s) ``` --- aws/resource_aws_autoscaling_group.go | 17 +++++- aws/resource_aws_autoscaling_group_test.go | 68 ++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/aws/resource_aws_autoscaling_group.go b/aws/resource_aws_autoscaling_group.go index 49afe4e4d69..1d170cc2c87 100644 --- a/aws/resource_aws_autoscaling_group.go +++ b/aws/resource_aws_autoscaling_group.go @@ -464,7 +464,22 @@ func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{}) } log.Printf("[DEBUG] AutoScaling Group create configuration: %#v", createOpts) - _, err := conn.CreateAutoScalingGroup(&createOpts) + + // Retry for IAM eventual consistency + err := resource.Retry(1*time.Minute, func() *resource.RetryError { + _, err := conn.CreateAutoScalingGroup(&createOpts) + + // ValidationError: You must use a valid fully-formed launch template. Value (tf-acc-test-6643732652421074386) for parameter iamInstanceProfile.name is invalid. Invalid IAM Instance Profile name + if isAWSErr(err, "ValidationError", "Invalid IAM Instance Profile") { + return resource.RetryableError(err) + } + + if err != nil { + return resource.NonRetryableError(err) + } + + return nil + }) if err != nil { return fmt.Errorf("Error creating AutoScaling Group: %s", err) } diff --git a/aws/resource_aws_autoscaling_group_test.go b/aws/resource_aws_autoscaling_group_test.go index a3f0df6fed9..186371617c1 100644 --- a/aws/resource_aws_autoscaling_group_test.go +++ b/aws/resource_aws_autoscaling_group_test.go @@ -988,6 +988,26 @@ func TestAccAWSAutoScalingGroup_launchTemplate_update(t *testing.T) { }) } +func TestAccAWSAutoScalingGroup_LaunchTemplate_IAMInstanceProfile(t *testing.T) { + var group autoscaling.Group + resourceName := "aws_autoscaling_group.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSAutoScalingGroupConfig_LaunchTemplate_IAMInstanceProfile(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAutoScalingGroupExists(resourceName, &group), + ), + }, + }, + }) +} + const testAccAWSAutoScalingGroupConfig_autoGeneratedName = ` data "aws_ami" "test_ami" { most_recent = true @@ -2484,3 +2504,51 @@ resource "aws_autoscaling_group" "bar" { } } ` + +func testAccAWSAutoScalingGroupConfig_LaunchTemplate_IAMInstanceProfile(rName string) string { + return fmt.Sprintf(` +data "aws_ami" "test" { + most_recent = true + owners = ["amazon"] + + filter { + name = "name" + values = ["amzn-ami-hvm-*-x86_64-gp2"] + } +} + +data "aws_availability_zones" "available" {} + +resource "aws_iam_role" "test" { + assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}" + name = %q +} + +resource "aws_iam_instance_profile" "test" { + name = %q + roles = ["${aws_iam_role.test.name}"] +} + +resource "aws_launch_template" "test" { + image_id = "${data.aws_ami.test.id}" + instance_type = "t2.micro" + name = %q + + iam_instance_profile { + name = "${aws_iam_instance_profile.test.id}" + } +} + +resource "aws_autoscaling_group" "test" { + availability_zones = ["${data.aws_availability_zones.available.names[0]}"] + desired_capacity = 0 + max_size = 0 + min_size = 0 + name = %q + + launch_template { + id = "${aws_launch_template.test.id}" + } +} +`, rName, rName, rName, rName) +}