- Newest
- Most votes
- Most comments
For what you are trying to achieve, I recommend using the AWS Parameters and Secrets Lambda Extension. It pretty much does what you are after: values are fetched and then, cached locally until a TTL expires, which you can configure using the SSM_PARAMETER_STORE_TTL environment variable.
See more details here: https://docs.aws.amazon.com/systems-manager/latest/userguide/ps-integration-lambda-extensions.html.
Yes, you can configure your Lambda function to retrieve values from Parameter Store once initially and then cache those values for subsequent invocations, even across cold starts. However, it's important to understand that this approach requires careful implementation and consideration of Lambda's execution environment behavior.
Here's how you can achieve this:
-
Use Lambda's execution environment reuse: When a Lambda function is invoked, AWS tries to reuse an existing execution environment if available. This means that any data stored in memory during one invocation can potentially be available for subsequent invocations.
-
Implement a caching mechanism: In your Lambda function code, you can create a caching mechanism using a static variable or object. This cache can store the values retrieved from Parameter Store.
-
Check the cache on each invocation: At the beginning of each function invocation, check if the cached values exist. If they do, use them. If not, retrieve the values from Parameter Store and store them in the cache.
-
Initialization outside the handler: Move the cache initialization logic outside the main handler function. This allows the cache to be set up during the execution environment's initialization phase, which happens before the first invocation and persists across invocations.
However, it's crucial to note some limitations and considerations:
-
No guarantee of persistence: While execution environments are often reused, AWS Lambda doesn't guarantee that your function will always use the same environment. New environments may be created for various reasons, including scaling or updates.
-
Potential for stale data: If the Parameter Store values change, your function won't automatically pick up these changes. You'll need to implement a mechanism to periodically check for updates or manually trigger a cache refresh when values change.
-
Cold starts: When a new execution environment is created (cold start), the cache will be empty, and you'll need to fetch from Parameter Store again.
-
Memory usage: Caching data in memory will increase your function's memory usage, which could impact cost and performance.
To implement this effectively, you might consider using a Lambda extension or a custom runtime that can manage the cache more robustly. These can provide more control over the caching lifecycle and potentially offer better persistence across cold starts.
Remember to balance the benefits of caching against the risk of using outdated data. Implement appropriate error handling and fallback mechanisms in case the cache fails or becomes unavailable.
Sources
how to set up caching in a lambda? | AWS re:Post
Community | We Improved Our Lambda Warm Start Speed by 95%! Here’s How
Relevant content
asked 3 years ago
asked 3 years ago
- AWS OFFICIALUpdated 2 days ago

Agree with the answer.. Another link https://docs.aws.amazon.com/lambda/latest/dg/with-secrets-manager.html