Skip to content

Commit

Permalink
resource/aws_lambda_function: Only retry IAM errors for one minute
Browse files Browse the repository at this point in the history
  • Loading branch information
bflad committed Mar 13, 2018
1 parent 1f1a27e commit 101ed64
Showing 1 changed file with 45 additions and 7 deletions.
52 changes: 45 additions & 7 deletions aws/resource_aws_lambda_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,8 @@ func resourceAwsLambdaFunctionCreate(d *schema.ResourceData, meta interface{}) e
params.Tags = tagsFromMapGeneric(v.(map[string]interface{}))
}

// 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
// Error creating Lambda function: InvalidParameterValueException: The role defined for the task cannot be assumed by Lambda.
err := resource.Retry(10*time.Minute, func() *resource.RetryError {
// IAM changes can take 1 minute to propagate in AWS
err := resource.Retry(1*time.Minute, func() *resource.RetryError {
_, err := conn.CreateFunction(params)
if err != nil {
log.Printf("[DEBUG] Error creating Lambda Function: %s", err)
Expand All @@ -386,7 +384,27 @@ func resourceAwsLambdaFunctionCreate(d *schema.ResourceData, meta interface{}) e
return nil
})
if err != nil {
return fmt.Errorf("Error creating Lambda function: %s", err)
if !isAWSErr(err, "InvalidParameterValueException", "Your request has been throttled by EC2") {
return fmt.Errorf("Error creating Lambda function: %s", err)
}
// Allow 9 more minutes for EC2 throttling
err := resource.Retry(9*time.Minute, func() *resource.RetryError {
_, err := conn.CreateFunction(params)
if err != nil {
log.Printf("[DEBUG] Error creating Lambda Function: %s", err)

if isAWSErr(err, "InvalidParameterValueException", "Your request has been throttled by EC2") {
log.Printf("[DEBUG] Received %s, retrying CreateFunction", err)
return resource.RetryableError(err)
}

return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
return fmt.Errorf("Error creating Lambda function: %s", err)
}
}

d.SetId(d.Get("function_name").(string))
Expand Down Expand Up @@ -686,7 +704,8 @@ func resourceAwsLambdaFunctionUpdate(d *schema.ResourceData, meta interface{}) e
if configUpdate {
log.Printf("[DEBUG] Send Update Lambda Function Configuration request: %#v", configReq)

err := resource.Retry(10*time.Minute, func() *resource.RetryError {
// IAM changes can take 1 minute to propagate in AWS
err := resource.Retry(1*time.Minute, func() *resource.RetryError {
_, err := conn.UpdateFunctionConfiguration(configReq)
if err != nil {
log.Printf("[DEBUG] Received error modifying Lambda Function Configuration %s: %s", d.Id(), err)
Expand All @@ -704,7 +723,26 @@ func resourceAwsLambdaFunctionUpdate(d *schema.ResourceData, meta interface{}) e
return nil
})
if err != nil {
return fmt.Errorf("Error modifying Lambda Function Configuration %s: %s", d.Id(), err)
if !isAWSErr(err, "InvalidParameterValueException", "Your request has been throttled by EC2, please make sure you have enough API rate limit.") {
return fmt.Errorf("Error modifying Lambda Function Configuration %s: %s", d.Id(), err)
}
// Allow 9 more minutes for EC2 throttling
err := resource.Retry(9*time.Minute, func() *resource.RetryError {
_, err := conn.UpdateFunctionConfiguration(configReq)
if err != nil {
log.Printf("[DEBUG] Received error modifying Lambda Function Configuration %s: %s", d.Id(), err)

if isAWSErr(err, "InvalidParameterValueException", "Your request has been throttled by EC2, please make sure you have enough API rate limit.") {
log.Printf("[DEBUG] Received %s, retrying UpdateFunctionConfiguration", err)
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
return fmt.Errorf("Error modifying Lambda Function Configuration %s: %s", d.Id(), err)
}
}

d.SetPartial("description")
Expand Down

0 comments on commit 101ed64

Please sign in to comment.