Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

Amazon recently added the wonderful feature of tagging EC2 instances with key-value pairs to make the management of large numbers of VMs a bit easier.

Is there some way to query these tags in the same way as some of the other user-set data? For example:

$ curl http://169.254.169.254/latest/meta-data/placement/availability-zone

us-east-1d

Is there some similar way to query the tags?

1 Answer

0 votes
by (44.4k points)

To retrieve your instance id use a combination of AWS metadata tool and the new Tag API to retrieve the tags for the instance.

After you have installed ec2-metadata and ec2-describe-tags, use this example shell command to retrieve your current instances name. In this example, “Name=demo” is the tag you have to use.

Assumes EC2_PRIVATE_KEY and EC2_CERT environment variables are set.

ec2-describe-tags \

   --filter "resource-type=instance" \

   --filter "resource-id=$(ec2-metadata -i | cut -d ' ' -f2)" \

   --filter "key=Name" | cut -f5

This returns demo.

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
+1 vote
1 answer
asked Sep 28, 2019 in AWS by chandra (29.3k points)

Browse Categories

...