Search code examples
pythonamazon-ec2automationboto

Get ec2 instance metadata from instance id


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


Solution

  • 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?