DEV Community

John Gakhokidze
John Gakhokidze

Posted on

1

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

#Region to scan
echo Please enter region to scan
read region

#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 steep, 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

#Cleanup csv
sed -i 's/:"/;"/g' $region-tags.csv

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

Sentry workshop image

Sick of your mobile apps crashing?

Let Simon Grimm show you how to fix them without the guesswork. Join the workshop and get to debugging.

Save your spot →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay