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.
7 Answers
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
-
1I have added two public ip address and I have used curl 169.254.169.254/latest/meta-data/public-ipv4 command but it showing the IP address that I have added recently. Is there a way to list all public ip address in an instance? Commented Nov 27, 2016 at 13:55
-
@Techiescorner you can run something like
curl http://169.254.169.254/latest/meta-data/network/interfaces/macs/09:34:54:0a:9b:4a/public-ipv4s
to get the public IP for each interface using their mac address (you can get that fromifconfig-a
)– IcemanCommented Feb 26, 2019 at 8:00 -
Duke's suggestion below to hit checkip.amazonaws.com is simpler and requires no access to instance profile or IAM credentials, which may be restrcted in some environments.(running on CI provider for instance)– EddieCommented Aug 14, 2019 at 15:59
-
On Windows I has to use double quotes for it to work on command prompt Commented Jan 5, 2021 at 22:48
-
1This doesn't work, but
curl http://checkip.amazonaws.com
works. Commented Jan 14 at 23:41
curl http://checkip.amazonaws.com
this returns the public ip address.
-
Was there an official documentation from AWS about this web service? Or is it some of those hacky ones? By the way https also works for that web service. Great answer there! Commented May 25, 2019 at 22:10
-
This is really interesting. Https is considerably slower than http, and, of course, both are much slower than curling 169.254.169.254 (which does not allow for https).– VinayCommented Jul 20, 2019 at 7:47
-
They have documented it in a small note here docs.aws.amazon.com/cli/latest/userguide/… Commented Feb 11, 2021 at 11:04
-
For me, today, on a generally restricted AWS server, only https://checkip.amazonaws.com (with https, not http) worked, and not the http version.– phhuCommented Jun 28, 2023 at 17:50
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.
-
3Personally, I try and avoid these websites as I don't have control over what they capture and log around a public address. Better to use a controlled endpoint under your own infrastructure, aws cli, or curl on 169.254.169.254 as per the other answers.– AriCommented Oct 12, 2020 at 0:34
-
1They'll also get sick of paying for the domain name and hosting one day, and your service will suddenly stop working because it can't resolve the random stranger's domain name and free service that you built a dependency on.– jbgCommented Nov 15, 2020 at 3:50
-
-
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
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
-
1
Get attached InstanceID with PublicIP.
aws ec2 describe-network-interfaces --query NetworkInterfaces[*].[Attachment.[InstanceId],Association.[PublicIp]] --output=json