Reputation: 2805
I'm prefacing this with a call for any non-AWS-written guides for their CLI stuff... their examples are few and far between and tell me nothing about the required syntax and further, fleshed-out reading would be welcome.
I'm trying to replicate a few tasks we do in the GUI with a script to save time. Currently I'm trying to find out the state of an instance by feeding it the name of the instance (aka, the Name tag). The issue is that the output I'm getting is only the state, with no identifying information. Sometimes users will put in a wildcard and get multiple instances back, and I would like to display the name of each to differentiate.
My successful query for the state of an instance looks like so;
aws ec2 describe-instances --query "Reservations[].Instances[].State[]" --filter Name=tag:Name,Values="${userinput}" --output text
With an output of
16 running
16 running
16 running
16 running
16 running
16 running
While it is correct that all of these matched my input because of the wildcard, eg test*
, I need to know what each one is called. Not the instance id, the name, ie test01, test02, etc.
I would have expected it to be
aws ec2 describe-instances --query "Reservations[].Instances[].State[].Tags[?Key=='Name'].Value" --filter Name=tag:Name,Values="${state}" --output text
but that outputs an error, or
aws ec2 describe-instances --query "Reservations[].Instances[].State[].[Tags[?Key=='Name'].Value]" --filter Name=tag:Name,Values="${state}" --output text
but that gives me None
How can I add the name column to the output?
Upvotes: 0
Views: 1640
Reputation: 200617
The text output format is kind of ugly because it prints multiple lines per instance, but here's a working version:
aws ec2 describe-instances --query "Reservations[].Instances[].[State.Name, Tags[?Key=='Name'].Value[]]" --filter Name=tag:Name,Values="${userinput}" --output text
I couldn't figure out how to get each instance on one line using just the AWS CLI tool, but here's a version that prints one line per instance by piping to sed
:
aws ec2 describe-instances --query "Reservations[].Instances[].[State.Name, Tags[?Key=='Name'].Value[]]" --filter Name=tag:Name,Values="${userinput}" --output text | sed 'N;s/\n/ /'
Upvotes: 1