Skip to content

How to create AWS S3 Buckets using Python and AWS CLI

Posted on:June 1, 2023 at 05:17 AM

The AWS CLI and Python are the most popular tools for managing Amazon Web Services. In this series of posts, I’ll show you how easy it is to work with S3 using AWS CLI and Python.
The AWS CLI can be installed with a simple pip install AWS CLI command, while python-boto3 is required to access the AWS API from Python. You can read these blogs if you need help installing AWS CLI and Python on your machine. This blog expects that you have AWS CLI and Python configured on your device.

Create an S3 bucket using AWS CLI

AWS CLI offers two ways to create S3 buckets. One method uses aws s3api commands, and another uses aws s3 commands.

The AWS s3 has a custom set of commands designed to make working with S3 easier. These are built on top of aws s3api commands.

Using s3api to create an S3 bucket

Let us start with s3api and check our options to create an S3 bucket.

aws s3api create-bucket --bucket "s3-bucket-from-cli-1"

I have used double quotes to show values for bucket names in this command. This is not mandatory, but I like to use it to add readability to CLI commands.

This command will create “s3-bucket-from-cli-1”. We can check this in the AWS S3 console.

S3 console showing AWS CLI has created a bucket

We can check that this command has created a bucket in S3 located in the US East 1 region. An S3 bucket will be created in the same region that you have configured as the default region while setting up AWS CLI. We can pass parameters to create a bucket command if you want to change that region and access policy while creating a bucket.

aws s3api create-bucket \
--bucket "s3-bucket-from-cli-2" \
--acl "public-read" \
--region us-east-2

Using the s3 mb command to create a bucket

As mentioned, we can create an S3 bucket using s3 commands API. In this case, we must pass the bucket name as s3 URI instead of plain text.

aws s3 mb "s3://s3-bucket-from-cli-2"
make_bucket: s3-bucket-from-cli-2

Creating an S3 bucket using Python

Now, we will write Python code to create an S3 bucket; just like CLI, Python offers multiple ways to create an S3 bucket. Let us check one by one.

Using the boto3 s3 client to create a bucket

The code below will create a bucket in AWS S3. As with the CLI, we can pass additional configurations while creating a bucket.

import boto3
import pprint

s3 = boto3.client("s3")
# creates 3 bucket with defulat set up
response = s3.create_bucket(Bucket="binary-guy-frompython-1")

# following parameters can be passed while creating bucket
response = s3.create_bucket(
    CreateBucketConfiguration={"LocationConstraint": "ap-south-1"},

S3 console screenshot showing python s3 client has created buckets.

After running the above code, we see two new buckets in the s3 console. Also, for bucket 2, the region is set to ap-south-1, as we have specified during the create bucket call.

Using the s3 resource object to create a bucket

Like the s3 client, we can also use s3 resource to create an S3 bucket.

s3_resource = boto3.resource('s3')
response = s3_resource.create_bucket(Bucket="testbucket-frompython-4")

/The process to use s3 resource is similar to s3 client from boto3. In the next few blogs, we will learn that the s3 resource is easier to use in certain situations.


I hope you have found this helpful. In the following few blogs, we will deal with S3 and learn how easy it is to work with S3 using Python and AWS CLI. See you in the next blog.