How to install AWS CLI on Ubuntu

What is AWS CLI?

AWS Command Line Interface : 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. The official AWS documentation provides very brief process to install AWS CLI on different platforms such as Windows, Mac, Amazon Linux
 
Prerequisites:
Before you begin with this guide, you should have good knowledge about AWS EC2 service,working withdifferent Linux distributions such as Ubuntu, RHEL, CentOS, Amazon Linux.
Following document will help you install AWS CLI on Ubuntu (16.04)
There are few methods by which the installation can be done.
Method 1: Using apt repository to install aws cli
Method 2: Installing Using Python pip
 
Let’s get started.
 

Method 1: Using apt repository to install aws cli

Step 1: Check version of OS that you are working on. Use following command for that.
root@localhost:~# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION=”Ubuntu 16.04.2 LTS”
 
The easiest way to install it using apt-get command as shown below.
root@localhost:~# apt-get update && apt-get install awscli
Now let’s see the version of awscli that got installed.
root@localhost:~# aws –version
aws-cli/1.11.13 Python/3.5.2 Linux/4.4.0-1022-aws botocore/1.4.70
The only problem with this method of installing aws cli using apt-get is that you won’t be getting the very latest version. AWS CLI is continuously evolving (more and more services and features are added to it on a regular basis), and you won’t be getting the latest features and services if you install it via apt-get.
Now that we have installed AWS CLI we can configure it using following steps.

Configure AWS CLI (Optional)

Following section will guide you to configure settings that the AWS Command Line Interface uses when interacting with AWS, such as your security credentials and the default region.
                Prerequisites : You should have the access key ID and secret access key for an IAM user.
Know more about What Is IAM? and AWS Security Credentials
For general use, the aws configure command is the fastest way to set up your AWS CLI installation.
$ aws configure
AWS Access Key ID [None]: AAJHxxxxxxxxxxxxxxxxxxxQ
AWS Secret Access Key [None]: EasfAUH8jsdsa\xxxxxxxxxxxxxxxxxxxxxxxjSMAs
Default region name [None]: us-east-1
Default output format [None]: json
 
Note:
The AWS CLI signs requests on your behalf and includes a date in the signature. Ensure that your computer’s date and time are set correctly; if not, the date in the signature may not match the date of the request, and AWS will reject the request.
For more info on AWS CLI visit here
An alternate method is to install AWS CLI as a python package using pip.
Let’s install AWS CLI on ubuntu 16.04 using Python pip and then at the end we will walk through on configuring it and using it.

Method 2: Installing Using Python Pip

Step 1: Install python pip package manager. It can be achieved using the below command.
root@localhost:~# apt-get update && apt-get install python-pip
 
Step 2: Now install aws cli using pip command.
root@localhost:~# pip install awscli
Step 3: Verify the version that we installed using pip. It should be latest compared to the apt-get version we got earlier.
root@localhost:~# aws –version
aws-cli/1.11.128 Python/2.7.12 Linux/4.4.0-1022-aws botocore/1.5.9
 
Follow above guide to configure AWS CLI.
AWS configure command creates two configuration files in the home directory of the user (using which you fired the aws configure command).
This configuration file is located under “.aws” directory under the home directory.
root@localhost:~# cd .aws/
root@ip-10-12-2-254:~/.aws# ls
config  credentials
 
There are two files inside the “.aws” directory.
One is “config” and the other is “credentials”.
Config file contains configurations like the region and output format that we entered duringaws configure command. Credentials file contains the access key and the secret key.
 
root@ip-10-12-2-254:~/.aws# cat config
[default]
region = us-east-1
root@ip-10-12-2-254:~/.aws# cat credentials
[default]
aws_access_key_id = AAJHxxxxxxxxxxxxxxxxxxxQ
aws_secret_access_key = EasfAUH8jsdsa\xxxxxxxxxxxxxxxxxxxxxxxjSMAs
 
Now that we know what aws configure does, we can easily create the .aws directory(along with config and credentials files. This removes the need for us to manually fire aws configure command.)
Note :Remember the fact that only the user whose home directory has .aws with credentials and config files with appropriate content can execute aws related commands
Another alternative method is to configure two environment variables in the linux shell. One variable will be called AWS_ACCESS_KEY_ID and the other will be called AWS_SECRET_ACCESS_KEY. So basically without aws configure and without .aws directory with “config” and “credentials” file, you can simply fire the below two commands and start using aws cli to access your services.
root@localhost:~# export AWS_ACCESS_KEY_ID=AAJHxxxxxxxxxxxxxxxxxxxQ
root@loclahost:~# export AWS_SECRET_ACCESS_KEY=EasfAUH8jsdsa\xxxxxxxxxxxxxxxxxxxxxxx….
root@localhost:~# export AWS_DEFAULT_REGION=us-east-1
 

How to configure AWS CLI for all users in the system?

It is quite simple.
Step 1 : Open the file /etc/environment and add the below two lines(replace it with correct access key        and secret key).
ubuntu@localhost:~$ cat /etc/environment
PATH=”/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games”
AWS_ACCESS_KEY_ID=AAJHxxxxxxxxxxxxxxxxxxxQ
AWS_SECRET_ACCESS_KEY=EasfAUH8jsdsa\xxxxxxxxxxxxxxxxxxxxxxxjSMAs
AWS_DEFAULT_REGION=us-east-1
At this stage we have installed AWS CLI on Ubuntu using either Method 1 or Method 2 and we have also configured AWS CLI now we can explore aws  service’s capabilities with the AWS CLI, and develop shell scripts to manage your resources.
Contributed By:
Apurva Deodhare
https://www.linkedin.com/in/apurv-deodhar-7a944862/