Ask Your Question
1

What is the method to obtain the AWS EC2 instance ID from the same EC2 instance?

asked 2021-05-01 11:00:00 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2022-05-28 04:00:00 +0000

lakamha gravatar image

There are a few ways to obtain the AWS EC2 instance ID from the same EC2 instance:

  1. Use the EC2 metadata service which provides a simple HTTP interface that can be used to access temporary EC2 instance metadata. The following command can be run on the EC2 instance to retrieve the instance ID:

    curl http://169.254.169.254/latest/meta-data/instance-id

  2. Use the AWS CLI installed on the EC2 instance to retrieve the instance ID by running:

    aws ec2 describe-instances --instance-ids $(curl -s 169.254.169.254/latest/meta-data/instance-id) --query 'Reservations[0].Instances[0].InstanceId' --output text

  3. Use an SDK in your preferred programming language (e.g. Python, Java, Node.js) to obtain the instance ID by making use of the EC2 metadata service or AWS CLI.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-05-01 11:00:00 +0000

Seen: 11 times

Last updated: May 28 '22