How to retrieve AWS EC2 Instance Metadata using Python
Introduction
Instance metadata is a set of information about the instance that is available to the instance itself. Instance metadata is available from within the instance and is used to configure and manage the running instance.
In this tutorial, we will look at how to retrieve the instance metadata using Python.
How to query metadata using Python?
Instance metadata
Instance metadata is divided into categories like hostname, events and security groups. To check the complete list of categories, you can use the following command:
import requests
response = requests.get("http://169.254.169.254/latest/meta-data")
print(r.text.split("\n"))
Output:
['ami-id', 'ami-launch-index', 'ami-manifest-path', 'block-device-mapping/', 'events/', 'hostname', 'identity-credentials/', 'instance-action', 'instance-id', 'instance-life-cycle', 'instance-type', 'local-hostname', 'local-ipv4', 'mac', 'metrics/', 'network/', 'placement/', 'profile', 'public-hostname', 'public-ipv4', 'public-keys/', 'reservation-id', 'security-groups', 'services/', 'system']
To get the value of a specific category, you can use the following command:
def get_aws_metadata():
metadata = {
'public-hostname': "",
'ami-id': "",
'instance-id': ""
}
for key in metadata.keys():
resp = requests.get(
f'http://169.254.169.254/latest/meta-data/{key}',
timeout=1
)
if resp.status_code != 200:
raise Exception()
metadata[key] = resp.text
return metadata
Output:
get_aws_metadata()
{
'public-hostname': 'ec2-xx-xxx-70-142.us-west-2.compute.amazonaws.com',
'ami-id': 'ami-055699311e5e8f1e1',
'instance-id': 'i-xxxx'
}
Dynamic instance data
The dynamic instance data is a bit more limited than the instance metadata. It can still be useful depending on your usecase. You can access the dynamic instance data using the following commands:
import requests
response = requests.get("http://169.254.169.254/latest/dynamic/instance-identity/document")
print(response.json())
Output:
{'accountId': 'xxxxx',
'architecture': 'arm64',
'availabilityZone': 'us-west-2a',
'billingProducts': None,
'devpayProductCodes': None,
'marketplaceProductCodes': None,
'imageId': 'ami-055699311e5e8f1e1',
'instanceId': 'i-xxxx',
'instanceType': 'c6g.medium',
'kernelId': None,
'pendingTime': '2023-02-28T04:00:45Z',
'privateIp': '172.31.41.179',
'ramdiskId': None,
'region': 'us-west-2',
'version': '2017-09-30'
}
Conclusion
In this tutorial, we looked at how to retrieve the instance metadata using Python. We also looked at how to retrieve the dynamic instance data using Python.