EC2 Instance Metadata Query Tool

Sample Code & Libraries>Amazon EC2>EC2 Instance Metadata Query Tool
Community Contributed Software

  • Amazon Web Services provides links to these packages as a convenience for our customers, but software not authored by an "@AWS" account has not been reviewed or screened by AWS.
  • Please review this software to ensure it meets your needs before using it.

A simple bash script that uses curl to query the EC2 instance Metadata from within a running EC2 instance.

Details

Submitted By: Chaitan@AWS
AWS Products Used: Amazon EC2
License: Apache License 2.0
Created On: October 30, 2008 11:49 PM GMT
Last Updated: August 30, 2012 3:14 AM GMT
Download

Updates

  • 2012-08-27: Bugfix - This version fixes a bug where the script was incorrectly reporting metadata as not available.

Prerequisites

  • You must run this from within an EC2 instance running on linux/unix platform
  • Curl

Using the tool

  • Download the tool from http://s3.amazonaws.com/ec2metadata/ec2-metadata

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

  • Check file permissions and make sure that it is executable

    	$ls -l ec2-metadata
    	-rwxr-xr-x 1 root root 10912 2008-10-23 19:07 ec2-metadata
          

  • If not, change the file premissions and make it executable

    	$ chmod u+x ec2-metadata
          

  • Cool, now run help:

    	$ ec2-metadata --help
    	Usage: ec2-metadata
    	  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.
          

Examples

  • To get the ami-id of the instance, run

    	$ec2-metadata -a
    	ami-id: ami-xxxxxxx
          

  • To get the public hostname, run

    	$ec2-metadata -p
    	public-hostname: ec2-x-x-x-x.compute-1.amazonaws.com
          

  • To get the local ipv4, run

    	$ec2-metadata -o
    	local-ipv4: 10.x.x.x
          

  • and so on ...

You can copy this script to your /usr/bin folder.

For more information on EC2 metadata, consult the Amazon EC2 documentation at http://docs.amazonwebservices.com/AWSEC2/2008-08-08/DeveloperGuide/AESDG-chapter-instancedata.html

©2014, Amazon Web Services, Inc. or its affiliates. All rights reserved.