How-to Get [Query] AWS EC2 Instance Metadata

Introduction

In this tutorial we will learn how to get EC2 Instance Metadata from an AWS EC2 instance. We will get the metadata using ec2-metadata and ec2 metadata. This is not typo. The tools name is really similar but one of them using dash on its name.

Using EC2 Instance Metadata Query Tool (ec2-metadata)

Install EC2 Instance Metadata Query Tool

First of all let’s install ec2-metadata tool.

Download the script using the command below.

$ wget http://s3.amazonaws.com/ec2metadata/ec2-metadata

Move the script to the /usr/local/bin/ directory.

$ sudo mv ec2-metadata /usr/local/bin

Change the permission of the script so we can use the script.

$ chmod u+x /usr/local/bin/ec2-metadata

Using EC2 Instance Metadata Query Tool Outside EC2 Instance

If you run ec2-metadata outside EC2 instance you will get the error message below.

[ERROR] Command not valid outside EC2 instance. Please run this command within a running EC2 instance.

When we run ec2-metadata from an EC2 instance without any option, the help page will be printed.

$ ec2-metadata v0.1.1
Use to retrieve EC2 instance metadata from within a running EC2 instance. 
e.g. to retrieve instance id: ec2-metadata -i
         to retrieve ami id: ec2-metadata -a
         to get help: ec2-metadata --help
For more information on Amazon EC2 instance meta-data, refer to the documentation at
http://docs.amazonwebservices.com/AWSEC2/2008-05-05/DeveloperGuide/AESDG-chapter-instancedata.html

Usage: ec2-metadata <option>
Options:
--all                     Show all metadata information for this host (also default).
-a/--ami-id               The AMI ID used to launch this instance
-l/--ami-launch-index     The index of this instance in the reservation (per AMI).
-m/--ami-manifest-path    The manifest path of the AMI with which the instance was launched.
-n/--ancestor-ami-ids     The AMI IDs of any instances that were rebundled to create this AMI.
-b/--block-device-mapping Defines native device names to use when exposing virtual devices.
-i/--instance-id          The ID of this instance
-t/--instance-type        The type of instance to launch. For more information, see Instance Types.
-h/--local-hostname       The local hostname of the instance.
-o/--local-ipv4           Public IP address if launched with direct addressing; private IP address if launched with public addressing.
-k/--kernel-id            The ID of the kernel launched with this instance, if applicable.
-z/--availability-zone    The availability zone in which the instance launched. Same as placement
-c/--product-codes        Product codes associated with this instance.
-p/--public-hostname      The public hostname of the instance.
-v/--public-ipv4          NATted public IP Address
-u/--public-keys          Public keys. Only available if supplied at instance launch time
-r/--ramdisk-id           The ID of the RAM disk launched with this instance, if applicable.
-e/--reservation-id       ID of the reservation.
-s/--security-groups      Names of the security groups the instance is launched in. Only available if supplied at instance launch time
-d/--user-data            User-supplied data.Only available if supplied at instance launch time.

Get The AMI ID used to launch this instance using ec2-metadata

$ ec2-metadata -a
ami-id: ami-002fe7e709bf81e3a

Get The index of this instance in the reservation (per AMI).

$ ec2-metadata -l
ami-launch-index: 0

Get The manifest path of the AMI with which the instance was launched.

$ ec2-metadata -m
ami-manifest-path: (unknown)

Get The AMI IDs of any instances that were rebundled to create this AMI.

$ ec2-metadata -n
ancestor-ami-ids: not available

Get Defines native device names to use when exposing virtual devices.

$ ec2-metadata -b
block-device-mapping: 
     ami: /dev/sda1
     root: /dev/sda1

Get EC2 Instance ID using ec2-metadata

$ ec2-metadata -i
instance-id: i-043c348f4bc24b818

Get Instance Type using ec2-metadata

$ ec2-metadata -t
instance-type: t2.nano

Get Private / Local Hostname of EC2 Instance

$ ec2-metadata -h
local-hostname: ip-172-31-16-47.ap-southeast-1.compute.internal

Get Private / Local IP of EC2 Instance

$ ec2-metadata -o
local-ipv4: 172.31.16.47

Get Kernel ID of the instance

$ ec2-metadata -k
kernel-id: not available

Get Availability Zone Of the EC2 Instance

$ ec2-metadata -z
placement: ap-southeast-1a

Get Product Codes of the EC2 Instance

$ ec2-metadata -c
product-codes: not available

Get Public Hostname of the EC2 Instance

$ ec2-metadata -p
public-hostname: ec2-13-229-225-234.ap-southeast-1.compute.amazonaws.com

Get Public IP of the EC2 Instance

$ ec2-metadata -v
public-ipv4: 13.229.225.234

Get Public Key of the EC2 Instance

$ ec2-metadata -u
public-keys: 
keyname:testing
index:0
format:openssh-key
key:(begins from next line)
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCV03OY7jrv/6rQyPL3ffsQcw7ENGixKFUpxaCXjIV1pD23rD9lb+mgX7OCBUsO4eXUUIPdr1ygh3o3AosM2CUv/pUexb4L2Mg2NkNyHQdfcGPQod0OFiw/bOd02FOD/oBxXQIl5UOuiXhfxEwZ5P3Hn1XaTwIMnT37e3gyfhif4XW9WKemCwCmSkPMvhEDYa0ymZElXvQ8YfkGWANktHj52de0b68D0Iwg6IVJbv9rqRcHVXk2Wp9V2X0cMLs8v9UYd4N0GmDB8FBC/ZAttWi6ESF0EKWv4jy/94EDP1nw9jD8tZnWiGV69vk/wKAoYI3BpZ6tAOTd82NdtFCWngrt testing

Get Ramdisk ID of The EC2 Instance

$ ec2-metadata -r
ramdisk-id: not available

Get Reservation ID using ec2-metadata

$ ec2-metadata -e
reservation-id: r-0589fc9d835e37094

Get Security Groups

$ ec2-metadata -s
security-groups: test-cluster

Get User-supplied data. Only available if supplied at instance launch time.

$ ec2-metadata -d
user-data: not available

Get all EC2 Metadata Using ec2-metadata

$ ec2-metadata --all

ami-id: ami-002fe7e709bf81e3a
ami-launch-index: 0
ami-manifest-path: (unknown)
ancestor-ami-ids: not available
block-device-mapping: 
     ami: /dev/sda1
     root: /dev/sda1
instance-id: i-043c348f4bc24b818
instance-type: t2.nano
local-hostname: ip-172-31-16-47.ap-southeast-1.compute.internal
local-ipv4: 172.31.16.47
kernel-id: not available
placement: ap-southeast-1a
product-codes: not available
public-hostname: ec2-13-229-225-234.ap-southeast-1.compute.amazonaws.com
public-ipv4: 13.229.225.234
public-keys: 
keyname:testing
index:0
format:openssh-key
key:(begins from next line)
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCV03OY7jrv/6rQyPL3ffsQcw7ENGixKFUpxaCXjIV1pD23rD9lb+mgX7OCBUsO4eXUUIPdr1ygh3o3AosM2CUv/pUexb4L2Mg2NkNyHQdfcGPQod0OFiw/bOd02FOD/oBxXQIl5UOuiXhfxEwZ5P3Hn1XaTwIMnT37e3gyfhif4XW9WKemCwCmSkPMvhEDYa0ymZElXvQ8YfkGWANktHj52de0b68D0Iwg6IVJbv9rqRcHVXk2Wp9V2X0cMLs8v9UYd4N0GmDB8FBC/ZAttWi6ESF0EKWv4jy/94EDP1nw9jD8tZnWiGV69vk/wKAoYI3BpZ6tAOTd82NdtFCWngrt testing
ramdisk-id: not available
reservation-id: r-0589fc9d835e37094
security-groups: test-cluster
user-data: not available

Using ec2metadata

ec2metadata (without dash) should already installed on Ubuntu / Amazon Linux based instance.

To test whether this command already exist or not we can use the --help option.

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

Query and display EC2 metadata.

If no options are provided, all options will be displayed

Options:
    -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)

    -u | --url URL          use URL (default: http://169.254.169.254/2009-04-04)

The the next sections we will learn to use each option of ec2metadata command to see the output sample.

Get Kernel ID of an EC2 Instance

$ ec2metadata --kernel-id
unavailable

Get Ramdisk ID of an EC2 Instance

$ ec2metadata --ramdisk-id
unavailable

Get Reservation ID of an EC2 Instance

$ ec2metadata --reservation-id
r-0589fc9d835e37094

Get AMI ID of an EC2 Instance

$ ec2metadata --ami-id
ami-002fe7e709bf81e3a

Get AMI Launch Index of an EC2 Instance

$ ec2metadata --ami-launch-index
0

Get AMI Manifest Path of an EC2 Instance

$ ec2metadata --ami-manifest-path
(unknown)

Get Ancestor AMI ID of an EC2 Instance

$ ec2metadata --ancestor-ami-ids
unavailable

Get Product Codes of an EC2 Instance

$ ec2metadata --product-codes
unavailable

Get Availability Zone Info of an EC2 Instance

$ ec2metadata --availability-zone
ap-southeast-1a

Get Instance ID of an EC2 Instance

$ ec2metadata --instance-id
i-043c348f4bc24b818

Get Instance Type of an EC2 Instance

$ ec2metadata --instance-type
t2.nano

Get Local Hostname of an EC2 Instance

$ ec2metadata --local-hostname
ip-172-31-16-47.ap-southeast-1.compute.internal

Get Public Hostname of an EC2 Instance

$ ec2metadata --public-hostname
ec2-13-229-225-234.ap-southeast-1.compute.amazonaws.com

Get Private IPv4 / Local IPv4 of an EC2 Instance

$ ec2metadata --local-ipv4
172.31.16.47

Get Public IPv4 of an EC2 Instance

$ ec2metadata --public-ipv4
13.229.225.234

Get Block Device Mapping Info of an EC2 Instance

$ ec2metadata --block-device-mapping
ami
root

Get Security Groups of an EC2 Instance

$ ec2metadata --security-groups
test-cluster

Get Mac Address of an EC2 Instance

$ ec2metadata --mac
unavailable

Get Instance Profile of an EC2 Instance

$ ec2metadata --profile
default-hvm

Get Instance Action of an EC2 Instance

$ ec2metadata --instance-action
none

Get Public Keys of an EC2 Instance

$ ec2metadata --public-keys
['ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCV03OY7jrv/6rQyPL3ffsQcw7ENGixKFUpxaCXjIV1pD23rD9lb+mgX7OCBUsO4eXUUIPdr1ygh3o3AosM2CUv/pUexb4L2Mg2NkNyHQdfcGPQod0OFiw/bOd02FOD/oBxXQIl5UOuiXhfxEwZ5P3Hn1XaTwIMnT37e3gyfhif4XW9WKemCwCmSkPMvhEDYa0ymZElXvQ8YfkGWANktHj52de0b68D0Iwg6IVJbv9rqRcHVXk2Wp9V2X0cMLs8v9UYd4N0GmDB8FBC/ZAttWi6ESF0EKWv4jy/94EDP1nw9jD8tZnWiGV69vk/wKAoYI3BpZ6tAOTd82NdtFCWngrt testing']

Get User Data of an EC2 Instance

$ ec2metadata --user-data

Which Package Providing ec2metadata In Ubuntu?

If you’re wondering which package providing the ec2metadata tool in Ubuntu, you can use the command below.

$ dpkg -S /usr/bin/ec2metadata

cloud-guest-utils: /usr/bin/ec2metadata

To see the cloud-guest-utils package installed we can use the command below

$ dpkg -l | grep cloud-guest-utils
ii  cloud-guest-utils              0.31-7-gd99b2d76-0ubuntu1         all          cloud guest utilities

We can also see the list of files in the cloud-guest-utils package using the command below.

$ dpkg -L cloud-guest-utils
/.
/usr
/usr/bin
/usr/bin/ec2metadata
/usr/bin/growpart
/usr/bin/vcs-run
/usr/share
/usr/share/doc
/usr/share/doc/cloud-guest-utils
/usr/share/doc/cloud-guest-utils/changelog.Debian.gz
/usr/share/doc/cloud-guest-utils/copyright
/usr/share/man
/usr/share/man/man1
/usr/share/man/man1/growpart.1.gz

References

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.