Question

Difficulty: EasyServerless Development with AWS Lambda

An application contains an AWS Lambda function that retrieves configuration files from Amazon S3 and writes audit logs to Amazon DynamoDB. The function's latency is higher than expected due to client initialization and S3 downloads occurring on every invocation. Which two actions will optimize the performance of this function by leveraging execution context reuse? (Select TWO.)

  1. Initialize the AWS SDK clients for S3 and DynamoDB outside of the Lambda handler function.Answer
  2. Download and store the configuration files in the local /tmp directory to reuse them across subsequent invocations.Answer
  3. C
    Instantiate the S3 and DynamoDB SDK clients inside the handler function to guarantee fresh connections for each invocation.
  4. D
    Hardcode the AWS access key and secret access key in the SDK client configuration to reduce credential retrieval time.
  5. E
    Configure the Lambda function to run inside a private VPC subnet without a NAT Gateway or VPC endpoints to ensure isolated database routing.

Answer

To optimize the Lambda function using execution context reuse, the developer should initialize the S3 and DynamoDB SDK clients outside of the handler function, and download and store the configuration files in the local /tmp directory.
Initializing AWS SDK clients outside the handler method enables the execution environment to reuse the connection pool across warm invocations. Additionally, caching files in the local /tmp directory allows subsequent invocations to read the configurations from local storage instead of performing network requests to Amazon S3.

Step-by-Step Solution

1
Identify performance bottlenecks related to execution context setup.
Recognize that SDK client instantiation and S3 downloads inside the handler execute on every invocation.
To optimize, initialization tasks should be moved to the global initialization phase (outside the handler).
2
Move SDK client initialization to the global scope.
SDK clients are initialized once during cold start and reused in subsequent warm invocations.
Reusing clients reduces latency by skipping initialization on warm starts.
3
Utilize the local execution environment storage.
Configuration files are cached in the /tmp directory and read locally if present.
Accessing /tmp is significantly faster than downloading the file from S3 on every invocation.

Key Concept

Execution context reuse and temporary storage cache optimization in AWS Lambda
Estimated Time:1m 0s
Rate this question