3

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 is no IPs info in the returned JSON result.

What aws cli command I can use to retrieve this info?

5

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.

| improve this answer | |
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, due to sheer coincidence, that instance was being shut down. So there was NetworkInterfaces section in the result.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.