Soru

Zorluk: ZorServerless Development with AWS Lambda

A financial transaction processor uses an AWS Lambda function to validate account balances against an Amazon DynamoDB table. The Lambda function is configured to run inside a private subnet of a VPC to comply with security standards. During peak load, the system experiences a high rate of database connection timeouts and latency spikes. An analysis reveals that the Lambda function is establishing a new connection to DynamoDB during every invocation, and the outbound traffic to DynamoDB is routed via a NAT Gateway, leading to high data processing costs.

Which combination of actions will resolve the latency issues and reduce data transfer costs?

  1. A
    Declare and initialize the DynamoDB client inside the Lambda handler function, and create an interface VPC endpoint for DynamoDB to bypass the NAT Gateway.
  2. B
    Declare and initialize the DynamoDB client outside the Lambda handler function, hardcode static credentials with minimum permissions in the client initialization, and configure a public NAT Instance.
  3. C
    Instantiate the DynamoDB client inside the Lambda handler function, attach an Internet Gateway directly to the private subnet, and configure a security group to allow inbound traffic from DynamoDB.
  4. Declare and initialize the DynamoDB client outside the Lambda handler function, and create a gateway VPC endpoint for DynamoDB with a route table entry pointing to it from the private subnet.Cevap

Cevap

Declare and initialize the DynamoDB client outside the Lambda handler function, and create a gateway VPC endpoint for DynamoDB with a route table entry pointing to it from the private subnet.
The correct solution involves defining and instantiating the DynamoDB client outside the handler function. AWS Lambda reuses the execution context for subsequent invocations, allowing global variables and connections to persist. This reduces the latency of establishing new TCP connections. Additionally, routing traffic to DynamoDB via a gateway VPC endpoint eliminates NAT Gateway transit costs and reduces network latency by keeping traffic within the AWS network.

Adım Adım Çözüm

1
Move the initialization of the DynamoDB client out of the handler function and place it in the global scope (outside the handler).
The DynamoDB client is initialized once during the Lambda container initialization (cold start) and is reused across subsequent invocations within the same execution context.
Reusing the client enables TCP connection pooling and avoids the overhead of establishing a new connection on every single invocation.
2
Create a gateway VPC endpoint for DynamoDB in the VPC where the Lambda function resides.
The VPC endpoint provides private connectivity to DynamoDB within the AWS network.
This routing mechanism keeps the traffic internal to the AWS network, eliminating the need to send DynamoDB traffic through the NAT Gateway.
3
Update the route table associated with the private subnet to include a route targeting the DynamoDB gateway VPC endpoint.
Traffic directed to DynamoDB from resources in the private subnet is automatically routed through the gateway endpoint.
This removes NAT Gateway data processing charges and reduces connection latency.

Anahtar Kavram

Optimization of AWS Lambda execution context reuse for database connections, and the implementation of gateway VPC endpoints for cost-effective, secure private routing to AWS services.
Tahmini Süre:2m 0s
Bu soruyu puanla