A developer is designing a serverless application that integrates with a third-party billing API. The application uses an AWS Lambda function to send requests. The API key for the billing provider is stored in AWS Secrets Manager and is automatically rotated every hours. During initial load testing, retrieving the key from Secrets Manager on every function invocation significantly increases the execution latency and Secrets Manager API costs. The developer wants to optimize the retrieval process while ensuring the function always uses a valid, unexpired API key. Which approach meets these requirements with the lowest latency and cost?
- ARetrieve the API key from Secrets Manager in the initialization code outside the handler and store it in a global variable, relying on Lambda to automatically re-run the initialization phase when the secret is rotated in Secrets Manager.
- BDeploy the Lambda function inside a private subnet of a custom VPC without a NAT gateway or VPC endpoint, and retrieve the API key from Secrets Manager inside the handler function on every invocation using the AWS SDK.
- Configure the AWS Parameters and Secrets Lambda Extension in the Lambda function, and retrieve the API key via a local HTTP request with a time-to-live (TTL) of seconds.Answer
- DConfigure the API key as a secure string parameter in Systems Manager Parameter Store, and write custom logic in the Lambda handler to fetch the parameter using hardcoded IAM access keys to bypass runtime credential checks.
Answer
Configure the AWS Parameters and Secrets Lambda Extension in the Lambda function, and retrieve the API key via a local HTTP request with a time-to-live (TTL) of seconds.
The correct approach is to configure the AWS Parameters and Secrets Lambda Extension. This extension runs alongside the Lambda function container and caches secrets locally, exposing a localhost endpoint. When the handler queries the local HTTP endpoint, the extension returns the cached key. If the key has expired based on the configured Time-to-Live (TTL), the extension calls AWS Secrets Manager to retrieve the new key. A short TTL like seconds ensures that when the key is rotated every hours, the cached value is refreshed within minutes, preventing authentication failures while still providing low latency and low Secrets Manager API costs.
Step-by-Step Solution
Key Concept
Caching secrets using the AWS Parameters and Secrets Lambda Extension is the recommended best practice for optimizing performance and cost when Lambda functions consume secrets that undergo periodic rotation.