I have a bunch of different lambdas that have high traffic and each one has to go out to vault to grab an api key. The traffic only last during business hours so the full 8h work day.
I wanted to use the static cache in lambda by writing into the /tmp folder and doing a check on start up but looking into it that will only work for per container and each container has a ttl of 30-60mins I believe. The keys in vault don’t rotate as often so I want the cache to the keys for the full workday or close to it.
So I wanted to try out lambda extensions as that writes the keys into the layer as a cache. However, the docs say that the cache has an expiration on it but doesn’t say it the expiration is configurable or how long it is.
Does anyone know how long it takes for the cache to expire? If it’s configurable, and if extensions better for my use case vs static cache?
the doc I was looking at: https://aws.amazon.com/blogs/compute/using-the-aws-parameter-and-secrets-lambda-extension-to-cache-parameters-and-secrets/
The AWS Parameter and Secrets extension is an objectively good piece of software which nobody should never use. It can read and cache your secrets, and you can configure this behavior, but the code itself is entirely closed source.
I don’t really think we as users should accept that we’ll run unknown, closed-source code in our trusted/isolated Lambda sandboxes alongside our own code. Especially software which has explicit access to decrypt credentials and secrets.
That said, you could absolutely cache credentials in /tmp, but it would be simpler to assign them to a variable in the global scope of your Lambda function and refresh them as needed.
Lambda extensions run alongside a single Lambda function sandbox, so there’s no shared cache benefit you gain across multiple concurrent Lambda sandboxes. I don’t think you need Extensions for this application, unless you very specifically wanted to re-cache secrets after the function execution has finished.
Hope this helps!
I agree. Any variable set outside the handler call will be kept in memory for the next call. Super simple solution and if works.
As long as the Lambda is still active. They don’t run forever…even when highly utilized, they will eventually terminate and new invocations will spawn. But they can run for 45 minutes and that’s probably a lot of savings of thrashing to get api keys.
Yes. For sure. While each instance is live.
The AWS Parameter and Secrets Extension is similarly tied to the Lambda Lifecycle. It has no external cache state, and it’s state cannot be shared across multiple Lambda functions. So it should always cost the same or less to cache the secret yourself (in terms of SSM/KMS/Secrets Manager costs, as well as Lambda compute time)
It’d be better to avoid caching credentials in /tmp. We were doing this until this week when the cached credentials expired. The Lambda invocations failed which caused the Lambda container to reset itself, but this reset doesn’t include /tmp. Every subsequent invocation of that container failed as the Lambda found the invalid cached credentials on start up.
So is there no secure way to cache secrets between lambda containers?
from what I’m hearing from the comments, the best solution is just global scope the keys for the duration of the lambda lifecycle?
It’s not about security, it’s about design. Lambda functions are stateless, so are extensions.
If you need a shared cache between Lambda sandboxes, you need to build that externally. You can use something like Elasticache or Momento, or DynamoDB.
We use ElastiCache, simple, quite cheap and really fast. The downside is if you ever have to remove your lambda function, it takes really long time for cloudformation to remove it from the VPC.
One option is to have a lambda that periodically fetches and caches the key, i typically use SecretsManager as the cache and then all your lambdas just read the key.