feat: added caching to aws region query
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,40 @@
|
||||
#!/usr/bin/python3
|
||||
r"""
|
||||
Documentation
|
||||
|
||||
License: This program is released under the MIT License
|
||||
"""
|
||||
|
||||
# Imports
|
||||
import boto3
|
||||
import pickle
|
||||
import os
|
||||
|
||||
|
||||
PICKLE_FILE = 'cache.pkl'
|
||||
|
||||
def getRegions(all_regions=False) -> list[str]:
|
||||
"""
|
||||
Returns all aws regions, cached in pickle file to avoid repeated AWS calls.
|
||||
|
||||
:param all_regions:
|
||||
:return:
|
||||
"""
|
||||
cache_key = "aws_regions"
|
||||
|
||||
if os.path.exists(PICKLE_FILE):
|
||||
with open(PICKLE_FILE, 'rb') as f:
|
||||
cache = pickle.load(f)
|
||||
if cache_key in cache:
|
||||
return cache[cache_key]
|
||||
|
||||
# Fetch from AWS and cache it
|
||||
ec2 = boto3.client('ec2', region_name='us-east-1')
|
||||
response = ec2.describe_regions(AllRegions=all_regions)
|
||||
regions = [region['RegionName'] for region in response['Regions']]
|
||||
|
||||
cache = {cache_key: regions}
|
||||
with open(PICKLE_FILE, 'wb') as f:
|
||||
pickle.dump(cache, f)
|
||||
|
||||
return regions
|
||||
Reference in New Issue
Block a user