From a2baf1d7556f78a72a1aaac58830e31376a6c8d1 Mon Sep 17 00:00:00 2001 From: Clint Shryock Date: Thu, 21 May 2015 14:58:34 -0500 Subject: [PATCH] provider/aws: Retry RunInstance if IAM profile hasn't propagated --- builtin/providers/aws/resource_aws_instance.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/builtin/providers/aws/resource_aws_instance.go b/builtin/providers/aws/resource_aws_instance.go index 8224ba4f2..8c6d5a2c6 100644 --- a/builtin/providers/aws/resource_aws_instance.go +++ b/builtin/providers/aws/resource_aws_instance.go @@ -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) }