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?
Dec 16, 2020 • ec2. AWS Boto3 is the Python SDK for AWS. Boto3 can be used to directly interact with AWS resources from Python scripts. In this tutorial, we will look at how we can use the Boto3 library to perform various operations on AWS EC2. Table of contents.
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
In fact, scrap that answer. All you need is ec2-metadata
https://github.com/adamchainz/ec2-metadata
pip3 install ec2-metadata
from ec2_metadata import ec2_metadata
print(ec2_metadata.instance_id)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With