Soru

Zorluk: OrtaServerless Development with AWS Lambda

A developer is creating an AWS Lambda function that fetches metadata from an external third-party API and saves the results to an Amazon DynamoDB table. The external API requires an API key for authentication. The developer needs to optimize the function's performance by minimizing connection latency and ensuring the API key is secured according to AWS best practices.

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

  1. Initialize the DynamoDB client and the HTTP client outside of the Lambda handler method.Cevap
  2. Store the API key in AWS Secrets Manager, retrieve it using the AWS SDK inside the Lambda function, and cache the retrieved key in a global variable outside the handler.Cevap
  3. C
    Hardcode the API key in the Lambda function's source code and initialize the AWS SDK client inside the handler method.
  4. D
    Re-create the DynamoDB client and the HTTP connection inside the handler method for every invocation to ensure clean execution states.
  5. E
    Deploy the Lambda function in a private VPC subnet without configuring a NAT Gateway to isolate the traffic to the external API.

Cevap

Initialize the DynamoDB client and the HTTP client outside of the Lambda handler method, and store the API key in AWS Secrets Manager, retrieving and caching it in a global variable outside the handler.
Initializing database clients and HTTP clients outside the handler allows AWS Lambda to reuse these connections across warm invocations, significantly optimizing execution latency. Additionally, retrieving sensitive keys from AWS Secrets Manager programmatically and caching them in global variables ensures credentials are kept secure while preventing API call overhead on subsequent executions.

Adım Adım Çözüm

1
Analyze performance optimization for database and external connections in Lambda.
Determine that SDK and HTTP clients should be initialized outside of the handler function.
This allows the function to reuse the execution context, including established TCP connections, across warm invocations, reducing latency.
2
Evaluate secure credential management options for the external API key.
Identify AWS Secrets Manager as the secure repository for the API key instead of hardcoding it in the source code.
Hardcoding credentials exposes secrets in source code repositories and makes key rotation difficult, violating AWS security best practices.
3
Optimize secret retrieval latency within the Lambda execution cycle.
Implement code to retrieve the secret and cache it in a global variable declared outside the handler.
Caching the secret ensures the Lambda function only calls the Secrets Manager service during cold starts, reducing latency and cost for subsequent warm starts.

Anahtar Kavram

AWS Lambda execution context reuse and secure credential management using AWS Secrets Manager.
Bu soruyu puanla