Skip to content

How to install and configure AWS CLI on ubuntu

AWS is an excellent cloud service provider. One of the reasons for its popularity is the easy to use web interface and the wide range of services offered.

AWS also provides a rich set command line based tool to access different services. These handy tools enable developers to automate, configure and manage AWS cloud resources from a Linux terminal.

In this post, we'll walk through the process of installing and configuring the AWS CLI and see an example of how easy it use to create and manage AWS resources.

What is the AWS CLI

AWS CLI is a tool that pulls all the AWS services together in one central console, enabling developers to easily control and configure multiple AWS services using a command line interface.

It reduces and in some cases eliminates the dependency of navigating & interacting with AWS Management Console.

The AWS CLI is an open source tool built on top of the AWS SDK for Python (Boto) that provides commands for interacting with AWS services. With minimal configuration, you can start using all of the functionality provided by the AWS Management Console from your terminal.

How to install AWS CLI on ubuntu

To install AWS CLI you will need an AWS account, in order to get an access key ID and secret access key and Python installed on your ubuntu development desktop.

There are two methods you can choose to install the AWS CLI on your Ubuntu Laptop, both will deliver the same result so you can choose whichever is best for you. We'll provide both so can choose.

How to install the AWS CLI using Homebrew

My preferred method of installing the AWS CLI these days has to be making use of LinuxBrew which is essentially Homebrew for Linux , I discuss How To Use Homebrew Package Manager On Linux and how to start making use of this great package manager.

Installing the aws cli using homebrew is really easy.

brew install awscli

After Brew has completed installing the awscli you can go directly to Configure AWS Cli

How to install the AWS CLI using PIP

To Install the AWS ClI simply open a terminal window and execute the following command:

pip install awscli upgrade user

Once the command has completed you can then ensure the $PATH to AWS CLI is configured which enables you to globally use the command: aws. You can do this in one line command using :

echo "export PATH=~/.local/bin:$PATH" >> ~/.bash_profile

ensure you refresh your bash profile by either closing the terminal window and reopening it or by using

 source ~/.bash_profile

How to Install AWS CLI on Ubuntu via Ubuntu repository

Alternatively, you can install the AWS CLI directly from the default Ubuntu repositories.

sudo apt install awscli

Once install has completed you can confirm the version using

aws --version

The console will respond with something similar too

aws-cli/1.16.91 Python/3.6.7 Linux/4.15.0-46-generic botocore/1.12.81

Warning

The version of AWS CLI available in the Ubuntu Package repository may not be the most up to date version. So if you want to ensure you're using the most up to date version it is probably better to install the AWS CLI using pip.

Configure AWS Cli

Before using aws-cli, you need to configure your AWS credentials. You can do this in several ways:

  • Environment variables
  • Shared credentials file
  • Config file
  • IAM Role

The quickest way to get started is to execute aws configure which will configure your credentials to use for all our AWS configuration workflows.

aws configure -- profile gary_woodfine

Check out the Configure AWS CLI with Multiple accounts

You will be prompted for your credentials. You will need to create IAM account details. You should not use your AWS Root account details.

AWS Access Key ID [None]: AKIATHREENINEEXAMPLE
AWS Secret Access Key [None]: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
Default region name [None]: eu-west-2
Default output format [None]: json

After completion of this step the AWS CLI has now been configured on your machine and ready for use.

Detailed information of the aws configure check out the Configuring the AWS CLI

Quick example to test AWS CLI

In order to illustrate how easy it is to use the AWS CLI to manage and configure AWS resources we will run through the process of creating and deleting an Amazon Simple Storage (S3) container.

The first step to check out what is available is simply to call up the help file for the aws s3 commands

aws s3 help

We can use the AWS CLI to list all the S3 buckets we have access too with our account

aws s3 ls

all your S3 bucket names will be displayed in your terminal window.

We can create a bucket using the following command and optionally set the region if you would like to create a bucket in a region other than the default region you configured.

Create S3 bucket using aws terminal

 aws s3 mb s3://threenine-test-bucket-sample --region eu-west-2

Remove S3 bucket using terminal

We can also delete the bucket by issuing a similar command but changing it slightly to use rb

 aws s3 rb s3://threenine-test-bucket-sample --region eu-west-2

Copy entire directory of files to S3 bucket using terminal

If you have an entire directory of files you want to load up to test you don't have to add them line by line. The AWS CLI comes with some great flexible option switches you can use to simply load up all documents in the in a directory.

An example of this, say you wanted to load up all the files in your current working directory to an S3 bucket. You could use the following command with the additional --recursive option

aws s3 cp ./  s3://threenine-test-bucket-sample --recursive

Download files from S3 using terminal

If you want to download all the files from our S3 bucket you can easily do so using the sync command

aws s3 sync s3://threenine-test-bucket-sample ~/threenine-bucket 

Benefits of the AWS CLI

The main benefits of the AWS CLI, that it saves developers time and frustration from having to the AWS Management Console. Thus enabling easier installs, support of all services, automating processes and commands from a familiar terminal command environment.

  • Easier to install. Smooth, quick, simple, and standardized.
  • Supports all Amazon Web Services. you control all the services from one simple tool.
  • Saves time. Developers find it faster and easier to use the AWS CLI once they reach a certain level of proficiency.
  • Scripting. The ability to automate control of all Amazon's web services with scripts. Shell scripts make it easy to fully automate cloud infrastructure.

Configure AWS CLI with Multiple accounts

Developers will typically have several accounts to use across several environments. Fortunately, AWS CLI enables you to configure several profiles to use.

AWS CLI enables you to configure multiple named profiles that are stored in the config and credentials files.

You can configure additional profiles by using aws configure with the --profile option, or by adding entries to the config and credentials files.

If you want to create profile for development then simply execute the configure with the switch --profile

aws configure --profile development

You can then add your development specific AWS Key and Secret and Region details. You can then configure your application to make use of the specific profile should you require.

Developers will often and preferably should have different AWS Keys and Secret to the actual Production keys being utilised. Undoubtedly developers should have separate environments they use for executing, running and testing i.e. Development, Staging and Production Environments.

In all likelihood these environments will connect to different resources so as not to corrupt the Production Data etc.

In .net core you can configure the AWS SDK to retrieve the desired Profile information from the Environment Specific AppSettings files. For instance, you can create an appsettings.Development.json to make use of the Development Profile.

{
  "AWS": {
    "Profile": "development",
    "Region": "eu-west-2"
  }
}

The convention for all developers to create a development profile eliminates the need for storing AWS Keys and Secrets in the App Settings.

This can also easily be achieved in Node, PHP, Ruby, Go and Python environments by following a similar strategy and making use of those languages configuration options.

Disadvantages of the AWS CLI

  • Your Access Keys are stored in plain text on disk. This is NOT secure.
  • You're always using permanent Access Keys for auth rather than Temporary Access Keys that are rotated.
  • Your credentials sit around on disk forever and the default Named Profile gets used for all commands if you forget to specify a different one, which is error prone.

Summary

The AWS Command Line Interface (CLI) is a unified tool to manage your AWS services enabling you with just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts.

Gary Woodfine
Latest posts by Gary Woodfine (see all)