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'
Imagineer
  • 815

6 Answers6

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
12

Try this

aws ec2 describe-instances --query 'Reservations[*].Instances[*].[InstanceId,Tags[?Key==`Name`].Value|[0],State.Name,PrivateIpAddress,PublicIpAddress]' --output text | column -t
alf-man
  • 121
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'

2

Something like this?

aws ec2 describe-instances --query 'Reservations[].Instances[].[PrivateIpAddress,Tags[?Key==`Name`].Value[]]' --output text | sed '$!N;s/\n/ /'
slm
  • 8,010
Alan
  • 21
  • 2
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 }'
jorfus
  • 855
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
JimLohse
  • 103