AWS CLI — Get tags for any object

Here is small goodie. Script to retrieve all tags from any resources in AWS.

I assume that you installed already AWS CLI — let us quick go through.

First you answer questions as
Profile to use
Region to scan
Resource type you want to get tags

echo Please enter profile to use
read profile

#Resource type
echo What resource type you want to scan for tags
echo Please enter one of the following
echo customer-gateway \| dedicated-host \| dhcp-options \| elastic-ip \| fleet \| fpga-image \| host-reservation \| image \| instance \| internet-gateway \| key-pair \| launch-template \| natgateway \| network-acl \| network-interface \| placement-group \| reserved-instances \| route-table \| security-group \| snapshot \| spot-instances-request \| subnet \| volume \| vpc \| vpc-endpoint \| vpc-endpoint-service \| vpc-peering-connection \| vpn-connection \| vpn-gateway
read resourceid

Next — script will get all resources in region and filter it against your requested resource.

Note — I choose to continue still querying AWS API in the next step, instead of working with local file, but instead on this step you can save results to file, and work with local json file instead — e.g. remove everything starting with --filters and leave redirection to file.

echo Gettign Resources RecourceType=$resourceid
aws ec2 describe-tags --filters "Name=resource-type,Values=$resourceid" | jq -r '.Tags[]| .ResourceId as $d|([$d]|@csv)'|sed 's/\"//g' >resources.txt

Next — script will read resources.txt and will request tags. Again, you will need to change next lines if you prefer to work from your local file

for n in $(cat resources.txt);do (aws ec2 describe-tags --filters "Name=resource-id,Values=$n" --profile $profile --region $region && echo -n $n: >>$region-tags.csv) |jq -r '.Tags[]|.Key as $k|([$k,.Value]|@csv)'|sed ':a;N;$!ba;s/\n/:/g' >>$region-tags.csv;done

Once done, the CSV file is available in your folder named $region-tag.csv

Originally published at https://dev.to on September 4, 2020.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

GO-vatar series: Lightweight thread

HTTP/2 for Modern Webapps

Create your own chatbot to handle your social media

Basic Mistakes of Writing Getters/Setters in Java

『猫和老鼠』~完整版本 (2021-HD) Tom and Jerry 完整版觀看電影在線小鴨 完整的電影 [4K]

20+ Free Udemy Courses to become a Web Developer in 2021

CS373 Spring 2022 Week 7 — David Tang

Aave V2 is Available on Public Testnet!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
John Gakhokidze

John Gakhokidze

More from Medium

How to use an API key to do a fetch request

Remote Browser Isolation — The Next Step in Endpoint Security?

Different Tools Used For API Testing

Package, Jars and deployment.