AWS CLI Hacks: The Only Cheat Sheet You’ll Ever Need
AWS CLI Hacks: The Only Cheat Sheet You’ll Ever Need
Introduction
The Amazon Web Services (AWS) Command Line Interface (CLI) is an incredibly powerful tool that enables developers and system administrators to manage AWS services from the terminal. While it has a steep learning curve, mastering the AWS CLI can significantly streamline workflows and automate various tasks in cloud management. This article presents essential hacks and handy commands to transform your AWS CLI experience, serving as the only cheat sheet you’ll ever need.
Getting Started
Before diving into the hacks, it’s essential to ensure the AWS CLI is installed and configured on your machine. Follow these steps:
-
Installation:
- For Windows, macOS, or Linux, use package managers like
piporbrewfor a hassle-free installation. - Example:
bash
pip install awscli –upgrade –user
- For Windows, macOS, or Linux, use package managers like
-
Configuration:
-
Run the configuration command:
bash
aws configure -
You will be prompted to enter your Access Key ID, Secret Access Key, region, and output format.
-
Essential Commands
1. Listing Resources
EC2 Instances
To get a list of all your EC2 instances:
bash
aws ec2 describe-instances –query “Reservations[].Instances[].[InstanceId,State.Name]” –output table
2. S3 Bucket Management
Listing Buckets
To list all your S3 buckets:
bash
aws s3 ls
Copying Files to S3
To copy a local file to your S3 bucket:
bash
aws s3 cp localfile.txt s3://your-bucket-name/
3. IAM User Management
Create a New User
To create a new IAM user:
bash
aws iam create-user –user-name newuser
Attach Policies to User
To attach a policy to the newly created user:
bash
aws iam attach-user-policy –user-name newuser –policy-arn arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess
Advanced Hacks
4. Command Autocompletion
Enhance your workflow by enabling command autocompletion. You can set this up by adding the following to your .bashrc or .bash_profile file:
bash
complete -C ‘aws_completer’ aws
5. JMESPath Query Language
Utilise JMESPath to filter and format AWS CLI output. For example, to display only the public IPs of all running EC2 instances:
bash
aws ec2 describe-instances –query “Reservations[*].Instances[?State.Name==’running’].PublicIpAddress” –output text
6. Saving Command Outputs to a File
You can save the output of any AWS CLI command directly to a file:
bash
aws s3 ls > s3_buckets.txt
7. Using Profiles for Multiple Credentials
If you manage multiple AWS accounts, using named profiles can be a lifesaver. Create profiles in your ~/.aws/config file:
[profile my-account]
aws_access_key_id = YOUR_ACCESS_KEY
aws_secret_access_key = YOUR_SECRET_KEY
region = us-west-1
Then, use the profile in your command:
bash
aws s3 ls –profile my-account
8. AWS CLI Pagination
For commands that return large sets of data, pagination is key. Use the --max-items parameter to limit the number of results:
bash
aws ec2 describe-instances –max-items 10
Conclusion
The AWS CLI is an invaluable tool for anyone working with AWS, allowing for a more efficient and streamlined management experience. By utilising the hacks and commands outlined above, you’ll be well on your way to mastering the CLI and enhancing your cloud management workflow. Whether you’re a beginner or a seasoned professional, this cheat sheet offers a handy reference to make your AWS journey smoother. Happy cloud computing!



Post Comment