Author's avatarCourseWizz3 min read

AWS CLI Tutorial - How to Install, Configure and Use AWS CLI

Post Cover Picture - AWS CLI Tutorial - How to Install, Configure and Use AWS CLI

Install

Installing the AWS CLI is dead easy. Your best bet is heading over to this AWS webpage which gives you detailed instructions on how to get started for each operating system.

But, if you're on a Mac 💻 like me then stay here! 😉

Step 1: Copy and paste the following into your terminal:

curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"
sudo installer -pkg AWSCLIV2.pkg -target /

What does this do?

Well the first command uses the curl command line tool to download the AWS CLI package from AWS.

The second command then installs the AWS CLI package using the built in Mac installer.

Step 2: Check installation was successful:

To make sure AWS CLI is correctly set up, run:

which aws
aws --version

The first command prints out the location of install.
The second command checks the CLI itself is working (you should see the cli version printed out).

Post Cover Picture

How easy was that?

Configuration

Head over to the AWS console, sign in and complete the following steps. If you haven't already made an AWS account check out this CourseWizz tutorial.

Step 1: Click on the Account name dropdown.

Post Cover Picture

Step 2: Click on Security and Credentials

Post Cover Picture

Step 3: Click on Create Access Key button:

This will create a root user Access Key. In practice you should create IAM roles and assign least privileged permissions and generate keys for each role.

Post Cover Picture

Step 4: Confirm you are happy to create key and click Create access key button

Post Cover Picture

Step 5: Success, you now have both Access Key and Secret Access Key:

Keep these (especially the secret) stored securely. After clicking done you will no longer be able to access the secret key!

Post Cover Picture

Back to the Terminal

Now we have everything we need to configure the AWS CLI. Open your terminal back up and type in the following command:

aws configure
Post Cover Picture

I have already completed the configure step hence it shows my obscured keys. But those doing it for the first time will need to:

  1. Copy and Paste Access Key
  2. Copy and Paste Secret Access Key
  3. Specify default AWS region
  4. Specify default output format. I choose json as it's my preferred although there are other options like standard text and yaml

Congratulations, you should now be configured to use the AWS CLI 🎉

Use

So what can i do with the newly installed AWS CLI?

You can do pretty much everything that can be done in the online AWS Console. But now, instead of opening up your browser and signing in, you can open the terminal and get straight to it.

Let's do something with it...

We will start simple, let's create a private S3 bucket called coursewizztestbucket and we are going to upload a something.txt file to it.

Type the following into the cli:

aws s3api create-bucket 
--bucket coursewizztestbucket 
--region eu-west-2
--create-bucket-configuration LocationConstraint=eu-west-2 

This command tells AWS CLI to use the create-bucket function of the S3 api. Additional options can be provided, options are denoted by -- and the following string is the value.

In this example we included --bucket --region --create-bucket-configuration. These are the minimum required options to create an S3 bucket.

If your bucket has been successfully created you will expect to see a JSON object with the location property printed:

Post Cover Picture

Great now we've made a bucket!

It looks quite empty, shall we put something inside?

Create a text file called "something.txt" and open the editor in the terminal.

touch something.txt
nano something.txt

"touch something.txt" creates a new file called something.txt."nano something.txt" opens nano, which is a terminal based editor. Now we can write something.

Post Cover Picture

To exit nano:

  • Press Ctrl x which will ask if you want to keep file changes. Press y for yes.
  • Now you will be asked what filename you want to write to, it should be pre-filled with something.txt, so press enter.
Post Cover Picture

Now we want to upload something.txt to our newly created S3 bucket.

Lets enter the following command:

aws s3api put-object 
--bucket coursewizztestbucket 
--body something.txt 
--key something

This time we are using put-object of the S3 api which uses a HTTP PUT request to upload a blob (our file something.txt) to S3. The options passed are bucket (specifies the bucket), body (file we are uploading) and key (unique name or id for the object in S3). See here for the full documentation of put-object.

If the upload was successful you should expect to see a JSON response with ETag and ServerSideEncryption properties printed like so:

Post Cover Picture

Now we have a bucket that is filled with something 😄

Summary

As you can see using the AWS CLI is nice, fast and easy! With some practice, you'll master the AWS APIs, allowing you to swiftly launch instances, upload files to S3, or handle any other task with ease.

For comprehensive AWS CLI guidance, refer to the AWS User Guide - especially if you're not yet an expert!