Get ec2 instance metadata from instance id

Question:

Hi I am using boto to spin up ec2 spot instance. I am having trouble getting instance hostname from instance id.

there is easies way to do from instance itself “wget -q -O – http://169.254.169.254/latest/meta-data/instance-id

but I am looking for way to get metadata using instance id

Any Help

Thanks

Asked By: roy

||

Answers:

The instance metadata is only available on the instance but you can get a lot of information about your instance using the EC2 API. So, if you have the instance ID you can do this:

import boto.ec2
conn = boto.ec2.connect_to_region('us-east-1')  # or whatever region you use
reservations = conn.get_all_instances(instance_ids='i-12345678')
instance = reservations[0].instances[0]
print(instance.public_dns_name)

Would print the public DNS name (i.e. hostname) of the instance.

Is that what you are looking for?

Answered By: garnaat

You can use "wget -q -O – http://169.254.169.254/latest/meta-data/hostname"
to get EC2 instance hostname

Answered By: Damyan Marchev
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.