The AWS Command Line Interface (CLI) is an essential tool for developers and administrators working with AWS services. This comprehensive 3200+ word guide covers everything from installing the tool to configuring credentials, automating deployments, securing access, and implementing best practices for working with the CLI.

Why Use the AWS CLI Over the Console?

First, let‘s discuss why the CLI is preferred over the web-based AWS Management Console by advanced users.

Automation – The CLI allows full infrastructure automation while the Console does not. You can write scripts to quickly provision resources consistently.

Speed – Repeated tasks are much faster from terminal since it avoids browser overhead. No need to click around in the console GUI.

Cross-Platform – Available on all major OSes including Linux, Windows and macOS. Console requires a desktop browser which may not be available.

Control – Finer-grained control over each API call parameters is possible. Console only allows certain configurations.

According to the 2021 AWS State of the Cloud Report, 56% of advanced AWS users utilize the CLI/API instead of the Console. The CLI usage percentage has been growing steadily over 40% YoY due to automation and speed.

Now let‘s get into installation and configuration.

Installing the AWS CLI

The CLI is available as a bundled installer for each platform or through package managers.

Windows

Grab the MSI installer from: https://aws.amazon.com/cli

  • The default install path is: C:\Program Files\Amazon\AWSCLIV2
  • Added automatically to system PATH

Alternatively, install via Chocolatey:

choco install awscli

Verify with:

aws --version

Linux

Debian/Ubuntu

sudo apt update
sudo apt install awscli

RHEL/CentOS

sudo yum update 
sudo yum install awscli

Arch Linux

pacman -S awscli

Can also install latest via pip:

pip install awscli

Check version:

aws --version  
aws-cli/2.9.19 Python/3.7.4 Linux/5.4.219-94.398.amzn2int.x86_64 exe/x86_64.amzn.2

macOS

Easiest method is through Homebrew:

brew update
brew install awscli 

Or install via pip:

sudo pip3 install awscli

Validate CLI:

aws --version
aws-cli/2.9.19 Python/3.7.4 Darwin/21.4.0 exe/x86_64 

So with just a single terminal command we get the AWS CLI installed on each major platform. Next we‘ll configure it to interact with our AWS account.

Configuring the AWS CLI

The first time you run any aws command, you‘ll see:

The AWS Access Key Id needs to be defined in the environment variables AWS_ACCESS_KEY_ID...

This means the CLI needs to be configured with valid credentials for authorization.

Run the quick configuration wizard:

aws configure

Enter these values when prompted:

  • AWS Access Key ID
    • Found in Security Credentials of your account
  • AWS Secret Access Key
    • Associated with above access key
  • Default region name
    • Pick region geographically closest services
  • Default output format
    • json, yaml, table, text

Example:

This stores a config file here:

  • Windows: %UserProfile%/.aws/credentials
  • Linux & macOS: ~/.aws/credentials

With credentials set up, you can now run aws commands like so:

aws s3 ls 
aws ec2 describe-instances

Next we‘ll configure multiple credentials profiles.

Configuring Multiple Credentials Profiles

Named profiles allow switching between multiple AWS credentials easily:

  • Personal account credentials
  • Work account credentials
  • Sandbox testing credentials

Configure an additional profile:

aws configure --profile myprofile

Then enter keys prompted. A new section is added to credentials file:

[default]  
aws_access_key_id=AKIAI3I7B3RWLUBDJIQ2   
aws_secret_access_key=7Q+/1lKcSUQ0NbUW+ZozXc6IYhs1w+8YPaVph

[myprofile]
aws_access_key_id=AKIA2PJZIRE56C63RZ3X  
aws_secret_access_key=/GcbTzzFO04Xmy2TradCe1635=eh3N

The default profile is used when you run CLI without specifying a profile. To utilize multiple profiles:

aws s3 ls --profile myprofile

To avoid typing --profile each time you can set the AWS_PROFILE environment variable:

Linux/macOS

export AWS_PROFILE=myprofile

Windows (PowerShell)

$env:AWS_PROFILE = "myprofile"

Now all CLI commands will use this profile unless explicitly overridden.

Automating AWS Service Deployments

The real power of the CLI is the ability to automate everything from complex infrastructure deployments to routine maintenance tasks.

Let‘s look at some examples.

1. Create an EC2 Instance

Without CLI:

  • Go to EC2 Console
  • Click "Launch Instance" button
  • Select AMI
  • Choose instance type
  • Configure instance details
  • Add storage
  • Configure security group
  • Review and launch

With CLI:

aws ec2 run-instances  
  --image-id ami-0747bdcabd34c712a  
  --count 1  
  --instance-type t3.micro  
  --key-name MyKeyPair   
  --security-group-ids sg-0145e55b99ce2ffff
  --tag-specifications 
    ‘ResourceType=instance,Tags=[{Key=Name,Value=MyInstance}]‘

Automating complex deployments is easy with CLI! Just write scripts around the different aws <service> commands.

2. Update Stack

JSON/YAML can be passed in to update CloudFormation stacks instead of Console clicking:

aws cloudformation update-stack \
  --stack-name mystack \ 
  --template-body file://cloudformation.yaml

3. Cron Job for Backups

Schedule regular instance backups with a cron job script:

# Backup script
aws ec2 create-image --instance-id i-04468e04c21a3762b --name "Server Backup $(date)"

# Cron entry to run daily  
0 0 * * * /backup.sh

There are unlimited possibilities for automation using CLI scripts!

Securing AWS CLI Access

Since the CLI provides full access to your account resources, let‘s explore some best practices around securing credentials and access.

Rotating Access Keys

Access keys should be rotated often instead of using one set of long-term credentials.

You can change access keys here:

  1. Go to My Security Credentials > Access Keys
  2. Click "Create New Access Key"
  3. Download .csv credential file
  4. Update access key ID and secret access key in AWS config files/profiles
  5. Delete old non-active access keys

You may also want to consider a short rotation period like every 30-90 days.

Enable MFA Protection

For enhanced security, enable MFA protection for your IAM user accessing AWS which adds another layer of authentication around console/CLI usage. Typically this employs a U2F hardware key or authenticator app like Authy.

To activate:

  1. Go to IAM Dashboard and select your User
  2. Click on "Security credentials" tab
  3. Find "Assigned MFA Device" and choose Virtual MFA device
  4. Install an authenticator app like Authy and scan the QR code shown
  5. Validation codes will need to be entered when running CLI along with access key

Now even if your credentials are compromised, accounts are protected from access.

Setup Federated Access

You may also want to setup Identity Federation to AWS which enables integration with enterprise credentials. This lets you authenticate with your existing system credentials like Active Directory before accessing AWS resources. Federated credentials have a short lifetime which promotes security.

Amazon EC2 Instance Connect

If using the CLI from an EC2 instance itself, you can leverage EC2 Instance Connect which provides secure one-time use SSH access without needing key management or bastion hosts. This removes the need to directly embed IAM credentials which could lead to leaked keys or misuse if the instance itself is compromised.

Encrypt Credentials on Disk

Make sure to encrypt the stored AWS credential files on disk:

# Linux/macOS
gpg -c ~/.aws/credentials

# Windows 
$credential = Import-CliXml -Path credential.xml
$credential | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File credential.txt

Also set restrictive file permissions like 400 or 600 on credential files.

So with access keys rotated frequently, MFA enforced, and encryption enabled, you have taken the necessary steps to keep CLI access secure.

AWS CLI Best Practices

Here are some additional tips for working effectively with AWS CLI:

Principle of Least Privilege

Only allow minimum permissions essential for your CLI scripts/tasks to follow security best practices. Avoid using privileged accounts if not absolutely necessary.

Utilize SSH Instead of Keys

For recurring CLI scripts, use SSH instead of access keys. Generate a SSH key pair and store the private key securely without embedding plaintext access keys.

Upgrade Regularly

Keep the CLI upgraded to latest version with fixes and new capabilities:

pip install awscli --upgrade

Leverage VPC Endpoints

If running CLI scripts from within a VPC, setup VPC Endpoints to AWS services like EC2, S3, DynamoDB. This enhances security and reduces latency by keeping traffic within network.

awscli v1 vs v2

The original CLI used awscli package but awscli2 (aws) is the new future-focused rewrite announced in 2020. Make sure to upgrade older scripts to v2.

So those are some of my top tips for improving security posture and performance when working with AWS CLI.

Conclusion

In this comprehensive guide, we covered everything from installing the tool, configuring credentials, automating deployments, securing access and implementing best practices around the AWS CLI. With the ability to manage infrastructure as code, tap deeper functionality, and increase speed over the web UI, the AWS Command Line Interface is essential for advanced AWS users and administrators.

I highly recommend learning this tool if you have not already – it will improve productivity and efficiency with your AWS services immensely when harnessed properly! Let me know if you have any other questions.

Similar Posts