How to Get the EC2 Instance ID from Within an EC2 Instance?

Every EC2 instance has associated metadata, which AWS makes available to all users & applications inside the instance. The instance ID is part of this metadata. Here’s a complete list of everything included in the metadata — Instance metadata categories. Run the following at a Bash prompt to get the instance ID:

wget -q -O -

The same can be captured in a variable for use in shell scripts like so:

EC2_INSTANCE_ID="`wget -q -O -`"

You can get a lot more info about the instance from its metadata, like its availability zone, region, etc.:

EC2_AVAIL_ZONE="`wget -q -O -`"
EC2_REGION="`echo \"$EC2_AVAIL_ZONE\" | sed -e 's:\([0-9][0-9]*\)[a-z]*\$:\\1:'`"

cURL can also be used instead of wget:


You can also use http://instance-data/ in place of but only if you’re using Amazon’s DNS. If you need to fetch various details about the instance, try /dynamic/instance-identity/document instead of making multiple calls to the metadata URL:

$ wget -q -O -

    "devpayProductCodes" : null,
    "privateIp" : "",
    "region" : "us-east-1",
    "kernelId" : "aki-12345678",
    "ramdiskId" : null,
    "availabilityZone" : "us-east-1a",
    "accountId" : "123456789012",
    "version" : "2010-08-31",
    "instanceId" : "i-12345678",
    "billingProducts" : null,
    "architecture" : "x86_64",
    "imageId" : "ami-12345678",
    "pendingTime" : "2020-01-01T05:01:23Z",
    "instanceType" : "t2.small"

These HTTP calls are not billed. The instance ID is also stored in /var/lib/cloud/data/instance-id.


On Amazon Linux AMIs, you can use:

$ ec2-metadata -i
instance-id: i-1234567890abcdef0

To catch it in a variable:

EC2_INSTANCE_ID=$(ec2-metadata --instance-id | cut -d " " -f 2);

Or on Ubuntu & other Linux flavors:

ec2metadata --instance-id

You might need to install this utility first:

sudo apt-get install cloud-utils

A LOT more is available using these utils:

ec2metadata --help
Syntax: /usr/bin/ec2metadata [options]

Query and display EC2 metadata.

If no options are provided, all options will be displayed

    -h --help               show this help

    --kernel-id             display the kernel id
    --ramdisk-id            display the ramdisk id
    --reservation-id        display the reservation id

    --ami-id                display the ami id
    --ami-launch-index      display the ami launch index
    --ami-manifest-path     display the ami manifest path
    --ancestor-ami-ids      display the ami ancestor id
    --product-codes         display the ami associated product codes
    --availability-zone     display the ami placement zone

    --instance-id           display the instance id
    --instance-type         display the instance type

    --local-hostname        display the local hostname
    --public-hostname       display the public hostname

    --local-ipv4            display the local ipv4 ip address
    --public-ipv4           display the public ipv4 ip address

    --block-device-mapping  display the block device id
    --security-groups       display the security groups

    --mac                   display the instance mac address
    --profile               display the instance profile
    --instance-action       display the instance-action

    --public-keys           display the openssh public keys
    --user-data             display the user data (not actually metadata)


Hit the same endpoint programmatically to fetch this info:

string instanceId = new StreamReader(HttpWebRequest


import boto.utils
region = boto.utils.get_instance_metadata()['local-hostname'].split('.')[1]

Or at the command line as:

python -c "import boto.utils; print boto.utils.get_instance_metadata()['local-hostname'].split('.')[1]"

Another example:



new AWS.MetadataService().request('instance-id',
  (error, data) => instanceID = data)


(New-Object System.Net.WebClient)

Or simply:

$instanceId=(Invoke-WebRequest -Uri


require 'rubygems'
require 'aws-sdk'
require 'net/http'

metadata_endpoint = ''
instance_id = Net::HTTP.get( URI.parse( metadata_endpoint + 'instance-id' ) )

ec2 =
instance = ec2.instances[instance_id]


import com.amazonaws.util.EC2MetadataUtils;
String myId = EC2MetadataUtils.getInstanceId();


import com.amazonaws.util.EC2MetadataUtils
val myid = EC2MetadataUtils.getInstanceId


import ("")
idBytes, err := aws.GetMetaData("instance-id")
id = string(idBytes)


$instance = json_decode(file_get_contents(
$id = $instance['instanceId'];