rsram312
rsram312

Reputation: 141

Describe EC2 instances Filter instance name using AWS cli

I want to use CLI commands to filter those instances which have particular tags and store it in a file on my local everytime I run the Jenkins job.

aws ec2 describe-instances --filters "Name=tag:Hello,Values=exclude"  --output table --query Reservations[*].Instances[*].{Id.InstanceId, State:State.name,Contact:tag:OwnerContact, InstanceProfileName:InstanceProfileName} > Book.txt 

I get the output in text file successfully, however for two of my parameters InstanceProfileName and OwnerContact , I get the output as None.

I tried a variety of combinations, still not able to output the instance name.

Upvotes: 2

Views: 2291

Answers (1)

John Rotenstein
John Rotenstein

Reputation: 270164

Here's an example that can output various parameters, including tag values:

aws ec2 describe-instances --query "Reservations[*].Instances[*].{Id:InstanceId,State:State.Name,Profile:IamInstanceProfile.Arn,Name:Tags[?Key=='Name'].Value}"

To get the syntax right, just try one element at a time. Start with:

aws ec2 describe-instances --query "Reservations[*].Instances[*]"

then add parameters, eg:

aws ec2 describe-instances --query "Reservations[*].Instances[*].Tags"

Then try each value that you want. Finally, string it all together in a big command.

To understand the --query options, take a look at: JMESPath Tutorial

Upvotes: 3

Related Questions