A simpler curl
command would be:
$ curl -s http://169.254.169.254/latest/meta-data
and will return the following output:
ami-id
ami-launch-index
ami-manifest-path
block-device-mapping/
events/
hostname
identity-credentials/
instance-action
instance-id
instance-life-cycle
instance-type
ipv6
local-hostname
local-ipv4
mac
metrics/
network/
placement/
profile
public-hostname
public-ipv4
public-keys/
reservation-id
security-groups
services/
As you see here, the URL to get the "public IPv4" would be:
$ curl -s http://169.254.169.254/latest/meta-data/public-ipv4
Some other (shell script) examples:
localIP=$(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)
publicIP=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4)
inst_type=$(curl -s http://169.254.169.254/latest/meta-data/instance-type)
av_zone=$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)
On the other hand, to get the local IPv4 address and the IPv6 address (AWS does not provide an API to query the IPv6 address) with a system command, you can use:
$ hostname -I
Note that there is no way to get the public IPv4 unless you ask for it on an external server, like the "AWS server" (http://169.254.169.254/
) or servers on Internet (as provided in the other answer).