Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List public IP addresses of EC2 instances

I want to list the public IP addresses of my EC2 instances using Bash, separated by a delimiter (space or a new-line).

I tried to pipe the output to jq with aws ec2 describe-instances | jq, but can't seem to isolate just the IP addresses.

Can this be done by aws alone, specifying arguments to jq, or something else entirely?

like image 551
Bas Peeters Avatar asked Jul 24 '14 16:07

Bas Peeters


People also ask

How many public IP addresses does AWS have?

By default, all AWS accounts are limited to five (5) Elastic IP addresses per Region, because public (IPv4) internet addresses are a scarce public resource.

Why EC2 instance doesn't have public IP?

The most common reason for no public IP address for your EC2 instance is that you are launching your EC2 instance using a private subnet. A private subnet means any EC2 instances located in that subnet aren't directly addressable from the public web.


2 Answers

Directly from the aws cli:

aws ec2 describe-instances \   --query "Reservations[*].Instances[*].PublicIpAddress" \   --output=text 
like image 114
Julio Faerman Avatar answered Oct 03 '22 00:10

Julio Faerman


  • Filter on running instances (you can drop that part if you don't need it)
  • Query for each PublicIPaddress and the Name Tag, handling when Name isn't set
aws ec2 describe-instances \   --filter "Name=instance-state-name,Values=running" \   --query "Reservations[*].Instances[*].[PublicIpAddress, Tags[?Key=='Name'].Value|[0]]" \   --output text 
like image 34
Brad Giaccio Avatar answered Oct 02 '22 23:10

Brad Giaccio