Question

Difficulty: EasyServerless Development with AWS Lambda

A developer is configuring an AWS Lambda function that reads data from an Amazon DynamoDB table. Currently, the function initializes the AWS SDK client inside the handler function on every invocation, and the DynamoDB table name is hardcoded in the function code. Which two actions should the developer take to improve performance and adhere to AWS development best practices? (Select two.)

  1. Initialize the AWS SDK client outside of the Lambda handler functionAnswer
  2. Store the DynamoDB table name as a Lambda environment variable and retrieve it in the codeAnswer
  3. C
    Increase the Lambda function timeout configuration to decrease the frequency of cold starts
  4. D
    Embed IAM access keys directly in the Lambda function's deployment package
  5. E
    Configure the Lambda function to run inside a private VPC subnet to reduce execution environment setup time

Answer

Initialize the AWS SDK client outside the handler function and store the DynamoDB table name in a Lambda environment variable.
Moving the client instantiation outside of the handler function takes advantage of execution context reuse, so subsequent requests do not pay the penalty of initializing the SDK. Storing configuration details in environment variables allows changes without modifying source code.

Step-by-Step Solution

1
Analyze the impact of client initialization placement
Moving the SDK initialization logic to the global scope (outside the handler) enables execution context reuse.
Code outside the handler runs during the initialization phase (cold start) and is kept in memory for subsequent warm starts, avoiding redundant connections.
2
Analyze code portability and decoupling options
Migrate hardcoded configuration strings to Lambda environment variables.
This isolates configuration changes from application logic, aligning with standard DevOps and security practices.

Key Concept

AWS Lambda execution context reuse and environment variables
Estimated Time:1m 0s
Rate this question