1

I want to create a S3 bucket with several properties:

def create_s3_bucket(name):
    s3Client = boto3.client('s3')

    try:
        s3Client.create_bucket(
            Bucket=name,
            ACL="private",
            CreateBucketConfiguration={
                'LocationConstraint': 'eu-central-1',
            },
        )
    except ClientError as e:
        print(e)

    try:
        s3Client.put_public_access_block(
            Bucket=name,
            PublicAccessBlockConfiguration={
                'BlockPublicAcls': True,
                'IgnorePublicAcls': True,
                'BlockPublicPolicy': True,
                'RestrictPublicBuckets': True
            }
        )
    except ClientError as e:
        print(e)

    try:

        s3Client.put_bucket_versioning(
            Bucket=name,
            VersioningConfiguration={
                'Status': 'Enabled'
            }
        )
    except ClientError as e:

        s3Client.put_bucket_tagging(
            Bucket=name,
            Tagging={
                'TagSet': [
                    {
                        'Key': 'firstTag',
                        'Value': 'firstValue'
                    },
                ]
            }
        )
    except ClientError as e:
        print(e)

This works so far. My question is: Is there a better, more elegant way how to handle the ClientError since I keep on repeating myself in the code? If I put each API call into one try catch block, then it might be the case that one call does not event take place.

3
  • How would put_public_access_block work if create_bucket failed? You need to flesh out what the behavior of your program should be in the error cases. Commented Jul 25, 2020 at 7:10
  • I need to specify my question: If the bucket e.g. already exists then it throws an exception. But this should prevent putting the bucket properties with follow up API calls. Commented Jul 25, 2020 at 7:44
  • If you want to check if bucket exists and finish your function, why don't you just return if it does not exist? Commented Jul 25, 2020 at 8:20

1 Answer 1

1

You could put all of this in the same try block which would ultimately prevent it from attempting functions that are not able to be performed (i.e. the bucket never got created).

If the exception is raised it should return something back other than a print to identify this failed, to prevent the caller of the function continuing further with the logic.

In addition in your exception statement you could have the ability to rollback if your bucket got created but was unable to carry on.

This code might look like the below

def create_s3_bucket(name):
    s3Client = boto3.client('s3')

    try:
        s3Client.create_bucket(
            Bucket=name,
            ACL="private",
            CreateBucketConfiguration={
                'LocationConstraint': 'eu-central-1',
            },
        )

        s3Client.put_public_access_block(
            Bucket=name,
            PublicAccessBlockConfiguration={
                'BlockPublicAcls': True,
                'IgnorePublicAcls': True,
                'BlockPublicPolicy': True,
                'RestrictPublicBuckets': True
            }
        )

        s3Client.put_bucket_versioning(
            Bucket=name,
            VersioningConfiguration={
                'Status': 'Enabled'
            }
        )

        s3Client.put_bucket_tagging(
            Bucket=name,
            Tagging={
                'TagSet': [
                    {
                        'Key': 'firstTag',
                        'Value': 'firstValue'
                    },
                ]
            }
        )
    except ClientError as e:
        print(e)
        #Do some tidy up (delete bucket or notify)
        return false

    return true

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, this helps me :) However I do not understand the purpose of return in your code. I also saw this return statements in documentation but without further explanation. What is their purpose in that case?
Glad it could be of some help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.