Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boto3 equivalent to boto.utils.get_instance_metadata()?

In regular boto 2.38 I used to access instance metadata (e.g. get current stack-name), through boto's

boto.utils.get_instance_metadata() 

Is there an equivalent in boto3, or do I need to go to the down level direct http address to fetch metadata about the running instance?

like image 591
user2123288 Avatar asked Jul 25 '15 20:07

user2123288


People also ask

What is the difference between Boto and Boto3?

Boto versions include Boto, Boto3 and Botocore. Boto3 is the latest version of the SDK, providing support for Python versions 2.6. 5, 2.7 and 3.3. Boto3 includes several service-specific features to ease development.

What is IMDSv1?

You can access instance metadata from a running instance using one of the following methods: Instance Metadata Service Version 1 (IMDSv1) – a request/response method. Instance Metadata Service Version 2 (IMDSv2) – a session-oriented method.

What is Boto3 EC2?

The AWS SDK for Python (Boto3) provides a Python API for AWS infrastructure services. Using the SDK for Python, you can build applications on top of Amazon S3, Amazon EC2, Amazon DynamoDB, and more.


2 Answers

Nope, still no equivalent in boto3, just hit this gap myself.
They have an open feature request for this https://github.com/boto/boto3/issues/313 that references this question.

As for workarounds,
you can continue to use boto.utils or use urllib/urllib2 to do the HTTP requests manually ie.

# 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() 

see What is the quickest way to HTTP GET in Python? for a quick intro on urllib and http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-instance-metadata.html#instancedata-data-categories for the URI structure of the metadata service.

like image 196
Nath Avatar answered Sep 20 '22 04:09

Nath


You could use the third-party library ec2-metadata.

Here an example from the docs showing how to get your EC2 region:

  pip install ec2-metadata    >>> from ec2_metadata import ec2_metadata   >>> print(ec2_metadata.region)   us-east-1 
like image 39
barbasa Avatar answered Sep 21 '22 04:09

barbasa