Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (12.9k points)

Using Amazon CLI, is there a way to get the public ip address of the current EC2? I'm just looking for the single string value, so not the json response describe-addresses returns.

2 Answers

0 votes
by (18.2k points)

You can use ec2 describe instances command in AWS CLI to display information about your running instances. 

Run the following command in your AWS CLI to display the public IP address of your instance.

$ aws ec2 describe-instances --instance-ids i-0c9c9b44b --query 'Reservations[*].Instances[*].PublicIpAddress' --output text

You can also display Public IP address of the instance where you have currently SSH'ed, using the following command in your putty terminal:

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

You can also display other metadata of your running instance where you have SSH'ed. 

Use the following command with the respective attribute(mentioned below) to display its respective value:

curl http://169.254.169.254/latest/meta-data/

ami-id

ami-launch-index

ami-manifest-path

block-device-mapping/

events/

hostname

iam/

instance-action

instance-id

instance-type

local-hostname

local-ipv4

mac

metrics/

network/

placement/

profile

public-hostname

public-ipv4

public-keys/

reservation-id

security-groups

services/

0 votes
by
edited by

To display your instance Public IP address, from within the instance :

try this 

curl -s ifconfig.co

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...