23

The closest I have get is using the following commands.

This command manage to lists all name of instances.

aws ec2 describe-instances --filters Name=vpc-id,Values=vpc-e2f17e8b --query 'Reservations[].Instances[].Tags[?Key==`Name`].Value[]'

This command manage to list all private ip address, instance id and ALL tags which I don't need. I just need the name.

aws ec2 describe-instances --filters Name=vpc-id,Values=vpc-e2f17e8b | jq '.Reservations[].Instances[] | {PrivateIpAddress, InstanceId, Tags}'

I'm not sure why I can't execute command like this way:

aws ec2 describe-instances | jq '.["Reservations"]|.[]|.Instances|.[]|.PrivateIpAddress + " " + .InstanceId + " " + .Tags[?Key==`Name`].Value[]'

This command works but its showing all the Tags Key names.

aws ec2 describe-instances | jq '.["Reservations"]|.[]|.Instances|.[]|.PrivateIpAddress + " " + .InstanceId + " " + .Tags'
2
  • 1
    Are you just copying these commands from somewhere without trying to understand what they do? Feb 28, 2014 at 15:10
  • Not really, I did try to understand how to use jq and how to get the basic json output I want. However, I couldn't find any examples for what I am trying to achieve. Using "Tags[?Key==Name].Value[]" as a filter for Key Name Value output is only possible after aws-cli v1.3.0. And I'm using a combination of --filter and jq to get the output I want. The closest command is aws ec2 describe-instances --filters Name=vpc-id,Values=vpc-e2f17e8b | jq '.Reservations[].Instances[] | {PrivateIpAddress, InstanceId, Tags}' I just need to know how to reference the Tag Key=Name using jq.
    – Imagineer
    Mar 2, 2014 at 22:26

6 Answers 6

28

You need to escape the backslashes in order to format the answer correctly.

aws ec2 describe-instances --query 'Reservations[].Instances[].[PrivateIpAddress,Tags[?Key==`Name`].Value[]]' --output text | sed '$!N;s/\n/ /'

So this is the actual command you want:

$ aws ec2 describe-instances --filters Name=vpc-id,Values=vpc-ac973bc9 --query 'Reservations[].Instances[].[PrivateIpAddress,InstanceId,Tags[?Key==`Name`].Value[]]' --output text | sed '$!N;s/\n/ /'
10.101.255.10   i-91efd39b Server1
10.101.255.9    i-f1e8d4fb Server2

And you don't need .Value[]. You can just use .Value, and that will give the same output.

This is awesome, btw. I will be implementing this myself!

CORRECTION: The above won't work if the value of .Value is "None". This works better:

$ aws ec2 describe-instances --filters Name=vpc-id,Values=vpc-ac973bc9 --query 'Reservations[].Instances[].[PrivateIpAddress,InstanceId,Tags[?Key==`Name`].Value[]]' --output text | sed 's/None$/None\n/' | sed '$!N;s/\n/ /'
10.101.255.10   i-91efd39b Server1
10.101.255.9    i-f1e8d4fb Server2
10.101.255.8    i-f6c2450a      None
10.101.255.7    i-34a6afce Server3
2
  • Can you please explain your sed command? I'm getting instance IDs/names off by one which might be because I'm not understanding the indirect shell expansion in the sed.
    – jorfus
    May 13, 2016 at 22:26
  • 3
    If an instance is not Named (doesn't have Tag:key=Name set), then the next instance gets printed on the same line. The first sed command prints the string "None\n" to mitigate that problem. The second sed command strips the linefeed off the instance-id, so that the Tag:key=Name string gets printed on the same line. May 17, 2016 at 0:28
12

Try this

aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId,Tags[?Key==`Name`].Value|[0],State.Name,PrivateIpAddress,PublicIpAddress]' --output text | column -t
3
  • 3
    You should include explanation for your code. Describing how and why this code solves the problem is more useful as it helps the OP and other readers solve this and similar issues themselves. Aug 31, 2016 at 13:13
  • This works, but indeed, why does it work? | is some kind of filter?
    – aairey
    Oct 22, 2018 at 18:30
  • This should be the accepted answer
    – grepit
    Jun 9, 2023 at 20:14
8

The above answers are OK, but my favorite of the same is;

aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId,State.Name,InstanceType,PrivateIpAddress,PublicIpAddress,Tags[?Key==`Name`].Value[]]' --output json | tr -d '\n[] "' | perl -pe 's/i-/\ni-/g' | tr ',' '\t' | sed -e 's/null/None/g' | grep '^i-' | column -t

in fact, one can place it in a BASH function list list;

awsls () { aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId,State.Name,InstanceType,PrivateIpAddress,PublicIpAddress,Tags[?Key==`Name`].Value[]]' --output json | tr -d '\n[] "' | perl -pe 's/i-/\ni-/g' | tr ',' '\t' | sed -e 's/null/None/g' | grep '^i-' | column -t }

then simply call from the prompt as 'awsls'

1
  • How do you add multiple functions in one file and just call the function you want?
    – Stryker
    Aug 14, 2018 at 19:49
2

Something like this?

aws ec2 describe-instances --query 'Reservations[].Instances[].[PrivateIpAddress,Tags[?Key==`Name`].Value[]]' --output text | sed '$!N;s/\n/ /'
1

I added a filter for instance state "running". Posting it here in case that's helpful for anyone.

My use case is slightly different, I'm generating Ansible host files so I just want private IP # name on all running hosts.

aws ec2 describe-instances --profile=$PROFILE --filters Name=vpc-id,Values=$VPCID Name=instance-state-name,Values=running --query 'Reservations[].Instances[].[PrivateIpAddress,Tags[?Key==`Name`].Value[]]' --output text | sed 's/None$/None\n/' | sed '$!N;s/\n/ /' | awk '{print $1 " #" $2 }'
0

Adding this for folks that will find this post when searching for how to get your instance info. You can add VPC in the select statement to receive that as well.

In powershell you can use:

(Get-EC2Instance -ProfileName Profile).Instances | select InstanceId,PrivateIPAddress,PublicIpAddress @{Name="Servername";Expression={$_.tags | where key -eq "Name" | select Value -expand Value}} | Format-Table.

With the AWS CLI you can use:

aws ec2 describe-instances --region=us-east-1 --query 'Reservations[].Instances[].[InstanceId,Tags[?Key==Name].Value|[0],PrivateIpAddress,PublicIpAddress]' --output text --profile ProfileName
1
  • I edited your answer to format the commands as commands/code. Would you please double-check that the period at the end of your first command needs to be there? If not, please edit it out, thanks
    – JimLohse
    May 3, 2019 at 6:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .