import json try: #py3 from urllib.request import urlopen except ImportError: #py2 from urllib2 import urlopen class EC2InstanceIdentity(object): """ Assists in discovery EC2 instance identity info Ref: http://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/instance-identity-documents.html """ METADATA_URL = 'http://169.254.169.254/latest/dynamic/instance-identity/document' """What URL to hit for the instance identity document""" def __init__(self): self._json_document = None def __fetch_document(self): """ Fetches the instance identity document & parses it """ raw_document = urlopen(self.METADATA_URL).read() self._json_document = json.loads(raw_document) def __getattr__(self, attr_name): if not self._json_document: self.__fetch_document() return self._json_document[attr_name]