Question

Difficulty: MediumServerless Development with AWS Lambda

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?

  1. 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
  2. B
    Call 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.
  3. C
    Store 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.
  4. D
    Configure 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

1
Analyze the scope of the variable initialization in the Lambda function.
Variables initialized outside the handler function (in the global scope) are preserved when the execution context is reused across multiple warm invocations.
Understanding Lambda's execution environment lifecycle helps identify where to place code that does not need to run on every invocation.
2
Move the Systems Manager Parameter Store API call to the global initialization block.
The API key is fetched once during the function's cold start (initialization phase) instead of on every invocation.
This avoids repeating API calls to Parameter Store, preventing rate limiting and lowering execution latency for subsequent requests.
3
Ensure secure storage and network paths are maintained.
The credentials remain securely stored in Parameter Store, and the Lambda function retains outbound internet access to call the third-party payment gateway.
Storing credentials securely and ensuring proper network configurations are critical for production serverless applications.

Key Concept

AWS Lambda Execution Context Reuse
Estimated Time:1m 30s
Rate this question