Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
819 views
in Technique[技术] by (71.8m points)

amazon web services - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...