Exercise: Installing AWS CLI

Natasha Ong
This is some text inside of a div block.
4 min read

Exercise Overview:

In this exercise, you will learn how to install the AWS CLI in your operating system (OS).

By the end of this exercise, you should be able to do the following:

  • Install the AWS Command Line Interface (AWS CLI) for your operating system
  • Configure the credentials to access your AWS account
  • Configure multiple profiles to access different AWS accounts

Key concepts:

An AWS Command Line Interface (AWS CLI) is a unified tool that provides a consistent interface for interacting with all parts of Amazon Web Services (AWS) directly from the command line. It allows users to control multiple AWS services and automate them through scripts.

There are different ways to install the AWS CLI, depending on your operating system (OS) or preference to use containers.

To install the AWS CLI v2: lnstalling or updating the latest version of the AWS CLI.

Task 1: Installing AWS CLI

In this task, you will install AWS CLI to your local computer with macOS, Windows OS, and Linux OS.

This task looks super long, but you're not doing all of it - make sure you're only focusing on the OS that's relevant to you!

For Windows OS

Here's the procedure on how you will install the AWS CLI on your Windows OS computer. If you're not using Windows OS, you can skip this.

1. Download and run the AWS CLI MSI installer for Windows (64-bit): https://awscli.amazonaws.com/AWSCLIV2.msi

2. Locate the file from your Downloads folder or any folder that you store the file, once located, double-click the file.

3. Click Run.

4. Wait a couple of seconds if you encounter this. Once the Next button is available, click Next.

5. Accept the License Agreement, and then choose Next.

6. On the Custom Setup, just click Next. It indicates that the AWS CLI v2 is the feature to be installed, requiring 10MB of space on the hard drive. The default installation location is "C:\Program Files\Amazon\AWSCLIV2". You have the option to change the installation directory.

7. Finally, click Install. For some users, User Account Control will appear. Click Yes to continue the installation.

8. Once the AWS CLI is installed, you can run aws --version command in your command line and see the following output (the version may be different). You may run this through Windows Powershell or Command Prompt (CMD).

Command Prompt:

Windows Powershell:

For Linux OS

Here's how you will install AWS CLI on your Linux OS computer. If you're not using Linux OS, you can skip this.

1. Download the installation file. Follow the command below.

2. Unzip the installer. The following example command unzips the package and creates a directory named aws under the current directory.

3. Run the install program. The installation command uses a file named install in the newly unzipped aws directory. By default, the files are all installed to /usr/local/aws-cli, and a symbolic link is created in /usr/local/bin. The command includes sudo to grant write permissions to those directories.

You can install without sudo if you specify directories that you already have write permissions to. Use the following instructions for the install command to specify the installation location:

  • Ensure that the paths you provide to the -i and -b parameters contain no volume name or directory names that contain any space characters or other white space characters. If there is a space, the installation fails.
  • --install-dir or -i – This option specifies the directory to copy all of the files to.
  • The default value is /usr/local/aws-cli.
  • --bin-dir or -b – This option specifies that the main aws program in the install directory is symbolically linked to the file aws in the specified path. You must have write permissions to the specified directory. Creating a symlink to a directory that is already in your path eliminates the need to add the install directory to the user's $PATH variable.
  • The default value is /usr/local/bin.

4. Once the installation is finished, confirm the installation with the following command.

If the aws command cannot be found, you might need to restart your terminal or follow the troubleshooting in Troubleshoot AWS CLI errors.

For macOS

  1. In your browser, download the macOS pkg file: https://awscli.amazonaws.com/AWSCLIV2.pkg
  2. Run your downloaded file and follow the on-screen instructions.

3. You can choose to install the AWS CLI in the following ways. The access permissions will be different depending on your choice, we recommend going with Install for all users of this computer (as the second and third require a complex setup process).

4. Press Continue until you reach the end, and press OK when you reach the pop-up about accessing files in your Downloads folder.

5. You should reach a success page!

Phew! That was a bit of a mission. We promise it gets easier from here.

Task 2: Enabling AWS CLI to your IAM users

In this task, you will enable the AWS CLI access to the IAM user that you will use.

  1. Ensure you log in first with your root user.
  2. Navigate to the IAM console, on the left side choose Users.
  3. Choose the IAM user that you want to give access to AWS CLI.
  4. Click the Security credentials tab. Scroll down a little bit and you will find Access keys.
  5. On the Access keys section, choose Create access key.
  6. On the Access key best practices & alternatives setup, choose Command Line Interface (CLI), and then check the box for confirmation, and choose Next.
  7. Choose Create access key.
  8. On the next page, you'll have the option to copy the Access key and Secret access key paste it into text editor, and save it somewhere else safe. Or download the CSV file which contains both credentials.
  9. Click Done.

Task 3: Access the AWS environment using AWS CLI

Now that you've created the access keys for your IAM users, you can access your AWS resources through AWS CLI. Everything you do in the AWS Management Console you can do as well using AWS CLI.

1. To access your AWS resources using AWS CLI, open a terminal (for macOS and Linux) or Windows Powershell, Command Prompt (for Windows).

2. Type the command aws configure and it will ask for the following:

  • AWS Access Key ID: This is the Access Key ID that you saved earlier.
  • Secret Access Key. This is the Secret Access Key that you saved earlier.
  • Default region name: Type here the region code where you want to access your resources. For example, if you want to access your EC2 instance in the Asia Pacific (Sydney) type the region code which is ap-southeast-2, and hit enter.
  • Default output format: This sets the format in which results from AWS CLI commands will be displayed. If set to JSON, the CLI will format the command output as JSON objects. This is useful for integrating with other tools that can parse JSON or for direct human readability. Enter json

Sick! You have now access to your AWS resources through CLI.

This is what it looks like on a Windows CLI.

This is what it looks like on a Mac CLI.

Alternative: AWS CloudShell

Aside from Windows Powershell or terminal for macOS/Linux OS, there's an alternative way to interact with your AWS resources. This is the AWS CloudShell.

Recap: AWS CloudShell is a browser-based command line interface (CLI) provided by AWS that allows you to directly manage and interact with AWS resources. It comes with a set of tools installed, such as the AWS CLI.

1. To navigate to AWS CloudShell, just simply click the CloudShell icon in the upper right corner beside the notification icon or in the lower left-hand corner of your AWS Management Console home page.

2. You can start by running the command aws configure. Similar to what you did in the previous task.

Congratulations! You've completed the hands-on exercise! You have successfully:

  • Installed the AWS Command Line Interface (AWS CLI) for your operating system
  • Configured the credentials to access your AWS account
  • Configured multiple profiles to access different AWS accounts

Let that sink in - you've just used a completely new way to interact with your AWS resources! That's freaking awesome. Next, let's dive into how we can use it to manage a resource.