Soru

Zorluk: KolayServerless Development with AWS Lambda

A developer is writing an AWS Lambda function that retrieves data from an Amazon RDS database. To optimize the function's performance and minimize connection overhead, the developer wants to reuse the database connection across multiple function invocations. How should the developer structure the function code to achieve this?

  1. A
    Create and close the database connection inside the Lambda handler function on every invocation.
  2. B
    Initialize the database connection inside the handler function using hardcoded access credentials.
  3. Initialize the database connection client outside of the Lambda handler function.Cevap
  4. D
    Return the database connection object in the response payload formatted for an API Gateway proxy integration.

Cevap

Initialize the database connection client outside of the Lambda handler function.
Initializing the database connection client outside of the Lambda handler function allows the connection to be kept alive in the execution context's memory. When subsequent invocations occur (warm starts), Lambda reuses the existing container and execution context, making the existing database connection immediately available and avoiding connection overhead.

Adım Adım Çözüm

1
Analyze the request to reuse database connections in an AWS Lambda function.
Identify that the database connection should persist across warm invocations.
Reusing connections avoids the overhead of establishing a new connection on every function invocation.
2
Evaluate where to declare the database connection client in the code.
Determine that variables declared outside the handler function (global scope) are retained when the execution context is reused.
AWS Lambda preserves the execution context (including global variables) for subsequent invocations until the container is destroyed.
3
Select the configuration that places the client initialization in the global scope.
Choose the option to initialize the database connection client outside of the Lambda handler function.
This is the AWS-recommended pattern for client reuse, database connection management, and latency reduction.

Anahtar Kavram

AWS Lambda Execution Context Reuse
Tahmini Süre:45s
Bu soruyu puanla