From 0d7e9d899ba131907bfab2d1ebe5baedd4ff76c7 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Wed, 2 Aug 2017 20:42:48 +0200 Subject: [PATCH] r/cloudtrail: Retry if IAM role isn't propagated yet --- aws/resource_aws_cloudtrail.go | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/aws/resource_aws_cloudtrail.go b/aws/resource_aws_cloudtrail.go index 8c59a5d9408..59fca787202 100644 --- a/aws/resource_aws_cloudtrail.go +++ b/aws/resource_aws_cloudtrail.go @@ -3,9 +3,11 @@ package aws import ( "fmt" "log" + "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/cloudtrail" + "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/helper/schema" ) @@ -116,7 +118,21 @@ func resourceAwsCloudTrailCreate(d *schema.ResourceData, meta interface{}) error input.SnsTopicName = aws.String(v.(string)) } - t, err := conn.CreateTrail(&input) + var t *cloudtrail.CreateTrailOutput + err := resource.Retry(15*time.Second, func() *resource.RetryError { + var err error + t, err = conn.CreateTrail(&input) + if err != nil { + if isAWSErr(err, cloudtrail.ErrCodeInvalidCloudWatchLogsRoleArnException, "Access denied.") { + return resource.RetryableError(err) + } + if isAWSErr(err, cloudtrail.ErrCodeInvalidCloudWatchLogsLogGroupArnException, "Access denied.") { + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + return nil + }) if err != nil { return err } @@ -250,7 +266,21 @@ func resourceAwsCloudTrailUpdate(d *schema.ResourceData, meta interface{}) error } log.Printf("[DEBUG] Updating CloudTrail: %s", input) - t, err := conn.UpdateTrail(&input) + var t *cloudtrail.UpdateTrailOutput + err := resource.Retry(15*time.Second, func() *resource.RetryError { + var err error + t, err = conn.UpdateTrail(&input) + if err != nil { + if isAWSErr(err, cloudtrail.ErrCodeInvalidCloudWatchLogsRoleArnException, "Access denied.") { + return resource.RetryableError(err) + } + if isAWSErr(err, cloudtrail.ErrCodeInvalidCloudWatchLogsLogGroupArnException, "Access denied.") { + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + return nil + }) if err != nil { return err }