aws-cli icon indicating copy to clipboard operation
aws-cli copied to clipboard

Retrieving the IP address for an EC2 instance given its <instance ID>

Open amelio-vazquez-reina opened this issue 12 years ago • 8 comments

Does the CLI have any commands that can be used to retrieve the private IP of an EC2 instance given the instance ID ?

What if I provide a tag instead?

amelio-vazquez-reina avatar Oct 07 '14 20:10 amelio-vazquez-reina

curl --silent http://169.254.169.254/latest/meta-data/public-ipv4

Please call any time:

Cell: +1-916-202-1600 Skype: EdwardMGoldberg LinkedIn: http://lnkd.in/2yWrBZ AWS: http://www.aws-partner-directory.com/PartnerDirectory/PartnerDetail?Name=myCloudWatcher

Edward M. Goldberg http://myCloudWatcher.com/ e.m.g.

On Tue, Oct 7, 2014 at 1:11 PM, ribonoous [email protected] wrote:

Does the CLI have any commands that can be used to retrieve the private IP of an AWS machine ?

— Reply to this email directly or view it on GitHub https://github.com/aws/aws-cli/issues/934.

EdwardMGoldberg avatar Oct 07 '14 20:10 EdwardMGoldberg

aws ec2 describe-instances --instance-id i-3f3f3f3f

if you have jq installed…

instance_id=aws ec2 describe-instances --instance-id i-3f3f3f3f| jq —raw-output .Reservations[].Instances[].PrivateIpAddress

On Oct 7, 2014, at 4:11 PM, ribonoous [email protected] wrote:

Does the CLI have any commands that can be used to retrieve the private IP of an AWS machine ?

— Reply to this email directly or view it on GitHub.

digitaljedi2 avatar Oct 07 '14 20:10 digitaljedi2

Thanks @digitaljedi2 Your JQ solution worked

amelio-vazquez-reina avatar Oct 07 '14 21:10 amelio-vazquez-reina

@ribonoous Also, you can use the --query option to avoid having an additional dependency. aws ec2 describe-instances --instance-id i-12345 --query 'Reservations[].Instances[].PrivateIpAddress'.

jamesls avatar Oct 07 '14 22:10 jamesls

Adding on top @jamesls answer:

aws ec2 describe-instances --instance-id i-0329b8ad --query 'Reservations[].Instances[].PrivateIpAddress' | grep -vE '\[|\]' | awk -F'"' '{ print $2 }'

This way you end up with a clean output.

cig0 avatar Sep 17 '18 20:09 cig0

This way you end up with a clean output.

There's also an extra param you can pass to describe-instances to accomplish the same thing:

aws ec2 describe-instances \
  --instance-id i-0329b8ad \
  --query 'Reservations[].Instances[].PrivateIpAddress' \
  --output text

fearphage avatar Sep 20 '18 18:09 fearphage

thy are all wrong... it seems the --instance.id is ignored and ALL instances are shown always

Zibri avatar Nov 30 '18 16:11 Zibri

thy are all wrong... it seems the --instance.id is ignored and ALL instances are shown always

It's instance-id, not instance.id

fjaguero avatar Dec 31 '20 17:12 fjaguero