When working with AWS EC2 instances via SSH, you often need to identify the specific instance you're logged into. AWS provides several ways to access this information through instance metadata and local commands.
The most reliable method is querying the instance metadata service:
# Get instance ID
curl http://169.254.169.254/latest/meta-data/instance-id
# Get AMI ID
curl http://169.254.169.254/latest/meta-data/ami-id
# Get public hostname
curl http://169.254.169.254/latest/meta-data/public-hostname
# Get private hostname
curl http://169.254.169.254/latest/meta-data/local-hostname
For systems without curl or when you need additional information:
# Using wget instead of curl:
wget -q -O - http://169.254.169.254/latest/meta-data/instance-id
# Getting availability zone:
cat /var/lib/cloud/data/instance-id 2>/dev/null || \
curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone
To access instance tags, you'll need AWS CLI installed and configured:
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
REGION=$(curl -s http://169.254.169.254/latest/meta-data/placement/region)
# Get all tags
aws ec2 describe-tags \
--filters "Name=resource-id,Values=$INSTANCE_ID" \
--region $REGION \
--output json
Here's a handy bash script that combines these methods:
#!/bin/bash
echo "=== EC2 Instance Information ==="
echo "Instance ID: $(curl -s http://169.254.169.254/latest/meta-data/instance-id)"
echo "AMI ID: $(curl -s http://169.254.169.254/latest/meta-data/ami-id)"
echo "Public DNS: $(curl -s http://169.254.169.254/latest/meta-data/public-hostname)"
echo "Private DNS: $(curl -s http://169.254.169.254/latest/meta-data/local-hostname)"
echo "Region/AZ: $(curl -s http://169.254.169.254/latest/meta-data/placement/region)/$(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)"
For regular use, add this function to your ~/.bashrc:
function ec2info() {
echo "Instance: $(curl -s http://169.254.169.254/latest/meta-data/instance-id)"
echo "AMI: $(curl -s http://169.254.169.254/latest/meta-data/ami-id)"
echo "Type: $(curl -s http://169.254.169.254/latest/meta-data/instance-type)"
echo "Public DNS: $(curl -s http://169.254.169.254/latest/meta-data/public-hostname)"
}
When working with AWS EC2 instances via SSH, you can use the instance metadata service to retrieve vital information. The metadata is available at this endpoint:
curl http://169.254.169.254/latest/meta-data/
For Ubuntu and Red Hat instances, these commands will give you specific information:
# Get instance ID
curl http://169.254.169.254/latest/meta-data/instance-id
# Get AMI ID
curl http://169.254.169.254/latest/meta-data/ami-id
# Get public hostname
curl http://169.254.169.254/latest/meta-data/public-hostname
# Get private IP
curl http://169.254.169.254/latest/meta-data/local-ipv4
To access tags assigned to the instance, you'll need to use the AWS CLI (must be installed and configured):
aws ec2 describe-tags \\
--filters "Name=resource-id,Values=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)" \\
--region $(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/.$//')
Create this function in your .bashrc for easy access to common metadata:
function ec2info() {
echo "Instance ID: $(curl -s http://169.254.169.254/latest/meta-data/instance-id)"
echo "AMI ID: $(curl -s http://169.254.169.254/latest/meta-data/ami-id)"
echo "Public DNS: $(curl -s http://169.254.169.254/latest/meta-data/public-hostname)"
echo "Private IP: $(curl -s http://169.254.169.254/latest/meta-data/local-ipv4)"
echo "Availability Zone: $(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone)"
}
For more comprehensive metadata access, consider these packages:
# Ubuntu/Debian
sudo apt install cloud-utils
# RHEL/CentOS
sudo yum install cloud-utils
Then use ec2metadata command:
ec2metadata --instance-id
ec2metadata --public-hostname
ec2metadata --ami-id
Remember that instance metadata is only accessible from within the instance itself. The 169.254.169.254 address is a link-local address and cannot be accessed from outside the instance.