From cbee50b66fdc298bacf5d784c03ccd3df9494528 Mon Sep 17 00:00:00 2001 From: Victor Cardenas Date: Wed, 13 Mar 2019 15:08:10 -0400 Subject: [PATCH] Retry S3 bucket lifecycle creation. Increase retry on aws error code timeout to 2 minutes. --- aws/awserr.go | 2 +- aws/resource_aws_s3_bucket.go | 57 +++++++++++++++++------------------ 2 files changed, 28 insertions(+), 31 deletions(-) diff --git a/aws/awserr.go b/aws/awserr.go index 09771aea759..5f72afa6061 100644 --- a/aws/awserr.go +++ b/aws/awserr.go @@ -21,7 +21,7 @@ func isAWSErr(err error, code string, message string) bool { func retryOnAwsCode(code string, f func() (interface{}, error)) (interface{}, error) { var resp interface{} - err := resource.Retry(1*time.Minute, func() *resource.RetryError { + err := resource.Retry(2*time.Minute, func() *resource.RetryError { var err error resp, err = f() if err != nil { diff --git a/aws/resource_aws_s3_bucket.go b/aws/resource_aws_s3_bucket.go index ae935d56baa..20974a87c68 100644 --- a/aws/resource_aws_s3_bucket.go +++ b/aws/resource_aws_s3_bucket.go @@ -757,7 +757,7 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error { var err error - _, err = retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + _, err = retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.HeadBucket(&s3.HeadBucketInput{ Bucket: aws.String(d.Id()), }) @@ -781,7 +781,7 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error { // Read the policy if _, ok := d.GetOk("policy"); ok { - pol, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + pol, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.GetBucketPolicy(&s3.GetBucketPolicyInput{ Bucket: aws.String(d.Id()), }) @@ -807,7 +807,7 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error { } // Read the CORS - corsResponse, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + corsResponse, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.GetBucketCors(&s3.GetBucketCorsInput{ Bucket: aws.String(d.Id()), }) @@ -839,7 +839,7 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error { } // Read the website configuration - wsResponse, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + wsResponse, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.GetBucketWebsite(&s3.GetBucketWebsiteInput{ Bucket: aws.String(d.Id()), }) @@ -906,7 +906,7 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error { // Read the versioning configuration - versioningResponse, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + versioningResponse, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.GetBucketVersioning(&s3.GetBucketVersioningInput{ Bucket: aws.String(d.Id()), }) @@ -937,7 +937,7 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error { // Read the acceleration status - accelerateResponse, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + accelerateResponse, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.GetBucketAccelerateConfiguration(&s3.GetBucketAccelerateConfigurationInput{ Bucket: aws.String(d.Id()), }) @@ -953,7 +953,7 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error { // Read the request payer configuration. - payerResponse, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + payerResponse, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.GetBucketRequestPayment(&s3.GetBucketRequestPaymentInput{ Bucket: aws.String(d.Id()), }) @@ -968,7 +968,7 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error { } // Read the logging configuration - loggingResponse, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + loggingResponse, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.GetBucketLogging(&s3.GetBucketLoggingInput{ Bucket: aws.String(d.Id()), }) @@ -996,7 +996,7 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error { // Read the lifecycle configuration - lifecycleResponse, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + lifecycleResponse, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.GetBucketLifecycleConfiguration(&s3.GetBucketLifecycleConfigurationInput{ Bucket: aws.String(d.Id()), }) @@ -1120,7 +1120,7 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error { // Read the bucket replication configuration - replicationResponse, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + replicationResponse, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.GetBucketReplication(&s3.GetBucketReplicationInput{ Bucket: aws.String(d.Id()), }) @@ -1139,7 +1139,7 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error { // Read the bucket server side encryption configuration - encryptionResponse, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + encryptionResponse, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.GetBucketEncryption(&s3.GetBucketEncryptionInput{ Bucket: aws.String(d.Id()), }) @@ -1167,7 +1167,7 @@ func resourceAwsS3BucketRead(d *schema.ResourceData, meta interface{}) error { // Add the region as an attribute - locationResponse, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + locationResponse, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.GetBucketLocation( &s3.GetBucketLocationInput{ Bucket: aws.String(d.Id()), @@ -1324,7 +1324,7 @@ func resourceAwsS3BucketPolicyUpdate(s3conn *s3.S3, d *schema.ResourceData) erro err := resource.Retry(1*time.Minute, func() *resource.RetryError { if _, err := s3conn.PutBucketPolicy(params); err != nil { if awserr, ok := err.(awserr.Error); ok { - if awserr.Code() == "MalformedPolicy" || awserr.Code() == "NoSuchBucket" { + if awserr.Code() == "MalformedPolicy" || awserr.Code() == s3.ErrCodeNoSuchBucket { return resource.RetryableError(awserr) } } @@ -1338,7 +1338,7 @@ func resourceAwsS3BucketPolicyUpdate(s3conn *s3.S3, d *schema.ResourceData) erro } } else { log.Printf("[DEBUG] S3 bucket: %s, delete policy: %s", bucket, policy) - _, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + _, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.DeleteBucketPolicy(&s3.DeleteBucketPolicyInput{ Bucket: aws.String(bucket), }) @@ -1360,7 +1360,7 @@ func resourceAwsS3BucketCorsUpdate(s3conn *s3.S3, d *schema.ResourceData) error // Delete CORS log.Printf("[DEBUG] S3 bucket: %s, delete CORS", bucket) - _, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + _, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.DeleteBucketCors(&s3.DeleteBucketCorsInput{ Bucket: aws.String(bucket), }) @@ -1407,7 +1407,7 @@ func resourceAwsS3BucketCorsUpdate(s3conn *s3.S3, d *schema.ResourceData) error } log.Printf("[DEBUG] S3 bucket: %s, put CORS: %#v", bucket, corsInput) - _, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + _, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.PutBucketCors(corsInput) }) if err != nil { @@ -1498,7 +1498,7 @@ func resourceAwsS3BucketWebsitePut(s3conn *s3.S3, d *schema.ResourceData, websit log.Printf("[DEBUG] S3 put bucket website: %#v", putInput) - _, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + _, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.PutBucketWebsite(putInput) }) if err != nil { @@ -1514,7 +1514,7 @@ func resourceAwsS3BucketWebsiteDelete(s3conn *s3.S3, d *schema.ResourceData) err log.Printf("[DEBUG] S3 delete bucket website: %#v", deleteInput) - _, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + _, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.DeleteBucketWebsite(deleteInput) }) if err != nil { @@ -1538,7 +1538,7 @@ func websiteEndpoint(s3conn *s3.S3, d *schema.ResourceData) (*S3Website, error) // Lookup the region for this bucket - locationResponse, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + locationResponse, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.GetBucketLocation( &s3.GetBucketLocationInput{ Bucket: aws.String(bucket), @@ -1621,7 +1621,7 @@ func resourceAwsS3BucketAclUpdate(s3conn *s3.S3, d *schema.ResourceData) error { } log.Printf("[DEBUG] S3 put bucket ACL: %#v", i) - _, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + _, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.PutBucketAcl(i) }) if err != nil { @@ -1661,7 +1661,7 @@ func resourceAwsS3BucketVersioningUpdate(s3conn *s3.S3, d *schema.ResourceData) } log.Printf("[DEBUG] S3 put bucket versioning: %#v", i) - _, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + _, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.PutBucketVersioning(i) }) if err != nil { @@ -1696,7 +1696,7 @@ func resourceAwsS3BucketLoggingUpdate(s3conn *s3.S3, d *schema.ResourceData) err } log.Printf("[DEBUG] S3 put bucket logging: %#v", i) - _, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + _, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.PutBucketLogging(i) }) if err != nil { @@ -1718,7 +1718,7 @@ func resourceAwsS3BucketAccelerationUpdate(s3conn *s3.S3, d *schema.ResourceData } log.Printf("[DEBUG] S3 put bucket acceleration: %#v", i) - _, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + _, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.PutBucketAccelerateConfiguration(i) }) if err != nil { @@ -1740,7 +1740,7 @@ func resourceAwsS3BucketRequestPayerUpdate(s3conn *s3.S3, d *schema.ResourceData } log.Printf("[DEBUG] S3 put bucket request payer: %#v", i) - _, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + _, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.PutBucketRequestPayment(i) }) if err != nil { @@ -1802,7 +1802,7 @@ func resourceAwsS3BucketServerSideEncryptionConfigurationUpdate(s3conn *s3.S3, d } log.Printf("[DEBUG] S3 put bucket replication configuration: %#v", i) - _, err := retryOnAwsCode("NoSuchBucket", func() (interface{}, error) { + _, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { return s3conn.PutBucketEncryption(i) }) if err != nil { @@ -2126,11 +2126,8 @@ func resourceAwsS3BucketLifecycleUpdate(s3conn *s3.S3, d *schema.ResourceData) e }, } - err := resource.Retry(1*time.Minute, func() *resource.RetryError { - if _, err := s3conn.PutBucketLifecycleConfiguration(i); err != nil { - return resource.NonRetryableError(err) - } - return nil + _, err := retryOnAwsCode(s3.ErrCodeNoSuchBucket, func() (interface{}, error) { + return s3conn.PutBucketLifecycleConfiguration(i) }) if err != nil { return fmt.Errorf("Error putting S3 lifecycle: %s", err)