There is an easy way to access to Instance information from within. This is very useful when writing scripts who are executed inside the Instance. The method is accessing the Instance Metadata using a HTTP GET call to the IP 169.254.169.254. It works on any EC2 instance and the IP address is always the same.
Examples:
– Obtaining the Instance ID:
$ wget -q -O – http://169.254.169.254/latest/meta-data/instance-id i-87eef4e2 |
– Public Hostname:
$ wget -q -O – http://169.254.169.254/latest/meta-data/public-hostname ec2-50-17-85-234.compute-1.amazonaws.com |
– Public IPv4 Address:
$ wget -q -O – http://169.254.169.254/latest/meta-data/public-ipv4 50.17.85.234 |
Check the Instance Metadata manual page for further reference.
Unfortunately, the Instance Tags are not available through Metadata yet (forums). There is a workaround using the API ec2-describe-instances command.
Example: To obtain a whole Tag list from the instance we are running in:
$ ec2-describe-instances `wget -q -O – http://169.254.169.254/latest/meta-data/instance-id` –show-empty-fields | grep TAG
TAG instance i-87eef4e2 Another Tag Another Value |