1

Anyone know how to get a list of all public IPs and their instance names on Amazon EC2 using aws CLI?

This got me the list of public IPs, but not their associated instance names. aws ec2 describe-instances --query "Reservations[].Instances[].PublicIpAddress" --output text

Thanks in advance.

sho
  • 439
  • 2
  • 7
  • 13
  • aws ec2 describe-instances --query "Reservations[].Instances[].Tags[].[Key,Value]" gives the name of the instance and other tags, but how do I give just the Name tag, and also the public IP? – sho Sep 16 '14 at 01:04
  • possible duplicate of [How do I get a simple list of my public IP addresses](http://stackoverflow.com/questions/24938971/how-do-i-get-a-simple-list-of-my-public-ip-addresses) – Bas Peeters Nov 04 '14 at 10:59

3 Answers3

9

http://docs.aws.amazon.com/cli/latest/userguide/controlling-output.html

Update: The CLI now supports filters:

aws ec2 describe-instances --query "Reservations[].Instances[].[PublicIpAddress,InstanceId,Tags[?Key=='Name'].Value]" 
chris
  • 36,094
  • 53
  • 157
  • 237
  • That got me the instance ID and Public IP, but I need the Tag with the Name of the instance. – sho Sep 16 '14 at 00:38
5

Got it working using this:

aws ec2 describe-instances --output table   --query 'Reservations[].Instances[].[Tags[?Key==`Name`] | [0].Value, PublicIpAddress]'
Piyush Patil
  • 14,512
  • 6
  • 35
  • 54
sho
  • 439
  • 2
  • 7
  • 13
0

Adding this for folks that will commonly find this post when searching for how to get your instance info.

In powershell you can use the following:

(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 Python 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
mrk
  • 8,059
  • 3
  • 56
  • 78