From 0bbb3df8929c701c9ddd0b1722c841aaf7156d2c Mon Sep 17 00:00:00 2001 From: sterling Date: Tue, 4 May 2021 11:35:47 -0400 Subject: [PATCH 1/3] use head_bucket request to determine if s3 bucket exists --- plugins/modules/s3_bucket.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/plugins/modules/s3_bucket.py b/plugins/modules/s3_bucket.py index 591551ac5fc..3dbb6dc4166 100644 --- a/plugins/modules/s3_bucket.py +++ b/plugins/modules/s3_bucket.py @@ -509,10 +509,15 @@ def create_or_update_bucket(s3_client, module, location): def bucket_exists(s3_client, bucket_name): - # head_bucket appeared to be really inconsistent, so we use list_buckets instead, - # and loop over all the buckets, even if we know it's less performant :( - all_buckets = s3_client.list_buckets(Bucket=bucket_name)['Buckets'] - return any(bucket['Name'] == bucket_name for bucket in all_buckets) + try: + s3_client.head_bucket(Bucket=bucket_name) + bucket_exists = True + except ClientError as e: + if e.response['Error']['Code'] == '404': + bucket_exists = False + else: + raise + return bucket_exists @AWSRetry.exponential_backoff(max_delay=120) From dc81e8b290a4153841f62f82fa6bb2b3058099d0 Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Fri, 27 Aug 2021 12:57:10 +0200 Subject: [PATCH 2/3] use is_boto3_error_code rather than directly reading response['Error']['Code'] --- plugins/modules/s3_bucket.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/plugins/modules/s3_bucket.py b/plugins/modules/s3_bucket.py index 3dbb6dc4166..302e856246f 100644 --- a/plugins/modules/s3_bucket.py +++ b/plugins/modules/s3_bucket.py @@ -512,11 +512,8 @@ def bucket_exists(s3_client, bucket_name): try: s3_client.head_bucket(Bucket=bucket_name) bucket_exists = True - except ClientError as e: - if e.response['Error']['Code'] == '404': - bucket_exists = False - else: - raise + except is_boto3_error_code('404'): + bucket_exists = False return bucket_exists From 1125b2ced0cdc7badc9196c0314191b150f36199 Mon Sep 17 00:00:00 2001 From: Mark Chappell Date: Fri, 27 Aug 2021 12:56:04 +0200 Subject: [PATCH 3/3] changelog --- changelogs/fragments/357-s3_bucket-use-head.yml | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 changelogs/fragments/357-s3_bucket-use-head.yml diff --git a/changelogs/fragments/357-s3_bucket-use-head.yml b/changelogs/fragments/357-s3_bucket-use-head.yml new file mode 100644 index 00000000000..78cb20e7bf8 --- /dev/null +++ b/changelogs/fragments/357-s3_bucket-use-head.yml @@ -0,0 +1,2 @@ +minor_changes: +- s3_bucket - updated to use HeadBucket instead of ListBucket when testing for bucket existence (https://github.com/ansible-collections/amazon.aws/pull/357).