Rewritten Issue
I am trying to limit the number of calls to AWS Parameter Store in my AWS Lambda and am using a global variable to cache a Parameter Store value on the first call. However, if I want to cache multiple Parameter Store values, I must create multiple global variables and if not [INSERT_GLOBAL_VARIABLE]
checks. I have attempted to solve this issue by creating a ParameterUtil
class to cache the AWS Boto client as an instance variable and then get_parameter
using the cached Boto client. I am unsure if this solves the issue and would like to know if caching the Boto client will result in only one call per parameter to the Parameter Store when get_parameter
is called.
import os
import boto3
redis_password = None
def get_redis_password():
global redis_password
if not redis_password:
client = boto3.client("ssm")
redis_password = client.get_parameter(
Name=f"{os.environ["ENV"]}.redis-cache.password",
WithDecryption=True
)
return redis_password["Parameter"]["Value"]
def lambda_handler(event, context):
get_redis_password()
parameter_util.py
import os
import boto3
class ParameterUtil:
def __init__(self):
self.boto_client = boto3.client("ssm")
def get_parameter(self, parameter_path):
response = self.boto_client.get_parameter(
Name=f"{os.environ['ENV']}.{parameter_path}", WithDecryption=True
)
return response["Parameter"]["Value"]
main.py
import os
import boto3
from parameter_util import ParameterUtil
redis_password = None
def lambda_handler(event, context):
param_util = ParameterUtil()
param_util.get_parameter(".redis-cache.password")
param_util.get_parameter(".another.parameter.store.key")