AWS assign a unique id known as an instance ID to each EC2 instance which is needed to manage and interact with the instance. In this article, we will see the best way to get the instance ID from an EC2 instance.

Instance Metadata service

Using the AWS EC2 Instance Metadata Service we can access instance-specific information, such as the Instance ID. By making a simple HTTP GET request to a well-known metadata URL, we can retrieve the EC2 information.

Following are the detailed steps which you can follow to retrieve the AWS instance ID.

  1. Connect to your AWS instance using SSH or maybe using a remote desktop.
  2. After connecting to your EC2 instance, in the terminal make an HTTP request to the following URL. You can use the command line tools such as wget or cURL to make an HTTP GET request to the metadata service URL.
$ curl http://169.254.169.254/latest/meta-data/instance-id

In the above example, I use the cURL utility to make an HTTP request.

Following is the sample output which I got when I run this from my instance.

i-02e3a2e0d9d12047c

You can also refer to the following link for more information on this.

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html

Categorized in:

Tagged in: