Web Service version of the EC2 Instance Metadata Query Tool
I think most of the EC2 users know about EC2′s instance metadata query tool. It’s an executable you can install on your ec2 instance. Once you save the file and make it executable you can get metadata of the instance such as hostname, block-device-mapping, instance-id etc.
But there is a web service version of this metadata tool too. This version is not so much advertised. Whenever I google ‘aws metadata query tool’, I never find it. I like the webservice version better becasue I don’t have to go through installation steps everytime I want get metadata of an instance.
Here is how you can use the webservice version:
curl http://169.254.169.254/latest/meta-data/
This will print all the metadata fields available for querying. It should print something like:
ami-id
ami-launch-index
ami-manifest-path
block-device-mapping/
hostname
instance-action
instance-id
instance-type
kernel-id
local-hostname
local-ipv4
placement/
public-hostname
public-ipv4
public-keys/
reservation-id
security-groups
Now, to get instance-id,you simply have to execute the following command:
curl http://169.254.169.254/latest/meta-data/instance-id
It will simply print the instance id of the machine you are executing this command on.
Please note that some metadata fields listed above have a / in front of them. The webservice url for those fields must have forward slash at the end. For example
curl http://169.254.169.254/latest/meta-data/public-keys
will not print anything. You must execute the command as:
curl http://169.254.169.254/latest/meta-data/public-keys/

Leave a Reply