What is AWS 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.

Versions of AWSCLI:

Version 1.x : The generally available version of AWS CLI, Which can be used in Production environments.

Version 2.x : This is the preview version of AWS CLI which is suitable to be used in Testing Environments.

Example:

Here is the command which is used to copy files from local machine to S3 bucket using AWS CLI.

aws s3 cp filename s3://bucketname/

Installing AWS CLI:

Method 1: Installing using Bundled Installer.

For offline or automated installations on Linux and MacOS, You can use this Method.

Prerequisites;

OS: Linux , MacOS

Python: Python 2 version 2.7+ or Python 3 version 3.4+

You can downloaded the bundler directly using the below link:

https://s3.amazonaws.com/aws-cli/awscli-bundle.zip

We will use curl to download and install using below commands:

curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip"

Extract the files from the bundle:


unzip awscli-bundle.zip

Run the script:


sudo ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws

Check the installation using the command,

aws --version
AWS CLI

Method 2: Installing using PIP

The pip package manager for Python provides an easy way to install, upgrade, and remove Python packages and their dependencies.

If you already have a pip installed in your machine , with support python version, Install AWS CLI using pip3 If you are using python 3.x+.

If you don’t have python installed in your machine.Install it using command,

apt-get install python2.7
pip3 install awscli --upgrade --user (0r)
pip install awscli

Check the installation using the command,

aws --version

Now, We have installed AWS CLI in ubuntu.

Configure AWS CLI:

Now we have to configure awscli , For that we need AWS access_key and secret_key. If you dont have, You can go ahead and create it under an IAM users.

To do so, Login to IAM Console,

IAM CONSOLE

Choose Users,

AWS CLI

Go to, Security Credentials, Under Access Keys, click create access key

AWS CLI

You can find access and secret key, You can also download as .csv.

Now run the following command to configure AWS CLI,

aws configure

It will access for ACCESS_KEY , SECRET_KEY and AWS_REGION.

AWS CLI

AWS CLI is installed.

You can find the .aws/credentials file in the users home directory.

How to Use Credentials file for Terraform:

You can configure multiple profile with different AWS API keys in credentials file as shown below:

[default]
aws_access_key_id = AKIAY3GUEWGWVTEXAMPLE2
aws_secret_access_key = TqzHNX3Pst1GqZusqUkRfwFFEXAMpLecPb4M9Ry
[profile1]

aws_access_key_id = AKIA6SNSQUGWVTEXAMPLE2
aws_secret_access_key = TqzHNX3Pst1GqZusqUkRfwFFEXAMpLecPb4M9Ry

If you want other tools such as TERRAFORM to use these Profile, We need to configure environment variable.

export AWS_PROFILE=profile1

We need to modify terraform main.tf script as shown below:

provider "aws" {
access_key = "${var.profile}"
secret_key = "${var.profile}"
region = "${var.region}"
}

Then we need to define variable in creds.tf:

variable "profile" {
description = "aws credentials for terraform"
}
variable "region" {
default = "ap-southeast-1"
}

if I run terraform commands, It will ask to enter the profile you want to use.

We have worked on how to install AWS CLI, Configure multiple Profile, and how to Use profiles for Terraform.