How to get EC2 instance ID and other metadata

1 min read | by Jordi Prats

On an EC2 instance we can retrieve some of it's metadata by using curl. Let's see how

If we use the following curl on an EC2 instance we will be able to see the different metadata we are going to be able to retrieve:

$ curl http://169.254.169.254/latest/meta-data/ ami-id ami-launch-index ami-manifest-path block-device-mapping/ events/ hostname iam/ identity-credentials/ instance-action instance-id instance-life-cycle instance-type local-hostname local-ipv4 mac metrics/ network/ placement/ profile public-keys/ reservation-id security-groups services/ 

For example, we can get the instance ID using the following command:

$ curl http://169.254.169.254/latest/meta-data/instance-id i-0f672e225d382a524 

This IP address (169.254.169.254) is a link-local address it's just reachable from the instance and according to AWS documentation we are not going to get billed for HTTP requests used to retrieve instance metadata and user data.


Posted on 24/03/2021

Categories