Open In App

How To Get An AWS EC2 Instance ID From Within That EC2 Instance?

Last Updated : 22 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Amazon EC2 is an important service of AWS that provides users a scalable and virtual computing resources in the cloud. Each EC2 instance is assigned a unique identifier called Instance ID. Instance ID is an alphanumeric code(which means a combination of alphabets and numbers) that is used for various operational tasks including starting, stopping, and managing instances programmatically.

Have you ever wondered to get the EC2 instance ID when you are logged in . Knowing EC2 instance id is a crucial task for automation, integration of other AWS services, troubleshooting, and monitoring also. This EC2 instance ID can be obtained from the Instance Metadata. Instance Metadata is a crucial feature of EC2 instances that allows an instance to access its data like instance type, Instance ID, Public IP, private IP, etc.

Here I will walk you through the steps to get the EC2 instance ID from within the EC2 instance and also i will be discussing the potential errors you may face while following the steps.

Steps To Get Instance ID From Within The EC2 Instance

Step 1: Create an EC2 instance. (follow the video to create an AWS EC2 instance )

Step 2: Check the EC2 instance ID .

instance-id

Step 3 : Connect the EC2 instance and obtain an authentication token to access the EC2 instance’s metadata profile . This authentication token is important to access the metadata or else you will get unauthorized error .

TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`

This session token will be valid for next 6 hours .

add-token

Step 4 : Now get the EC2 instance id . Here also mention the correct path or else you will get not found error .

curl -H "X-aws-ec2-metadata-token: $TOKEN" -v http://169.254.169.254/latest/meta-data/instance-id

get-instance-id

Here you will see same EC2 instance ID as in the Step 2 .

Potential Errors

1 . When you try to obtain the authentication token without using the header (‘X-aws-ec2-metadata-token-ttl-seconds’) , then it will generate the ERROR 400-Missing or Invalid Parameters .

400

2 . When you try to access the metadata service without using a token or expired token or a invalid token , then you will get ERROR 401-Unauthorized .

unauthorized-access

3. When you try to access a invalid metadata path you will get ERROR 404-Not Found .

404-error

Conclusion

At first you have created an EC2 instance . Then added a session token and finally accessed the instance ID of EC2 . You have also learned about the potential errors that you will face while retrieving the instance ID .

How to get an AWS EC2 instance ID from within that EC2 instance -FAQs

1 . Does any IAM role is required to get EC2 instance ID from within the EC2 instance ?

The answer is no because EC2 instance metadata service does not require any IAM roles for accessing the instance information .

2 . How often EC2 instance ID change ?

EC2 instance does not change throughout the instance’s lifecycle . Even after the instance is stopped or restarted , the instance ID remains constant .

3 . In what situations the EC2 instance ID would be useful ?

EC2 instance can be used in scripts for doing any type of automation or it can also help in integration of the other AWS services also .

4 . Can i change the instance ID after the instance is launched ?

No you can not change the instance ID after the EC2 instance is launched as instance ID remains constant throughout the instance lifecycle .

5 . What other information can be retrieved from the metadata service ?

Other information like public IP , instance type , ami-id ,etc can also be retrieved from the metadata service .



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads