Creating an AWS S3 Bucket can be done programmatically, using the aws cli or through various automation platforms. One easy way is through the aws cli using a couple easy commands.
Aws S3 buckets are a great way to store static files of various formats at a very low cost. Before using the aws cli or creating an S3 bucket, you will need to create an AWS account.
How to set up the AWS CLI
First visit amazons command line interface page and download the mac, linux or windows version. Then sign into the aws dashboard and create an iam user to connect to your environment using the aws cli.
Creating an AWS IAM User
When creating an AWS S3 bucket user there are 2 options to choose from:
- Access Key (Programmatic Access) – Enables an access key ID and secret access key for the AWS API, CLI, SDK, and other development tools.
- Password (AWS Management Console Access) – Enables a password that allows users to sign-in to the AWS Management Console.
Since we only need to be able to sign into the CLI we will only need a Programmatic Access Key for our access type.
First create a username and select the Access key checkbox. You have 3 ways you can associate the correct permissions to a user:
- Add a User to an Existing group that holds the correct permissions
- Copy the permissions from an existing user
- Attach Existing policies directly to the user
Creating AWS IAM groups is great when you have selected users who will be performing different tasks across different AWS services. This will eliminate users having too few or too much power accidentally. Since we only need the user to access S3 let’s attach the policy directly to the user:
To create and manage our AWS S3 Bucket, assign the AmazonS3FullAccess policy. S3 has other permissions that are less restrictive depending on the use case for the user. Tags can then be assigned to the user. Tags help with identifying what things are used for across your AWS environment.
Now with the user created you will be presented with a screen to save the credentials for your user. This will be the only time you can view this screen so you have 2 options
- Download the keys in a csv and save in a safe place
- Input the keys into the application you need and don’t use them again.
Let’s get into now creating our AWS S3 Bucket from the CLI.
Creating an S3 Bucket
With the AWS CLI installed open up command prompt on your computer and type “aws s3 configure” and fill in the following fields
- AWS Access Key ID [None]: enter access key from aws user we created
- AWS Secret Access Key [None]: enter secret key
- Default region name [None]: select a region from one of many regions here
- Default output format [None]: default is json so you can hit enter here without putting anything in
Now that we are connected to AWS, we can type the command aws into the cli to see the command structure
There are two types of commands you can use to create an S3 bucket. The S3 command or S3API both have their benefits depending on usage. You’ll probably want to use the s3 commands via the cp or sync command operations if you wish to upload a set of files from your local machine to your S3 bucket. If you wish to set a bucket policy, though, you’ll need to use the s3api commands and the put-bucket-policy command action.
Let’s create a simple bucket using the S3API. Type the following in substituting the bucket name for something you want to call it. Bucket names must be unique across the entire S3 domain in AWS so you can’t use a name somebody else may have already used. Also, the region can be whatever region you want it in also.
- aws s3api create-bucket –bucket test-bucket-2262022 –region us-east-2 –create-bucket-configuration LocationConstraint=us-east-2
Buckets by default are created in us-east-1 so to be able to create in a different region we must add the –create-bucket-configuration LocationConstraint=us-east-2 at the end. If not, you will get an (IllegalLocationConstraintException).
Now with the bucket created we can upload files a multitude of ways. The bucket can be used to store personal items from your computer, hold static website files or offsite backups if you work for a company looking to save critical information.
How to Store Files in your AWS S3 Bucket
Using the CLI we can leverage a simple command to upload everything from a folder on our computer up to S3. Replacing the C: and the s3: path with your directory and created S3 bucket
- aws s3 cp c:\Users\SLAdmin\Pictures s3://test-bucket-2262022 –recursive
With no errors in the cli all the contents of your directory should be uploaded to your S3 bucket. Outside of the AWS CLI you can also Directly add files to the bucket going directly to the S3 back in the AWS management console.
I hope that the explanation and examples I provided will assist you in getting the most out of both the s3 and s3api commands. Do not, however, confine yourself to the examples I have presented. Try to think of some other ways to use the s3 and s3api commands together today!
Keep an eye out for our future blog article!