A developer is writing a backend AWS Lambda function that must parse a static lookup table stored in Amazon S3. The lookup table is updated only once a week, but the Lambda function is invoked thousands of times per hour. The developer wants to optimize the function's execution time and minimize Amazon S3 data retrieval costs. Which of the following is the most efficient design pattern for the developer to implement?
- AWrite the downloaded lookup table directly to the function's deployment package directory (`./`) during the first run to persist it for all future invocations.
- Download the lookup table to the `/tmp` space and load it into a global variable outside of the Lambda handler function, reusing the cached data for subsequent warm starts.Answer
- CDeploy the Lambda function inside a private subnet of a VPC to download the lookup table from Amazon S3 on every invocation, without configuring a NAT Gateway or a VPC endpoint.
- DStore the lookup table in AWS Secrets Manager and initialize the AWS SDK client inside the handler function to retrieve the data on each invocation.
Answer
Download the lookup table to the `/tmp` space and load it into a global variable outside of the Lambda handler function, reusing the cached data for subsequent warm starts.
By downloading the lookup table to the `/tmp` space and parsing it into a global variable outside the handler function, the initialization code executes only during a cold start. Subsequent invocations that reuse the warm execution context skip this download and parse phase entirely, accessing the cached data directly from memory. This provides sub-millisecond access times and minimizes S3 API charges.
Step-by-Step Solution
Key Concept
Reusing the Lambda execution context (global variables and `/tmp` space) to cache static or rarely changing data across warm invocations.