AWS CLI Cheat Sheet
AWS CLI Cheat Sheet
Table of Contents
Volumes
Describing volumes
Describing volumes using a different aws user profile
Listing Available Volumes IDs
Deleting a Volume
Deleting Unused Volumes.. Think Before You Type :-)
Creating a Snapshot
Creating an Image (AMI)
Creating AMI Without Rebooting the Machine
AMIs
Listing AMI(s)
Describing AMI(s)
Listing Amazon AMIs
Using Filters
Lambda
List Functions
Describe a Function
Invoke a Function
Update a Function Code
Publish a Version
List Layers
List Aliases of a Function
Describe an Alias
Create an Alias
Delete an Alias
List Function Tags
Delete a Function
Using AWS Lambda with Scheduled Events
IAM
List Users
List Policies
List Groups
Get Users in a Group
Describing a Policy
List Access Keys
List Keys
List the Access Key IDs for an IAM User
List the SSH Public Keys for a User
S3 API
Listing Buckets
Listing Only Bucket Names
Getting a Bucket Region
Listing the Content of a Bucket
Syncing a Local Folder with a Bucket
Copying Files
Copying Folders
Removing a File from a Bucket
Deleting a Bucket
Emptying a Bucket
VPC
Creating A VPC
Allowing DNS hostnames
Subnets
Creating A Subnet
Auto Assigning Public IPs To Instances In A Public Subnet
Internet Gateway
Creating An IGW
Attaching An IGW to A VPC
NAT
Setting Up A NAT Gateway
Route Tables
List Databases
List Public Databases
List Non Protected Databases (DeletionProtection)
Describe the Automated Backups for a DB Instance
Create a DB Cluster
Create a DB Instance
Create a DB Security Group
Create a Read Replica
Create a Custom DB Cluster Endpoint
Apply Tag to a DB
Create a Cluster Snapshot
Create a CloudWatch Alarm for a DB Instance
Connect Deeper
Volumes
Describing volumes
aws ec2 describe-volumes
This is the generic form. Use --profile <your_profile_name> , if you have multiple AWS profiles or
accounts.
With "profile":
Deleting a Volume
aws ec2 delete-volume --region <region> --volume-id <volume_id>
With "profile":
Creating a Snapshot
You are free to change the AMI name image-$(date +'%Y-%m-%d_%H-%M-%S') to a name of your
choice.
AMIs
Listing AMI(s)
Describing AMI(s)
e.g:
Using Filters
e.g: Describing Windows AMIs that are backed by Amazon EBS.
Lambda
List Functions
Describe a Function
aws lambda get-function --function-name my-function
Invoke a Function
aws lambda invoke --function-name my-function --payload '{ "name": "Bob" }'
response.json
Publish a Version
List Layers
Let's take this example in which we want to list information of layers that are compatible with
Python 3.7 runtime.
nodejs
nodejs4.3
nodejs6.10
nodejs8.10
nodejs10.x
nodejs12.x
java8
java8.al2
java11
python2.7
python3.6
python3.7
python3.8
dotnetcore1.0
dotnetcore2.0
dotnetcore2.1
dotnetcore3.1
nodejs4.3-edge
go1.x
ruby2.5
ruby2.7
provided
provided.al2
Describe an Alias
Create an Alias
Delete an Alias
Delete a Function
##
IAM
List Users
List Policies
List Groups
Describing a Policy
List Keys
S3 API
Listing Buckets
Or
aws s3 ls
e.g
e.g
aws s3api get-bucket-location --bucket practicalaws.com
e.g
aws s3 ls s3://practicalaws.com
e.g
Copying Files
Or:
To copy all files from a filder, look at "Copying Folders". Or use the following example, where I
copy the content of the folder "images (contains images) in the remote folder "images".
cd images
aws s3 cp . s3://saltstackfordevops.com/images --recursive --region us-east-2
Copying Folders
To exclude files:
aws s3 cp <folder_name>/ s3://<bucket_name>/ --recursive --exclude "
<file_name_or_a_wildcard>"
e.g: To only include a certain type of files (PNG) and exclude others (JPG)
aws s3 rm s3://<bucket_name>/<object_name>
e.g
aws s3 rm s3://practicalaws.com/temp.txt
Deleting a Bucket
e.g
Emptying a Bucket
e.g
e.g:
aws s3 ls --recursive s3://<bucket-name> | cut -d' ' -f5- | awk '{print $NF}' |
while read line; do
echo "$line"
aws s3api put-object-acl --acl private --bucket <bucket-name> --key "$line"
done
VPC
Creating A VPC
e.g
Subnets
Creating A Subnet
aws ec2 create-subnet --vpc-id <vpc_id> --cidr-block <cidr_block> --
availability-zone <availability_zone> --region <region>
Internet Gateway
Creating An IGW
NAT
then use the AllocationId to create the NAT Gateway for the public zone in
Route Tables
CloudFront
Listing Distributions
In some cases, you need to setup this first:
Then:
Invalidating Files From a Distribution
To invalidate index and error HTML files from the distribution with the ID Z2W2LX9VBMAPRX:
e.g.:
RDS
List Databases
aws rds describe-db-instances
or:
Create a DB Cluster
Create a DB Instance
aws rds create-db-instance \
--db-instance-identifier mysql-instance \
--db-instance-class db.t3.micro \
--engine mysql \
--master-username admin \
--master-user-password xxxxx \
--allocated-storage 40
Apply Tag to a DB
Create a CloudWatch Alarm for a DB Instance
e.g.: When average CPU for latest 15 minutes is above 90%
This will monitor the DB instance during a period of 300 seconds (5 minutes) during 3 evaluation
periods: 5*3 = 15 minutes.
If in the three periods, the average is equal or more than 90%, then the alarm will trigger the SNS
resource.
You should subscribe to the SNS resource you create by email or SMS.
Connect Deeper
This work was first published in Practical AWS course.
Join our online community FAUN and subscribe to our podcast The DevOps Fauncast.
Visit our publication The Chief I/O, subscribe to the newsletter and get cloud native insights from
our contributors.