Question

Difficulty: EasyServerless Development with AWS Lambda

A developer is writing an AWS Lambda function that must download a static reference file from Amazon S3 and reuse it across multiple invocations. The developer wants to minimize execution time and reduce the number of calls to Amazon S3.

Which two actions should the developer take to accomplish this?

  1. Declare the Amazon S3 client and configuration variables outside of the handler function.Answer
  2. Store the downloaded reference file in the local /tmp directory of the execution environment.Answer
  3. C
    Update the Lambda function's environment variables dynamically at runtime to store the file's content.
  4. D
    Embed the AWS access key and secret key directly in the Amazon S3 client initialization code to speed up authentication.
  5. E
    Deploy the Lambda function within a private VPC subnet with no NAT gateway to enable faster access to local storage.

Answer

To optimize execution time and reuse files across invocations, declare the client and variables outside of the handler function, and store the downloaded file in the local /tmp directory.
Declaring the Amazon S3 client outside of the handler ensures it is initialized once during cold start, and storing the downloaded file in the /tmp directory leverages ephemeral storage that persists across warm starts. Together, these steps significantly reduce execution time and avoid redundant calls to S3.

Step-by-Step Solution

1
Leverage execution context reuse.
Declaring initialization code and S3 clients outside the handler ensures they are executed once during the initialization phase (cold start), making them available for all warm starts.
This reduces the overhead of re-creating the client on every function execution.
2
Use ephemeral local storage for caching.
Download the static reference file to the /tmp directory, which provides writeable disk space that persists as long as the execution context is kept alive.
This allows subsequent invocations to read the file from local storage instead of making expensive network calls to S3.

Key Concept

AWS Lambda execution context lifecycle, initialization phase, and local ephemeral storage usage.
Rate this question