Get the public IP address in the current instance of EC2

Using the Amazon CLI, is there a way to get the public IP address of the current EC2? I'm just looking for a single string value, so the json response descriptions are not returned.

+30
source share
3 answers

You can use the AWS command line interface to return information about any / all instances of Amazon EC2, for example:

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

If you request information about the EC2 instance from which you are executing the command, then the current IP address can be obtained using 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/ 

Thus, the private IP address is accessible via:

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

The public IP address is available through:

 $ curl http://169.254.169.254/latest/meta-data/public-ipv4 54.232.200.77 
+55
source
 curl http://checkip.amazonaws.com 

this returns the public IP address.

+1
source

If you are inside an instance -

 $ curl icanhazip.com 162.202.17.123 
0
source

All Articles