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
Method 1: MSI Installer (Recommended)
Step 1: Download the installer
Download AWS CLI v2 from the official site:
Step 2: Run the installer
- Double-click the
AWSCLIV2.msifile - Follow the installation wizard
- Accept the terms of use
- Click "Install"
- 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
Method 1: Official Package (Recommended)
# 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
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
| Command | Description |
|---|---|
aws --version | AWS CLI version |
aws help | General help |
aws <service> help | Help for a service |
aws configure list | Display configuration |
aws sts get-caller-identity | Verify 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
| Option | Description |
|---|---|
--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:
- Close and reopen PowerShell
- Check PATH:
$env:PATH - 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 --versionworks) - "localstack" profile configured
- ~/.aws/config and ~/.aws/credentials files created
-
awslocalalias 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
| Action | Command |
|---|---|
| Modify an instance | aws ec2 modify-instance-attribute --instance-id i-123 --instance-type m5.xlarge |
| Create an AMI | aws ec2 create-image --instance-id i-123abc --name "Base_Image" |
| Copy AMI to another region | aws ec2 copy-image --source-image-id ami-123 --source-region us-east-1 --region eu-west-1 --name "prod-server" |
| Register an AMI | aws ec2 register-image --root-device-name /dev/sda1 --name "linux_ami" |
EBS Commands
| Action | Command |
|---|---|
| Create a volume | aws ec2 create-volume --size 80 --availability-zone us-east-1a --volume-type gp3 |
| Attach a volume | aws ec2 attach-volume --volume-id vol-123 --instance-id i-abc --device /dev/sdf |
S3 Commands
| Action | Command |
|---|---|
| Create a bucket (API) | aws s3api create-bucket --bucket my-bucket |
| Create a bucket (simple) | aws s3 mb s3://my-bucket |
| List buckets | aws s3 ls |
| Copy a file | aws s3 cp file.txt s3://my-bucket/ |
| Synchronize | aws s3 sync ./local s3://my-bucket/ |
IAM Commands
| Action | Command |
|---|---|
| List users | aws iam list-users |
| Get a policy | aws iam get-policy --policy-arn arn:aws:iam::123:policy/X |
| List roles | aws 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
| Option | Description | Example |
|---|---|---|
--profile | Use a profile | aws s3 ls --profile prod |
--region | Specify the region | aws ec2 describe-instances --region eu-west-1 |
--output | Output format | aws ec2 describe-instances --output table |
--query | Filter JSON | aws ec2 describe-instances --query 'Reservations[].Instances[].InstanceId' |
--dry-run | Test without executing | aws 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
Keep this reference handy! These commands are used daily by AWS administrators.
Validation Quiz
- Which command checks the AWS CLI version?
- Where are AWS credentials stored?
- What is the format of an AWS CLI command?
- Why do we use
--endpoint-urlwith LocalStack? - Which command lists S3 buckets?
Next section: Complete AWS CLI Cheatsheet