A developer is writing an AWS Lambda function that retrieves data from a database. To optimize performance and reduce latency, the developer wants to reuse the database connection client across multiple function executions. In which part of the code should the database connection client be initialized to achieve this goal?
- Outside the main handler function in the global initialization codeCevap
- BInside the main handler function so that it is re-initialized on every invocation
- CInside the handler function by hardcoding the database access credentials directly in the SDK client setup
- DIn a private VPC subnet without a NAT Gateway to ensure secure caching of the connection pool
Cevap
The database connection client should be initialized outside the main handler function in the global initialization code.
Initializing the database connection client outside the main handler function in the global initialization code executes the initialization once during the initialization (INIT) phase. Warm invocations reuse the same execution environment, which caches the client and reduces connection latency and resource load.
Adım Adım Çözüm
Anahtar Kavram
Execution Context Reuse in AWS Lambda
Tahmini Süre:45s