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_autoscaling_group: Retry creation for eventual consistency with launch template IAM instance profile #5633

Merged
merged 1 commit into from
Aug 30, 2018
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
17 changes: 16 additions & 1 deletion aws/resource_aws_autoscaling_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
68 changes: 68 additions & 0 deletions aws/resource_aws_autoscaling_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}