AWS CLI Cheatsheet
AWS
CLI
Cheatsheet
Command Line
Note
Quick Reference - Most Used Commands
# Add a new AWS profile
aws configure --profile new-profile-name # Create new profile with access keys
aws configure sso --profile new-profile-name # Create new SSO profile (interactive)
# SSO setup process:
# 1. Run: aws configure sso --profile profile-name
# 2. Enter SSO start URL when prompted (e.g., https://your-org.awsapps.com/start)
# 3. Enter SSO region (e.g., us-east-1)
# 4. It will open browser for authentication
# 5. Select the account and role
# Sign in to AWS (for SSO profiles)
aws sso login --profile profile-name # Login to specific SSO profile
aws sso login # Login to default SSO profile
# List all AWS profiles/accounts on this computer
aws configure list-profiles # Show all configured profiles
# Check which account/profile you're currently using
aws sts get-caller-identity # Show current user/role
aws configure list # Show current profile configuration
# Switch between profiles
export AWS_PROFILE=profile-name # Set profile for current session
aws configure list # Verify the switch worked
# Use a specific profile for one command (without switching)
aws s3 ls --profile profile-name # Run single command with specific profile
# Clear profile (use default)
unset AWS_PROFILE # Remove profile overrideSetup
# Configure AWS CLI
aws configure # Interactive setup
aws configure set region us-east-1 # Set default region
aws configure set output json # Set output format
aws configure list # View current configuration
aws configure list-profiles # List all profiles
# Check current identity
aws sts get-caller-identity # Show current user/roleConfiguration & Profiles
# List all available AWS profiles/accounts
aws configure list-profiles # Show all configured profiles
cat ~/.aws/credentials | grep '\[' | tr -d '[]' # Alternative way to list profiles
aws configure list # Show current profile configuration
# Multiple profiles
aws configure --profile myprofile # Create named profile
aws s3 ls --profile myprofile # Use specific profile
# Environment variables
export AWS_PROFILE=myprofile # Set default profile
export AWS_REGION=us-west-2 # Set default regionS3 (Simple Storage Service)
# Bucket operations
aws s3 ls # List all buckets
aws s3 mb s3://my-bucket # Create bucket
aws s3 rb s3://my-bucket # Remove empty bucket
aws s3 rb s3://my-bucket --force # Remove bucket and all contents
# Object operations
aws s3 ls s3://bucket-name # List objects in bucket
aws s3 ls s3://bucket-name/folder/ # List objects in folder
aws s3 cp file.txt s3://bucket/ # Upload file
aws s3 cp s3://bucket/file.txt . # Download file
aws s3 mv file.txt s3://bucket/ # Move file to S3
aws s3 rm s3://bucket/file.txt # Delete file
# Sync operations
aws s3 sync ./local-folder s3://bucket/folder/ # Upload folder
aws s3 sync s3://bucket/folder/ ./local-folder # Download folder
aws s3 sync . s3://bucket --delete # Sync and delete removed filesEC2 (Elastic Compute Cloud)
# Instance management
aws ec2 describe-instances # List all instances
aws ec2 describe-instances --instance-ids i-xxx # Describe specific instance
aws ec2 start-instances --instance-ids i-xxx # Start instance
aws ec2 stop-instances --instance-ids i-xxx # Stop instance
aws ec2 reboot-instances --instance-ids i-xxx # Reboot instance
aws ec2 terminate-instances --instance-ids i-xxx # Terminate instance
# Instance filtering
aws ec2 describe-instances --filters "Name=instance-state-name,Values=running"
aws ec2 describe-instances --filters "Name=tag:Name,Values=my-server"
# Security groups
aws ec2 describe-security-groups # List security groups
aws ec2 describe-security-groups --group-ids sg-xxx
# Key pairs
aws ec2 describe-key-pairs # List key pairs
aws ec2 create-key-pair --key-name my-key # Create key pair
# AMIs
aws ec2 describe-images --owners self # List your AMIs
aws ec2 describe-images --image-ids ami-xxx # Describe specific AMIIAM (Identity and Access Management)
# Users
aws iam list-users # List all users
aws iam get-user # Get current user info
aws iam get-user --user-name username # Get specific user info
aws iam create-user --user-name newuser # Create user
# Groups
aws iam list-groups # List all groups
aws iam get-group --group-name groupname
# Roles
aws iam list-roles # List all roles
aws iam get-role --role-name rolename # Get specific role
# Policies
aws iam list-policies # List all policies
aws iam list-attached-user-policies --user-name username
aws iam list-attached-role-policies --role-name rolenameLambda
# Function management
aws lambda list-functions # List all functions
aws lambda get-function --function-name my-function
aws lambda invoke --function-name my-function output.json
aws lambda update-function-code --function-name my-function --zip-file fileb://function.zip
# Logs
aws logs describe-log-groups # List log groups
aws logs tail /aws/lambda/my-function # Tail function logsRDS (Relational Database Service)
# Database instances
aws rds describe-db-instances # List DB instances
aws rds describe-db-instances --db-instance-identifier mydb
aws rds start-db-instance --db-instance-identifier mydb
aws rds stop-db-instance --db-instance-identifier mydb
aws rds reboot-db-instance --db-instance-identifier mydbUseful Query Examples
# Get instance IDs and states
aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId,State.Name]' --output table
# Get S3 bucket sizes
aws s3 ls --summarize --human-readable --recursive s3://bucket-name
# List running instances with names
aws ec2 describe-instances --filters "Name=instance-state-name,Values=running" \
--query 'Reservations[*].Instances[*].[InstanceId,Tags[?Key==`Name`].Value|[0]]' --output table
# Get latest AMI ID for Amazon Linux
aws ec2 describe-images --owners amazon \
--filters "Name=name,Values=amzn2-ami-hvm-*" "Name=virtualization-type,Values=hvm" \
--query 'Images | sort_by(@, &CreationDate) | [-1].ImageId' --output textCommon Options
--profile profile-name # Use specific AWS profile
--region us-west-2 # Override default region
--output table|json|text|yaml # Set output format
--query 'expression' # Filter output with JMESPath
--dry-run # Preview operation without executing
--no-cli-pager # Disable paging for long outputHelpful Tips
- Use
aws <service> helpfor service documentation - Use
aws <service> <command> helpfor command-specific help - Add
--dry-runto preview destructive operations - Use
--queryto filter and format output - Set
AWS_PAGER=""to disable paging globally - Use
aws configure listto debug configuration issues
Error Troubleshooting
```bash # Common debugging commands aws sts get-caller-identity # Verify credentials aws configure list # Check configuration aws ec2 describe-regions # Test API connectivity