How to obtain current instance ID from boto3?

bmargulies picture bmargulies · Oct 23, 2015 · Viewed 12.3k times · Source

Is there an equivalent of

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

with boto3 to obtain the current running instance instance-id in python?

Answer

Jordon Phillips picture Jordon Phillips · Oct 23, 2015

There is no api for it, no. There is InstanceMetadataFetcher, but it is currently only used to fetch IAM roles for authentication.

Any sort of GET should serve you though. Botocore uses the python requests library which is quite nice.

import requests
response = requests.get('http://169.254.169.254/latest/meta-data/instance-id')
instance_id = response.text