10

I try to launch a service on ec2 instance. the service is supposed to send out the id of the instance. I know this could be obtained using something like curl http://0.0.0.0/latest/meta-data. is there any other ways that you could directly get the meta data maybe from the instance shell or some APIs in python?

Wang Nick
  • 385
  • 1
  • 6
  • 17

1 Answers1

18

Amazon EC2 instance metadata can be accessed via:

http://169.254.169.254/latest/meta-data/

To retrieve the ID of the instance from which that request is made:

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

This can be retrieved by curl, wget, web browser or anything that makes a call to retrieve an HTTP page.

If you wish to do it programmatically, here's some code from boto3 equivalent to boto.utils.get_instance_metadata()?:

# Python2
import urllib2
instanceid = urllib2.urlopen('http://169.254.169.254/latest/meta-data/instance-id').read()

# Python3
import urllib.request
instanceid = urllib.request.urlopen('http://169.254.169.254/latest/meta-data/instance-id').read().decode()

There is also a boto.utils.get_instance_metadata() call in boto (but not boto3) that returns the instance metadata as a nested Python dictionary.

Community
  • 1
  • 1
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • That's really useful!! thanks a lot – Wang Nick May 06 '17 at 05:54
  • 1
    Hi @WangNick, if this or any answer has solved your question please consider [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – John Rotenstein May 06 '17 at 14:13