Home/Engineer/Cloud/AWS CLI v2: Installing, Sett…
CloudBeginnerUbuntuDebianCentOSmacOSAWSAWS-CLIEC2

AWS CLI v2: Installing, Setting Up Profiles and Credentials, and Everyday EC2, S3, and IAM Commands

A practitioner-focused walkthrough of AWS CLI v2 installation, profile setup, and the EC2, S3, and IAM commands you use most in day-to-day work.

Contents
  1. Installation
  2. Credential Setup
  3. EC2
  4. S3
  5. IAM
  6. Useful Options

Installation

Bash
# Linux (x86_64)
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

# Check version
aws --version

Credential Setup

Bash
# Interactive setup (Access Key, Secret, Region, output format)
aws configure

# Per-profile setup (multiple accounts)
aws configure --profile prod
aws configure --profile dev

# Temporary setup via environment variables
export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
export AWS_DEFAULT_REGION=ap-northeast-2

Credential file locations: ~/.aws/credentials, ~/.aws/config


EC2

Bash
# List instances (name, state, IP)
aws ec2 describe-instances \
  --query 'Reservations[*].Instances[*].[Tags[?Key==`Name`].Value|[0],State.Name,PublicIpAddress]' \
  --output table

# Start/stop instances
aws ec2 start-instances --instance-ids i-1234567890abcdef0
aws ec2 stop-instances --instance-ids i-1234567890abcdef0

# Add a security group rule (allow port 22 from my IP)
MY_IP=$(curl -s ifconfig.me)
aws ec2 authorize-security-group-ingress \
  --group-id sg-12345678 \
  --protocol tcp --port 22 --cidr "$MY_IP/32"

S3

Bash
# List buckets
aws s3 ls

# Upload a file
aws s3 cp local-file.txt s3://my-bucket/path/

# Sync a directory
aws s3 sync ./dist s3://my-bucket/static --delete

# List objects in a bucket
aws s3 ls s3://my-bucket/ --recursive --human-readable

# Download a file
aws s3 cp s3://my-bucket/backup.tar.gz .

# Generate a presigned URL (valid for 24 hours)
aws s3 presign s3://my-bucket/private-file.pdf --expires-in 86400

IAM

Bash
# Check current credentials
aws sts get-caller-identity

# List users
aws iam list-users --output table

# List roles
aws iam list-roles --query 'Roles[*].[RoleName,Arn]' --output table

Useful Options

Bash
# Use a specific profile
aws s3 ls --profile prod

# Output format (json/yaml/table/text)
aws ec2 describe-instances --output yaml

# Specify a region
aws ec2 describe-instances --region us-east-1

# Parse with jq
aws ec2 describe-instances | jq '.Reservations[].Instances[].PublicIpAddress'
#AWS#AWS-CLI#EC2#S3#Cloud
Editorial note

AI tools may assist drafting. Check the evidence in each guide for whether commands were run and which environment was used. Confirm the current docs for your version before applying instructions, and email us about errors. email us

Topic-related official docs (auto-linked · not per-claim sources)AWS documentation ↗

Questions & answers

Ask a question about this guide. We'll review and reply.