Articles

Getting the AWS instance ID from within the EC2 instance


The EC2 Metadata: Getting your Amazon Instance ID and more

Sometimes it can be quite useful to be able to get the instance information from within the EC2 instance. A clear example is to be able to get the instance id.

Luckily for us, Amazon made this information available through the EC2 instance MetaData

How to get the EC2 Instance Metadata

The instance metadata is available in all amazon EC2 instances at the following address:

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

In there you can find quite a lot of useful information, including the instance id.

Getting the Amazon AWS EC2 instance id with CURL and WGET

To get the instance id of your EC2 instance, just query http://169.254.169.254/latest/meta-data/instance-id, either with CURL or WGET. To get the instance id with CURL from the command line, use:

Or rather by using wget:

Using the Amazon AWS MetaData tool

Amazon has also made publicly available its EC2 MetaData tool so it can be used from the command line to get all this information.

To install the metadata tool, follow these steps:

To get the instance if by using it, just do:

And it will yield the instance id in a pinch.

Enjoy!