Installing AWS CLI v2 on Linux: A Step-by-Step Guide

Working with AWS services requires a reliable way to interact with your cloud resources from the command line. The AWS Command Line Interface (CLI) is the official tool for this purpose, and version 2 brings numerous improvements over its predecessor. In this guide, I'll walk you through automating the installation of AWS CLI v2 on Linux systems using a simple bash script.
Why Use AWS CLI v2?
Before diving into the installation, let's understand why AWS CLI v2 is worth having:
Improved Performance: AWS CLI v2 offers faster execution times for many commands
New Features: Access to the latest AWS services and command functionalities
Better Usability: Enhanced auto-completion and command help system
IAM Identity Center Integration: Simplified login for AWS organizations using SSO
Interactive Parameters: AWS CLI v2 can prompt for required parameters when not provided
The Installation Script
Here's a bash script that handles the AWS CLI v2 installation process, including checking if it's already installed:
#!/bin/bash
# Check if AWS CLI v2 is installed
if aws --version 2>/dev/null | grep -q 'aws-cli/2'; then
echo "AWS CLI v2 is installed"
else
echo "Installing AWS CLI v2"
# Download AWS CLI v2
echo "Downloading AWS CLI v2"
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
# Install unzip if not already installed
if ! command -v unzip &> /dev/null; then
echo "Installing unzip"
sudo apt update
sudo apt install -y unzip
fi
# Unzip the downloaded file
echo "Unzipping AWS CLI v2"
unzip awscliv2.zip
# Install AWS CLI v2
echo "Installing AWS CLI v2"
sudo ./aws/install
# Clean up the downloaded files and unzipped folder
echo "Cleaning up"
rm -rf awscliv2.zip aws
# Verify the installation
echo "Installation complete"
aws --version
fi
Understanding the Script
Let's break down each section of this script to understand what's happening:
1. Checking for Existing Installation
# Check if AWS CLI v2 is installed
if aws --version 2>/dev/null | grep -q 'aws-cli/2'; then
echo "AWS CLI v2 is installed"
else
# Installation code here...
fi
This section checks if AWS CLI v2 is already installed on your system. It runs aws --version
and searches for "aws-cli/2" in the output. If found, it means AWS CLI v2 is already installed, and the script simply confirms this and exits. The 2>/dev/null
redirects any potential error messages to null, preventing them from appearing in the terminal.
2. Downloading the Installer
# Download AWS CLI v2
echo "Downloading AWS CLI v2"
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
If AWS CLI v2 isn't installed, the script downloads the installer package from the official AWS source. It uses curl
to fetch the zip file and saves it as "awscliv2.zip" in the current directory.
3. Ensuring Dependencies are Met
# Install unzip if not already installed
if ! command -v unzip &> /dev/null; then
echo "Installing unzip"
sudo apt update
sudo apt install -y unzip
fi
The AWS CLI installer comes as a zip file, so we need the unzip
utility to extract it. This part checks if unzip
is available, and if not, installs it using the apt package manager. This makes the script more robust by ensuring all dependencies are met.
4. Extracting and Installing
# Unzip the downloaded file
echo "Unzipping AWS CLI v2"
unzip awscliv2.zip
# Install AWS CLI v2
echo "Installing AWS CLI v2"
sudo ./aws/install
After downloading, the script extracts the zip file and runs the installer with sudo privileges, which allows it to install the CLI tools system-wide.
5. Cleanup and Verification
# Clean up the downloaded files and unzipped folder
echo "Cleaning up"
rm -rf awscliv2.zip aws
# Verify the installation
echo "Installation complete"
aws --version
Finally, the script removes the temporary files (the zip file and extracted directory) to keep your system clean. It then verifies the installation was successful by running aws --version
, which should now show the installed AWS CLI v2 version.
Getting Started with AWS CLI
Now that you have AWS CLI v2 installed, here's how to set it up for use:
1. Configure AWS Credentials
Before you can use the AWS CLI, you need to configure it with your AWS credentials:
aws configure
You'll be prompted to enter:
AWS Access Key ID
AWS Secret Access Key
Default region name (e.g., us-east-1)
Default output format (json, text, or table)
2. Basic AWS CLI Commands
Here are some examples of common AWS CLI commands to get you started:
List all S3 buckets:
aws s3 ls
Create a new S3 bucket:
aws s3 mb s3://my-new-bucket
Start an EC2 instance:
aws ec2 start-instances --instance-ids i-1234567890abcdef0
View your Lambda functions:
aws lambda list-functions
Best Practices for AWS CLI Usage
Use IAM Roles: When possible, use IAM roles instead of access keys
Set Up Profiles: Use multiple profiles for different AWS accounts
aws configure --profile project1aws s3 ls --profile project1
Query Output: Use the --query parameter to filter results
aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId,State.Name]' --output table
Use MFA: Configure the CLI to use Multi-Factor Authentication for added security
Conclusion
Installing AWS CLI v2 on Linux is a straightforward process, especially when automated with a script like the one we've examined. This tool opens up the entire AWS ecosystem to your command line, making it easier to manage resources and automate workflows.
Whether you're managing infrastructure as code, building CI/CD pipelines, or simply need quick access to your AWS resources, the AWS CLI is an essential tool in your cloud development toolkit.
What AWS services do you manage using the CLI? Do you have favorite commands or shortcuts that make your workflow more efficient? Share your experiences in the comments below!
Note: Remember to keep your AWS credentials secure and never share your access keys. Consider using environment variables, AWS IAM roles, or the AWS credentials file with appropriate permissions.
Subscribe to my newsletter
Read articles from shrinivas joshi directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
