An organization has deployed a retail processing system where an AWS Lambda function must verify transactions against a third-party payment vendor's HTTPS API. The function retrieves the vendor API keys from AWS Systems Manager Parameter Store. During load testing, the team notices latency spikes and hits the rate limit for Parameter Store API requests. Which implementation strategy will resolve these issues while maintaining security best practices?
- Retrieve the API keys from Parameter Store outside the Lambda handler function during the initialization phase, and store them in a global variable for subsequent invocations to reuse.Answer
- BCall the Parameter Store API inside the Lambda handler function on every invocation, relying on Lambda to automatically cache and deduplicate these external requests within the same execution context.
- CStore the API keys in plain text within the Lambda function's environment variables and initialize the AWS SDK client inside the handler on every invocation.
- DConfigure the Lambda function to run inside a private VPC subnet without a NAT Gateway or VPC endpoints to isolate the database queries and secure the payment vendor calls.
Answer
Retrieve the API keys from Parameter Store outside the Lambda handler function during the initialization phase, and store them in a global variable for subsequent invocations to reuse.
The correct strategy is to retrieve the API keys from Parameter Store outside the Lambda handler function during the initialization phase, and store them in a global variable. When AWS Lambda executes a function, it initializes the execution environment and runs the code outside the handler. When the function is invoked multiple times, Lambda reuses the same execution environment (warm starts), preserving variables declared in the global scope. This reduces the number of API calls to Parameter Store, prevents hitting API rate limits, and decreases the function's overall execution latency.
Step-by-Step Solution
Key Concept
AWS Lambda Execution Context Reuse
Estimated Time:1m 30s