Skip to content

Instantly share code, notes, and snippets.

@noamtamim
Created April 21, 2021 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noamtamim/d2a0fc4329394281227486d456982b5c to your computer and use it in GitHub Desktop.
Save noamtamim/d2a0fc4329394281227486d456982b5c to your computer and use it in GitHub Desktop.
Get AWS Region (with cache)
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