Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.