Accessing Amazon EC2 Instance information from the command line using curl

3268

If you use Amazon Web Services EC2 for virtualising your infrastructure, you may find that once you have a few instances started, that it may be hard (especially if you aren’t using a configuration management tool) to trace back ‘which machine belongs to what security group?’ or ‘which AMI did I use to bring up this instance?.’

 

Well theres a few quick tricks you can use to get some info out of your running instances. To do this you need to access the instances meta-data, and this is available simply by using ‘Curl’ from your running Linux instance, as follows:

 

‘curl http://169.254.169.254/latest/meta-data/<metadata-object>’

 

here are some of the metadata objects:

ami-launch-index

ami-manifest-path

block-device-mapping/

hostname

instance-action

instance-id

instance-type

kernel-id

local-hostname

local-ipv4

placement/

profile

public-hostname

public-ipv4

public-keys/

reservation-id

security-groups

 

ami-manifest-path will tell you from where on AWS the AMI (the Amazon Machine Image for the linux virtual machine) was launched. If you rolled/composed your own AMI this helps you match up which Bucket on S3 relates to your AMI.

 

for example:curl http://169.254.169.254/latest/meta-data/ami-manifest-path

 

 

public-ipv4 is the world-facing IP address of the running virtual machine.

 

If your not sure from looking at the AWS Dashboard (or dont have access to it) you can run:

 

curl http://169.254.169.254/latest/meta-data/instance-id

 

this will help you make sure if you want to terminate an instance that you kill off the correct one.

 

Its easy to experiment with, and the fact that you use curl to access the metadata information, it means that its also something thats pretty easy to compose a shell script for commonly needed tasks.

Hope someone finds this helpful.