Soru

Zorluk: OrtaServerless Development with AWS Lambda

A developer is building a serverless backend using AWS Lambda that queries an Amazon RDS database. To optimize performance and reduce database connection overhead, the developer wants to implement execution context reuse. The Lambda function also needs to retrieve database credentials securely from AWS Systems Manager Parameter Store.

Which two actions should the developer take to meet these performance and security requirements? (Select two.)

  1. Initialize the database connection pool outside of the Lambda handler function to reuse connections across subsequent invocations.Cevap
  2. Retrieve the database credentials from Parameter Store outside of the Lambda handler function, caching the values and implementing a refresh mechanism.Cevap
  3. C
    Retrieve the database credentials inside the handler function on every invocation to ensure the execution context remains completely stateless.
  4. D
    Declare the database credentials as hardcoded string constants inside the Lambda function code to eliminate Parameter Store latency.
  5. E
    Deploy the Lambda function within a private VPC subnet without a NAT Gateway or VPC endpoint to establish a secure database channel.

Cevap

The developer should initialize the database connection pool outside the Lambda handler function to leverage execution context reuse, and fetch the database credentials from Parameter Store outside the handler, caching them with a mechanism to refresh the credentials before expiration.
To optimize performance, initialization logic such as database connection pools and credential retrieval should be executed outside the handler function. During subsequent warm invocations, the Lambda service reuses the same execution environment, allowing global variables and open network connections to remain active. Retrieving and caching credentials outside the handler reduces API overhead, while introducing a refresh mechanism ensures rotated credentials are eventually updated without manual restarts.

Adım Adım Çözüm

1
Move resource-heavy initialization logic (such as database connection pools) outside the Lambda handler function.
Database connections are created once during the environment initialization and reused across subsequent warm invocations, reducing connection setup latency.
Execution environments are kept active by Lambda for subsequent requests. Anything declared in global scope remains warm.
2
Retrieve database credentials from Systems Manager Parameter Store outside the handler function.
Credentials are fetched once when the container initializes instead of on every invocation.
This reduces Parameter Store API request volume and lowers execution latency for subsequent invocations.
3
Implement a local cache with a Time-To-Live (TTL) or refresh check for the retrieved credentials.
The Lambda function can adapt to rotated credentials without requiring a cold start.
If secrets are cached indefinitely, credential rotation in the backend would lead to database connection failures until the container is recycled.

Anahtar Kavram

Optimizing AWS Lambda performance and security via execution context reuse and static initialization.
Bu soruyu puanla