Skip to main content

AWS CLI v2 Installation

What is AWS CLI?​

AWS CLI (Command Line Interface) is Amazon's official command-line tool for interacting with AWS services. It allows you to execute commands to manage your AWS resources from a terminal.

┌─────────────────────────────────────────────────────────────┐
│ AWS CLI │
├─────────────────────────────────────────────────────────────┤
│ │
│ Terminal ──► aws <service> <action> <options> │
│ │ │ │ │
│ │ │ └── Parameters │
│ │ └── Command (create, list...) │
│ └── AWS Service (ec2, s3, lambda...) │
│ │
│ Examples: │
│ $ aws s3 ls # List S3 buckets │
│ $ aws ec2 describe-instances # List EC2 instances │
│ $ aws lambda list-functions # List functions │
│ │
└─────────────────────────────────────────────────────────────┘

Prerequisites​

Before installing AWS CLI, ensure you have:

  • Administrator rights on your machine
  • Internet connection
  • Functional Terminal/PowerShell

Installation on Windows​

Step 1: Download the installer

Download AWS CLI v2 from the official site:

Step 2: Run the installer

  1. Double-click the AWSCLIV2.msi file
  2. Follow the installation wizard
  3. Accept the terms of use
  4. Click "Install"
  5. Wait for the installation to complete

Step 3: Verify the installation

Open a new PowerShell and run:

aws --version

Expected result:

aws-cli/2.x.x Python/3.x.x Windows/10 exe/AMD64 prompt/off

Method 2: Via PowerShell (Alternative)​

# Download and install
msiexec.exe /i https://awscli.amazonaws.com/AWSCLIV2.msi /quiet

# Verify (new terminal)
aws --version

Installation on macOS​

# Download the package
curl "https://awscli.amazonaws.com/AWSCLIV2.pkg" -o "AWSCLIV2.pkg"

# Install
sudo installer -pkg AWSCLIV2.pkg -target /

# Verify
aws --version

Method 2: Via Homebrew​

# Install via Homebrew
brew install awscli

# Verify
aws --version

Installation on Linux​

Universal Method (all distributions)​

# Download
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"

# Unzip
unzip awscliv2.zip

# Install
sudo ./aws/install

# Verify
aws --version

# Clean up
rm -rf aws awscliv2.zip

Ubuntu/Debian​

# Install dependencies
sudo apt update
sudo apt install -y unzip curl

# Then follow the universal method above

AWS CLI Configuration​

Configuration for LocalStack​

Since we are using LocalStack (local AWS emulator), we will configure AWS CLI to point to LocalStack instead of real AWS.

Step 1: Create a LocalStack profile

aws configure --profile localstack

Enter the following values:

AWS Access Key ID [None]: test
AWS Secret Access Key [None]: test
Default region name [None]: us-east-1
Default output format [None]: json
Why "test"?

LocalStack does not require real credentials. The "test" values work perfectly.

Step 2: Create an alias (optional but recommended)

To avoid typing --endpoint-url every time:

Windows PowerShell:

# Add to PowerShell profile
notepad $PROFILE

# Add this line:
function awslocal { aws --endpoint-url=http://localhost:4566 --profile localstack $args }

macOS/Linux:

# Add to ~/.bashrc or ~/.zshrc
echo 'alias awslocal="aws --endpoint-url=http://localhost:4566 --profile localstack"' >> ~/.bashrc
source ~/.bashrc

Configuration Verification​

# View configuration
aws configure list --profile localstack

Expected result:

      Name                    Value             Type    Location
---- ----- ---- --------
profile localstack manual --profile
access_key ****************test shared-credentials-file
secret_key ****************test shared-credentials-file
region us-east-1 config-file ~/.aws/config

Configuration File Structure​

AWS CLI stores its configuration in two files:

~/.aws/
├── config # General settings (region, output)
└── credentials # Access keys (access key, secret key)

~/.aws/config File​

[default]
region = us-east-1
output = json

[profile localstack]
region = us-east-1
output = json

~/.aws/credentials File​

[default]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY

[localstack]
aws_access_key_id = test
aws_secret_access_key = test

Basic AWS CLI Commands​

Command Structure​

aws <service> <action> [--options]

Useful Commands​

CommandDescription
aws --versionAWS CLI version
aws helpGeneral help
aws <service> helpHelp for a service
aws configure listDisplay configuration
aws sts get-caller-identityVerify identity

Examples by Service​

S3 (Storage):

aws s3 ls                          # List buckets
aws s3 mb s3://my-bucket # Create a bucket
aws s3 cp file.txt s3://bucket/ # Copy a file

EC2 (Compute):

aws ec2 describe-instances         # List instances
aws ec2 describe-vpcs # List VPCs
aws ec2 describe-security-groups # List Security Groups

Lambda (Serverless):

aws lambda list-functions          # List functions
aws lambda invoke --function-name X out.txt # Invoke

Important Global Options​

OptionDescription
--profile <name>Use a specific profile
--region <region>Specify the region
--output <format>Output format (json, text, table)
--endpoint-url <url>Custom URL (for LocalStack)
--query <jmespath>Filter JSON output

Examples with Options​

# Use the localstack profile
aws s3 ls --profile localstack

# Change the region
aws ec2 describe-instances --region eu-west-1

# Table output
aws ec2 describe-instances --output table

# Filter with JMESPath
aws ec2 describe-instances --query 'Reservations[].Instances[].InstanceId'

Installation Test​

Without LocalStack (basic verification)​

# Verify version
aws --version

# Verify configuration
aws configure list --profile localstack

With LocalStack (after LocalStack installation)​

# Start LocalStack first (see next section)

# Test S3
aws --endpoint-url=http://localhost:4566 s3 ls --profile localstack

# Or with the alias
awslocal s3 ls

Troubleshooting​

Problem: "aws command not found"​

Windows:

  1. Close and reopen PowerShell
  2. Check PATH: $env:PATH
  3. Reinstall AWS CLI

macOS/Linux:

# Check location
which aws

# If empty, add to PATH
export PATH="/usr/local/bin:$PATH"

Problem: "Unable to locate credentials"​

# Check configuration
aws configure list

# If empty, reconfigure
aws configure --profile localstack

Problem: "Connection refused"​

LocalStack is probably not started. See the next section.

Expected Screenshot​

After a successful installation, you should see:

PS C:\Users\YourName> aws --version
aws-cli/2.15.0 Python/3.11.6 Windows/10 exe/AMD64 prompt/off

PS C:\Users\YourName> aws configure list --profile localstack
Name Value Type Location
---- ----- ---- --------
profile localstack manual --profile
access_key ****************test shared-credentials-file
secret_key ****************test shared-credentials-file
region us-east-1 config-file ~/.aws/config

Validation Checklist​

  • AWS CLI v2 installed (aws --version works)
  • "localstack" profile configured
  • ~/.aws/config and ~/.aws/credentials files created
  • awslocal alias created (optional)

Quick Reference for AWS CLI Commands​

General Syntax​

aws [service] [operation] [parameters] [options]

Example:

aws iam get-policy --policy-arn arn:aws:iam::123456789:policy/MyPolicy

EC2 Commands​

ActionCommand
Modify an instanceaws ec2 modify-instance-attribute --instance-id i-123 --instance-type m5.xlarge
Create an AMIaws ec2 create-image --instance-id i-123abc --name "Base_Image"
Copy AMI to another regionaws ec2 copy-image --source-image-id ami-123 --source-region us-east-1 --region eu-west-1 --name "prod-server"
Register an AMIaws ec2 register-image --root-device-name /dev/sda1 --name "linux_ami"

EBS Commands​

ActionCommand
Create a volumeaws ec2 create-volume --size 80 --availability-zone us-east-1a --volume-type gp3
Attach a volumeaws ec2 attach-volume --volume-id vol-123 --instance-id i-abc --device /dev/sdf

S3 Commands​

ActionCommand
Create a bucket (API)aws s3api create-bucket --bucket my-bucket
Create a bucket (simple)aws s3 mb s3://my-bucket
List bucketsaws s3 ls
Copy a fileaws s3 cp file.txt s3://my-bucket/
Synchronizeaws s3 sync ./local s3://my-bucket/

IAM Commands​

ActionCommand
List usersaws iam list-users
Get a policyaws iam get-policy --policy-arn arn:aws:iam::123:policy/X
List rolesaws iam list-roles

Metadata and UserData (from an EC2 instance)​

# Retrieve instance metadata
curl http://169.254.169.254/latest/meta-data/

# Metadata examples
curl http://169.254.169.254/latest/meta-data/instance-id
curl http://169.254.169.254/latest/meta-data/public-ipv4
curl http://169.254.169.254/latest/meta-data/ami-id

# Retrieve user-data (startup script)
curl http://169.254.169.254/latest/user-data

Useful Global Options​

OptionDescriptionExample
--profileUse a profileaws s3 ls --profile prod
--regionSpecify the regionaws ec2 describe-instances --region eu-west-1
--outputOutput formataws ec2 describe-instances --output table
--queryFilter JSONaws ec2 describe-instances --query 'Reservations[].Instances[].InstanceId'
--dry-runTest without executingaws ec2 run-instances --dry-run

Output Formats​

# JSON (default)
aws ec2 describe-instances --output json

# Readable table
aws ec2 describe-instances --output table

# Raw text
aws ec2 describe-instances --output text
Tip

Keep this reference handy! These commands are used daily by AWS administrators.


Validation Quiz​

  1. Which command checks the AWS CLI version?
  2. Where are AWS credentials stored?
  3. What is the format of an AWS CLI command?
  4. Why do we use --endpoint-url with LocalStack?
  5. Which command lists S3 buckets?

Next section: Complete AWS CLI Cheatsheet