diff --git a/aws/resource_aws_instance.go b/aws/resource_aws_instance.go index 52e088e2c8e..29024e1ebc5 100644 --- a/aws/resource_aws_instance.go +++ b/aws/resource_aws_instance.go @@ -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 @@ -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 diff --git a/aws/resource_aws_instance_test.go b/aws/resource_aws_instance_test.go index 0c9f9e52a99..f2632c2929d 100644 --- a/aws/resource_aws_instance_test.go +++ b/aws/resource_aws_instance_test.go @@ -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 {