obautista
obautista

Reputation: 3773

Get Public IP Address on current EC2 Instance

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.

Upvotes: 130

Views: 139614

Answers (7)

John Rotenstein
John Rotenstein

Reputation: 270184

UPDATE: This answer assumes the instance is configured with Instance Metadata Service v1 (IMDSv1). For v2, see the comments below.


The AWS Command-Line Interface (CLI) can be used to return information on any/all Amazon EC2 instances, eg:

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

54.232.200.77

If you are seeking information about the EC2 instance from which you are executing the command, then the current IP address can be obtained via the instance metadata service:

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

ami-id
ami-launch-index
ami-manifest-path
block-device-mapping/
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/

So, the private IP address is available via:

$ curl http://169.254.169.254/latest/meta-data/local-ipv4

172.31.10.221

The public IP address is available via:

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

54.232.200.77

Upvotes: 186

shimii
shimii

Reputation: 149

For the New Users who are confused why the AWS ec2 Metadata API call giving a 401 answer please refer this:

The Metadata Service Version 2 is session based and it expects a session token to create access to the Meta Data API.

A token can be created using the below command:

TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"

and once the token is generated, use it to call the top-level items from Metadata API service as below:

curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/

For more details please refer the official documentation here

Upvotes: 6

Parishilan Rayamajhi
Parishilan Rayamajhi

Reputation: 3057

From inside instnace type following command :

curl -s v4.ident.me

Upvotes: 2

Damon Jones
Damon Jones

Reputation: 41

Some of the ec2 linux images (e.g. debian 10) include a pre-installed ec2metadata cli tool which can retrieve the current vm's public ip among other metadata.

ec2metadata --public-ipv4

Upvotes: 4

markroxor
markroxor

Reputation: 6506

If you are inside the instance -

$ curl icanhazip.com
162.202.17.123

here is one more way

$ curl -s ifconfig.me
162.202.17.123

these methods are not limited to just AWS.

Upvotes: 15

Raghvendra Soni
Raghvendra Soni

Reputation: 11

Get attached InstanceID with PublicIP.

aws ec2 describe-network-interfaces --query NetworkInterfaces[*].[Attachment.[InstanceId],Association.[PublicIp]] --output=json

Upvotes: 1

Duke Dougal
Duke Dougal

Reputation: 26406

curl http://checkip.amazonaws.com

this returns the public ip address.

Upvotes: 151

Related Questions