Ever found yourself in the need to quickly print details or execute commands on multiple of the same type of AWS resource? Like, get the size of all your s3 buckets, all list/update all tags on your EC2 machines? AWS CLI is more than enough!
The following are a set of useful AWS CLI scripts that make use of either command querying or parse the json output using JQ to for loop a list of resources and execute modify commands or get additional details on them.
All these assume you have a local install of both AWS CLI and JQ, and you have CLI configured with your account and region.
Code Completion for AWS CLI on your bash_profile
First, a useful script to allow aws cli code completion on your bash profile. Useful for when you don’t remember all the commands (Which are a lot)
List all EMR Clusters along with the associated EC2 Instance Ids
This one will list all EMR Clusters along with the associated EC2 Instance Ids.
It will print the result in the format:
# "cluster_id | [$ec2_instance-id]... "
List all AMIs along with any associated instances
At work, we recently had the need to list all our unused AMI’s, making sure they are not being used by any instance or running resource. This is how you can get such a list
Add Tags to a Kinesis Stream
Adding tags is a useful feature for organizing your resources. At my work, we use it for cost allocation across teams. This is a simple way to do so. Notice you can customize this one to work for any kind of resource that allow tagging.
Get S3 Buckets Tags
Tags, or any details/Information, really. Just change or add the appropriate field on the query, and have it printed.
If you are curious, there’s a lot more Gists on my Github profile.