aws/aws-secretsmanager-caching-python

Right way to implement caching

Closed this issue · 1 comments

Following the README.md, I have implemented caching. Essentially added the following the code:

session = boto3.session.Session()
client = session.client(
    service_name='secretsmanager',
    region_name=region_name
)
config=SecretCacheConfig()
cache = SecretCache(config=config, client=client)
secrets = cache.get_secret_string(secret_id=secret_id)

and removed the old code of how I was fetching the secrets i.e.

session = boto3.session.Session()
client = session.client(
    service_name='secretsmanager',
    region_name=region_name
)

secret_value_response = client.get_secret_value(
     SecretId=secret_id
)

The question I have is: Is it the right way to implement way caching? As it appears it doesn't seem to work. I am using this piece of code in a flask api server. Whenever I make the api call, the code uses secrets to fetch data from mysql db. What I have observed is at each API call, the get_secret_string() function is calling _client.describe_secret() (this function). This function is adding time to API call and making overall response time of api high.

Please help

All of my lambda runtimes increased dramatically as well doing the exact same thing as the OP. Please answer the question. My understanding is that implementing this should lower lambda execution time.
Basically it not clear how to detect an existing cache and reuse it on subsequent lambda invocations. If you could please explain that with an example it would be very appreciated.