Created
April 21, 2021 16:11
-
-
Save noamtamim/d2a0fc4329394281227486d456982b5c to your computer and use it in GitHub Desktop.
Get AWS Region (with cache)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_region_name(): | |
from urllib import request, error | |
me = get_region_name | |
if hasattr(me, 'cache'): | |
return me.cache | |
try: | |
with request.urlopen("http://169.254.169.254/latest/meta-data/placement/region", timeout=1) as res: | |
me.cache = res.read().decode().strip() | |
except error.URLError: | |
me.cache = None | |
return me.cache |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment