Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain current instance ID from boto3?

Tags:

python

boto3

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?

like image 890
bmargulies Avatar asked Oct 23 '15 12:10

bmargulies


People also ask

What is boto3 EC2?

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.


2 Answers

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
like image 76
Jordon Phillips Avatar answered Sep 19 '22 19:09

Jordon Phillips


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)
like image 22
pixiemops Avatar answered Sep 21 '22 19:09

pixiemops