Skip to content

Commit

Permalink
Merge pull request #3055 from terraform-providers/b-aws_instance-inst…
Browse files Browse the repository at this point in the history
…ance-profile-update-retries

resource/aws_instance: Retry IAM instance profile (re)association for eventual consistency on update
  • Loading branch information
bflad authored Jan 18, 2018
2 parents a18c314 + 6ca1a85 commit e88ab8e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
38 changes: 28 additions & 10 deletions aws/resource_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,11 +822,20 @@ func resourceAwsInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
if _, ok := d.GetOk("iam_instance_profile"); ok {
// Does not have an Iam Instance Profile associated with it, need to associate
if len(resp.IamInstanceProfileAssociations) == 0 {
_, err := conn.AssociateIamInstanceProfile(&ec2.AssociateIamInstanceProfileInput{
InstanceId: aws.String(d.Id()),
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{
Name: aws.String(d.Get("iam_instance_profile").(string)),
},
err := resource.Retry(1*time.Minute, func() *resource.RetryError {
_, err := conn.AssociateIamInstanceProfile(&ec2.AssociateIamInstanceProfileInput{
InstanceId: aws.String(d.Id()),
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{
Name: aws.String(d.Get("iam_instance_profile").(string)),
},
})
if err != nil {
if isAWSErr(err, "InvalidParameterValue", "Invalid IAM Instance Profile") {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
return err
Expand All @@ -836,11 +845,20 @@ func resourceAwsInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
// Has an Iam Instance Profile associated with it, need to replace the association
associationId := resp.IamInstanceProfileAssociations[0].AssociationId

_, err := conn.ReplaceIamInstanceProfileAssociation(&ec2.ReplaceIamInstanceProfileAssociationInput{
AssociationId: associationId,
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{
Name: aws.String(d.Get("iam_instance_profile").(string)),
},
err := resource.Retry(1*time.Minute, func() *resource.RetryError {
_, err := conn.ReplaceIamInstanceProfileAssociation(&ec2.ReplaceIamInstanceProfileAssociationInput{
AssociationId: associationId,
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{
Name: aws.String(d.Get("iam_instance_profile").(string)),
},
})
if err != nil {
if isAWSErr(err, "InvalidParameterValue", "Invalid IAM Instance Profile") {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
return err
Expand Down
7 changes: 1 addition & 6 deletions aws/resource_aws_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2072,18 +2072,13 @@ resource "aws_iam_role" "test" {
assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
}
resource "aws_iam_instance_profile" "test" {
name = "test-%s"
roles = ["${aws_iam_role.test.name}"]
}
resource "aws_instance" "foo" {
ami = "ami-4fccb37f"
instance_type = "m1.small"
tags {
bar = "baz"
}
}`, rName, rName)
}`, rName)
}

func testAccInstanceConfigWithInstanceProfile(rName string) string {
Expand Down

0 comments on commit e88ab8e

Please sign in to comment.