How to use waiter functionality for bucket_not_exists using Boto3 and AWS Client?


Problem Statement − Use boto3 library in Python to check whether a bucket does not exist using waiter functionality. For example, use waiters to check whether Bucket_2 does not exist in S3.

Approach/Algorithm to solve this problem

Step 1 − Import boto3 and botocore exceptions to handle exceptions.

Step 2 − Use bucket_name as the parameter in the function.

Step 3 − Create an AWS session using boto3 library.

Step 4 − Create an AWS client for S3.

Step 5 − Now create the wait object for bucket_not_exists using get_waiter function.

Step 6 − Now, use the wait object to validate whether the bucket does not exist. By default, it checks in every 5 seconds until a successful state is reached. Here successful state means bucket should not be present in S3. An error is returned after 20 failed checks. However, the user can define polling time and max attempts.

Step 7 − It returns None.

Step 8 − Handle the generic exception if something went wrong while checking the bucket.

Example

Use the following code to use waiter to check whether bucket_not_exists −

import boto3
from botocore.exceptions import ClientError

def use_waiters_check_bucket_not_exists(bucket_name):
   session = boto3.session.Session()
   s3_client = session.client('s3')
   try:
      waiter = s3_client.get_waiter('bucket_not_exists')
      waiter.wait(Bucket=bucket_name,
                  WaiterConfig={
                     'Delay': 2, 'MaxAttempts': 5})
      print('Bucket not exist: ' + bucket_name)
   except ClientError as e:
      raise Exception( "boto3 client error in use_waiters_check_bucket_not_exists: " + e.__str__())
   except Exception as e:
      raise Exception( "Unexpected error in use_waiters_check_bucket_not_exists: " + e.__str__())

print(use_waiters_check_bucket_not_exists("Bucket_2"))
print(use_waiters_check_bucket_not_exists("Bucket_1"))

Output

Bucket not exist: Bucket_2
None

botocore.exceptions.WaiterError: Waiter BucketNotExists failed: Max
attempts exceeded
"Unexpected error in use_waiters_check_bucket_not_exists: " +
e.__str__())
Exception: Unexpected error in use_waiters_check_bucket_not_exists:
Waiter BucketNotExists failed: Max attempts exceed

For Bucket_2, the output is print statement and None. Since the response doesn’t return anything, it prints None.

For Bucket_1, the output is an exception, since this bucket exists even after max attempts to check.

In exception, it can be read that Max attempts exceeded.

Updated on: 22-Mar-2021

732 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements