Skip to content

Commit

Permalink
Merge pull request #2037 from hashicorp/f-aws-iam-instance-bug
Browse files Browse the repository at this point in the history
provider/aws: Retry RunInstance if IAM profile hasn't propagated
  • Loading branch information
catsby committed May 22, 2015
2 parents 22ae6e9 + a2baf1d commit 338bb50
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion builtin/providers/aws/resource_aws_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,21 @@ func resourceAwsInstanceCreate(d *schema.ResourceData, meta interface{}) error {
// Create the instance
log.Printf("[DEBUG] Run configuration: %#v", runOpts)
var err error
runResp, err := conn.RunInstances(runOpts)

var runResp *ec2.Reservation
for i := 0; i < 5; i++ {
runResp, err = conn.RunInstances(runOpts)
if awsErr, ok := err.(awserr.Error); ok {
// IAM profiles can take ~10 seconds to propagate in AWS:
// http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/iam-roles-for-amazon-ec2.html#launch-instance-with-role-console
if awsErr.Code() == "InvalidParameterValue" && strings.Contains(awsErr.Message(), "Invalid IAM Instance Profile") {
log.Printf("[DEBUG] Invalid IAM Instance Profile referenced, retrying...")
time.Sleep(2 * time.Second)
continue
}
}
break
}
if err != nil {
return fmt.Errorf("Error launching source instance: %s", err)
}
Expand Down

0 comments on commit 338bb50

Please sign in to comment.