How to create a S3 bucket using the AWS Command Line Interface

Learn how to manage AWS S3 bucket using the Command Line Interface

Every operation that you can perform on the AWS Console can be executed with the command line interface (CLI). AWS Simple Storage Service (S3) is no exception. In this post, you will learn how to create and delete S3 buckets using the CLI.

Create a bucket

Open the terminal and type:

aws s3api create-bucket --bucket <yourbucketname> \
    --region us-east-1

In case you want to create a bucket outside the us-east-1 you need to provide an additional parameter called LocationConstraint. Here's how to create the same bucket in the eu-central-1 region:

aws s3api create-bucket --bucket <yourbucketname> \
    --region eu-central-1 \
    --create-bucket-configuration LocationConstraint=eu-central-1

Remember, the bucket name must be unique across all AWS accounts and can only consist of letters, numbers, and hyphens (-).

If the operation is successful you will see the following output:

{
    "Location": "http://<yourbucketname>.s3.amazonaws.com/"
}

The link reported after the creation is a unique endpoint to retrieve files stored in your bucket. It is recommended to disable any public access to the bucket, here's how to do it:

aws s3api put-public-access-block --bucket <yourbucketname> \
    --public-access-block-configuration "BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true"

We can retrieve the current configuration for public access to a bucket by typing:

aws s3api get-public-access-block --bucket <yourbucketname>

Delete a bucket

aws s3api delete-bucket --bucket <yourbucketname>

Unlike the create-bucket operation, the delete operation can be executed without specifying the AWS region and there is no need to specify additional parameters.

The delete operation will have no effect if the bucket contains a file. So, no risk of accidentally deleting a bucket that still has files stored in it ;).

Conclusions

In this post, we have learned how to create and delete an AWS S3 bucket using the AWS CLI.

References

Did you find this article valuable?

Support Marco Boretto by becoming a sponsor. Any amount is appreciated!