Introduction to the AWS CLI#

Overview

Teaching: 30 mins

Exercises: 10 mins

Questions:

  • How do I use the AWS CLI?

Objectives:

  • Understand when and how to use the AWS CLI

  • Understand basic AWS CLI commands to provision resources

The AWS CloudShell#

Now that we have successfully created an EC2 Instance and an s3 bucket, we will explore ways to interact with these two AWS resources using the AWS Command Line Interface (CLI). The AWS Command Line Interface (CLI) is a unified tool to manage your AWS services. With just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts.

AWS CloudShell is a browser-based shell that makes it easy to securely manage, explore, and interact with your AWS resources. With CloudShell, you can quickly run scripts with the AWS Command Line Interface (AWS CLI). You can use CloudShell right from your browser and at no additional cost.

You can find the AWS CloudShell on the top toolbar of the console. Clicking the icon will launch the AWS CloudShell.

If this is your first time using the cloud console, you may encounter a pop up. It is fine to dismiss it.

Once we have launched the CloudShell, we can run some commands. Here a fundamental knowledge of unix commands may be useful but we will utilize the several aws-cli commands to help you get a feel of what the AWS CLI can do. At the prompt (the prompt is where is says [cloudshell-user@ip-XXXXXXX~]$), we will test out some commands.

To check which file directory you are working in:

pwd

/home/cloudshell-user

AWS CLI and s3 buckets#

Let’s now use the the AWS CLI command to list your s3 buckets. You will see the bucket that you created in the previous episode listed. Note that the XXXXXXX in bucket-userXXXXXXXX denotes your individual user ID that you used to create your s3 bucket.

aws s3 ls

2022-02-04 06:31:43 bucket-userXXXXXXX

Let’s list the contents of your s3 bucket:

aws s3 ls s3://bucket-userXXXXXXX

Looks like there is nothing there right now. So let us create a text file called hemingway.txt and populate it with a nonsensical quote.

echo "The world breaks everyone" > hemingway.txt

Let’s check the contents of the file:

cat hemingway.txt

The world breaks everyone

Great! Now we have a file to work with. Let’s move the file to your s3 bucket using this command:

aws s3 mv ./hemingway.txt s3://bucket-userXXXXXXX

Then we’ll list the contents of your CloudShell home directory just to see what’s going on.

ls

Looks like there’s nothing there again. Have no fear, we merely moved your file into your s3 bucket. Let’s try listing the contents of your s3 bucket now.

aws s3 ls s3://bucket-userXXXXXXX

2022-02-04 06:45:30 26 hemingway.txt

Note

mv stands for move. mv is used to move one or more files or directories from one place to another in a file system like UNIX. cp stands for copy and is used to copy one or more files or directories from one place to another.

Exercise

How would you copy the hemingway.txt file from the s3 bucket back to your CloudShell?

Answer

aws s3 cp s3://bucket-userXXXXXXX/hemingway.txt .

In the next episode, you will learn how to utilize the AWS CLI within an EC2 instance and use that to create a fun research workflow!