|
Resources
Sample Code and Libraries
EC2 Instance Metadata Query Tool
 |
A simple bash script that uses curl to query the EC2 instance Metadata from within a running EC2 instance.
| Submitted By: |
Ravi@AWS
|
| AWS Products Used: |
Amazon EC2 |
| Language(s): |
Other |
| License: |
Apache License 2.0 |
|
|
 |
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 <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.
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
|
Posts:
5
Registered:
9/6/08
|
|
|
|
Should probably not smash all user-data lines together...
Posted:
Jan 7, 2009 4:35 PM PST
|
|
|
Ravi,
Nice tool... But when I use it on multi-line user-data, the user-data is all crammed together on one line. For multi-line user-data, that is a problem. E.g., I had user-data on my instance of:
associate-address 1.2.3.4
attach-volume hfskjhfdsj /dev/sdh
but when I access it vi ec2_metadata -d, I get:
$ ./ec2-metadata -d
user-data: asssociate-address 75.101.141.64 attach-volume hfskjhfdsj /dev/sdh
It would be nice if multi-line user-data could retain it's line breaks, either all the time or at least as an option.
Thanks for listening,
Chris
|
|
Posts:
1
Registered:
11/7/07
|
|
|
|
Re: Should probably not smash all user-data lines together...
Posted:
Jul 18, 2009 5:01 AM PDT
in response to: Chris Markle
|
|
|
This will preserve formatting of user-data provided the base64 or openssl command are found.
--- ec2-metadata.orig 2009-07-18 11:37:28.000000000 +0000
+++ ec2-metadata 2009-07-18 11:57:34.000000000 +0000
@@ -287,11 +287,21 @@
function print_user_data()
{
echo -n 'user-data: '
-x=$(curl -s
http://169.254.169.254/latest/user-data)
-if [ $(echo $x|grep 404|wc -l) -eq 0 ]; then
- echo $x
- else
- echo not available
+b64=`which base64 openssl 2>/dev/null | head -n1 | sed 's/openssl/openssl base64/'`
+if [ -z "$b64" ]; then
+ x=$(curl -s
http://169.254.169.254/latest/user-data)
+ if [ $(echo $x|grep 404|wc -l) -eq 0 ]; then
+ echo $x
+ else
+ echo not available
+ fi
+else
+ x=$(curl -s
http://169.254.169.254/latest/user-data|$b64)
+ if [ $(echo $x|$b64 -d|grep 404|wc -l) -eq 0 ]; then
+ echo $x | $b64 -d
+ else
+ echo not available
+ fi
fi
}
|
|
Posts:
6
Registered:
5/17/06
|
|
|
|
Re: Should probably not smash all user-data lines together...
Posted:
Jan 22, 2010 1:41 PM PST
in response to: Chris Markle
|
|
|
|
|
There's actually a pretty bad bug with this script. If you have '404' in any of your results (e.g. instance id i-abc12404), the script returns 'not available' for the result. I've attached a patch.
|
|
|
|
 |
|
|