There are a few ways to obtain the AWS EC2 instance ID from the same EC2 instance:
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
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
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.
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
Asked: 2021-05-01 11:00:00 +0000
Seen: 11 times
Last updated: May 28 '22