EC2 Instance Metadata Query Tool

Sample Code & Libraries>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: Ravi@AWS
AWS Products Used: Amazon EC2
License: Apache License 2.0
Created On: October 30, 2008 11:49 PM GMT
Last Updated: November 4, 2008 4:36 PM GMT
Download

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 

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

©2010, Amazon Web Services LLC or its affiliates. All rights reserved.