14

Answers I found so far (e.g. Find out public ip address of the EC2 server) suggest using wget or curl to reach the server.

They are not useful for me because my ec2 instances are not reachable from the internet directly.

I have tried

aws ec2 --profile prod describe-instances --filters Name=instance-id,Values=i-00914683ababcba7eb1

But there are no IPs in the returned JSON result.

Which aws CLI command I can use to retrieve this info?

chicks
  • 3,639
  • 10
  • 26
  • 36
Anthony Kong
  • 2,976
  • 10
  • 53
  • 91

2 Answers2

23

Generally you can do it with --query filter.

If you need the private IP address only:

aws --region YOUR-AWS-REGION \
ec2 describe-instances \
--filters \
"Name=instance-state-name,Values=running" \
"Name=instance-id,Values=i-00914683ababcba7eb1" \
--query 'Reservations[*].Instances[*].[PrivateIpAddress]' \
--output text

If you need the public ip address only:

aws --region YOUR-AWS-REGION \
ec2 describe-instances \
--filters \
"Name=instance-state-name,Values=running" \
"Name=instance-id,Values=i-00914683ababcba7eb1" \
--query 'Reservations[*].Instances[*].[PublicIpAddress]' \
--output text

Or you can have both:

aws --region YOUR-AWS-REGION \
ec2 describe-instances \
--filters \
"Name=instance-state-name,Values=running" \
"Name=instance-id,Values=i-00914683ababcba7eb1" \
--query 'Reservations[*].Instances[*].[PrivateIpAddress, PublicIpAddress]' \
--output text

Of course you can have the output in json format too. Just change --output text to --output json

More information about --query filters.

Peycho Dimitrov
  • 988
  • 7
  • 9
0

It turns out I can do this

aws ec2 --profile prod describe-instances \
--filters Name=instance-id,Values=i-0f47d09e1d9659981 \
| jq ".Reservations[0].Instances[0].NetworkInterfaces[0].PrivateIpAddress"

and it will return

10.0.9.7

My command in the question failed because, coincidentally, that instance was being shut down. So there was no NetworkInterfaces section in the result.

chicks
  • 3,639
  • 10
  • 26
  • 36
Anthony Kong
  • 2,976
  • 10
  • 53
  • 91