How to Fetch the Tags for Ec2-Describe-Instances in a Shell Script

Query EC2 tags from within instance

You can use a combination of the AWS metadata tool (to retrieve your instance ID) and the new Tag API to retrieve the tags for the current instance.

AWS-cli ec2 describe instances

To describe all instances with Tag "NAME" Use:

aws ec2 describe-instances --filters "Name=tag-key,Values=Name"

or

This Gives InstanceId with Particular Tag "Name"

aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId, Tags[?Key==`Name`].Value[0]]'

or

This Gives InstanceId with Particular Tag "Name" and Value of Tag

aws ec2 describe-instances --query 'Reservations[].Instances[].[InstanceId, Tags[?Key==`Name`], Tags[?Key==`Name`].Value[]]'

To describe all instances with Tag "Purpose" and its value as "test" Use:

aws ec2 describe-instances --filters "Name=tag:Purpose,Values=test"

If you already know the Instance id:

aws ec2 describe-instances --instance-ids i-1234567890abcdef0

To find every instance which doesn't contain a tag named "Purpose":

aws ec2 describe-instances | jq '.Reservations[].Instances[] | select(contains({Tags: [{Key: "Purpose"} ]}) | not)'

To filter against the value of the tag, instead of the name of the tag:

aws ec2 describe-instances | jq '.Reservations[].Instances[] | select(contains({Tags: [{Key: "Name"}, {Value: "testbox1"}]}) | not)'

To find every instance which doesn't contain a tag:

aws ec2 describe-instances | jq '.Reservations[].Instances[] | select(contains({Tags: [{Key: ""}, {Value: ""}]}) | not)'

Filtering by tags not working when using aws ec2 describe-instances from command line (cli)

I tried to verify your command and it produces errors as you wrote it:

Error parsing parameter '--filters': Second instance of key "Name" encountered for input:
Name=tag:ShortPurpose,Values=Fleet,Name=instance-state-code,Values=80
^
This is often because there is a preceeding "," instead of a space.

However, I was able to successful use it on my sandbox instances as follows:

aws ec2 describe-instances \
--query "Reservations[*].Instances[*].InstanceId" \
--filters Name=tag:ShortPurpose,Values=Fleet Name=instance-state-code,Values=80

Retrieve the value of certain tag from within the instance

The difficulty here is that the Name of an instance is actually just a tag itself. Thus, you have to search for a tag associate with an instance by specifying the Name tag, then look at the other tags.

The describe-tags command can list the Name tags:

aws ec2 describe-tags --filters "Name=resource-type,Values=instance" "Name=key,Values=Name"

You would then need to find the specific instance with the matching name:

aws ec2 describe-tags --filters "Name=resource-type,Values=instance" "Name=key,Values=Name" "Name=value,Values=MY-NAME"

However, all this returns is the tag you have already specified, rather than the other tags for the instance.

You really need a 2-step process that first finds the Instance ID, then retrieves the tags for that instance:

aws ec2 describe-tags --filters "Name=resource-id,Values=`aws ec2 describe-tags --filters "Name=resource-type,Values=instance" "Name=key,Values=Name" "Name=value,Values=MY-NAME" --query Tags[].ResourceId --output text`" "Name=key,Values=MY-TAG-NAME" --query Tags[].Value --output text

List all EC2 Instances - AWSCLI

i solved this issue replacing the default profile on the second for.

for region in $(aws ec2 describe-regions --profile ${account} --region sa-east-1 --output text | cut -f3)

This script works, i hope this script can help's.



Related Topics



Leave a reply



Submit