Installing AWS CLI on Ubuntu 24.04
The AWS Command Line Interface (CLI) is a unified tool to manage your AWS services. This comprehensive guide covers installation, configuration, and practical usage.
Prerequisites
- Ubuntu 24.04 system
- User with sudo privileges
- Internet connection
- AWS account (for configuration)
Step 1: Update System Packages
sudo apt update
sudo apt upgrade -y
Step 2: Install Required Dependencies
sudo apt install -y unzip curl
Why these packages?
- unzip: Required to extract AWS CLI archive
- curl: Needed to download the installation file
Step 3: Download AWS CLI Package
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
Verification Tip: You can verify the download using SHA256 checksums:
curl -o awscliv2.sig https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip.sig
gpg --verify awscliv2.sig awscliv2.zip
Step 4: Extract the AWS CLI Package
unzip awscliv2.zip
Step 5: Install AWS CLI
sudo ./aws/install
Note: If you're updating an existing installation, use:
sudo ./aws/install --update
Step 6: Verify the Installation
aws --version
Step 7: Configure AWS CLI
aws configure
You'll need 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)
Common AWS CLI Commands
Essential System Commands
# Get AWS CLI version
aws --version
# Get caller identity (who am I?)
aws sts get-caller-identity
# List available regions
aws ec2 describe-regions
Basic S3 Commands
# List all buckets
aws s3 ls
# Upload a file
aws s3 cp myfile.txt s3://my-bucket/
# Download a file
aws s3 cp s3://my-bucket/myfile.txt ./
# List contents of a bucket
aws s3 ls s3://my-bucket/
Basic EC2 Commands
# List all running instances
aws ec2 describe-instances --filters "Name=instance-state-name,Values=running"
# Start an instance
aws ec2 start-instances --instance-ids i-1234567890abcdef0
# Stop an instance
aws ec2 stop-instances --instance-ids i-1234567890abcdef0
# List all security groups
aws ec2 describe-security-groups
Basic IAM Commands
# List users
aws iam list-users
# List groups
aws iam list-groups
# List access keys for a user
aws iam list-access-keys --user-name myuser
Advanced Configuration
Configuration Profiles
Create multiple profiles for different AWS accounts:
aws configure --profile production
aws configure --profile development
Use specific profile:
aws s3 ls --profile production
Environment Variables
Add to your ~/.bashrc:
export AWS_PROFILE=production
export AWS_DEFAULT_REGION=us-west-2
Advanced Usage Examples
S3 Operations
# Sync local directory with S3
aws s3 sync . s3://my-bucket/
# Copy with specific permissions
aws s3 cp myfile.txt s3://my-bucket/ --acl public-read
# List objects with specific prefix
aws s3 ls s3://my-bucket/folder/
EC2 Advanced Commands
# Get detailed instance information
aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId,State.Name,Tags[?Key==`Name`].Value[]|[0]]' --output table
# Create AMI from instance
aws ec2 create-image --instance-id i-1234567890abcdef0 --name "My-Server-Backup-$(date +%Y%m%d)"
# List all volumes
aws ec2 describe-volumes --query 'Volumes[*].{ID:VolumeId,Size:Size,State:State,Type:VolumeType}'
Using AWS CLI with jq
# Install jq
sudo apt install jq
# Example: Format instance list
aws ec2 describe-instances | jq '.Reservations[].Instances[].InstanceId'
# Example: Get specific tag values
aws ec2 describe-instances | jq '.Reservations[].Instances[] | {id: .InstanceId, name: (.Tags[]|select(.Key=="Name").Value)}'
Troubleshooting
Common Issues
Credentials Issues
# Check credentials file
cat ~/.aws/credentials
# Check config file
cat ~/.aws/config
Permission Problems
# Fix permissions
chmod 600 ~/.aws/credentials
chmod 600 ~/.aws/config
Command Completion
# Enable command completion
complete -C '/usr/local/bin/aws_completer' aws
Security Best Practices
- Use IAM roles instead of access keys when possible
- Regularly rotate access keys
- Never commit credentials to version control
- Use separate profiles for different environments
- Enable MFA for CLI operations when possible
- Monitor CloudTrail for CLI activities
- Use AWS CLI profiles to prevent accidents
Cleanup
Remove installation files:
rm -rf aws awscliv2.zip
Additional Tips
- Use the --dry-run flag to test commands without making changes
- Utilize the --query parameter to filter results
- Save common commands as shell aliases
- Keep the AWS CLI updated for new features
- Use the help command for detailed information: aws help