Soru

Zorluk: OrtaServerless Development with AWS Lambda

A developer has deployed an AWS Lambda function inside a private subnet of a VPC to process data and write it to an Amazon RDS database. The function also needs to call a third-party payment processing API over HTTPS and upload a summary report to Amazon S3. During testing, the developer observes that the Lambda function can write to the RDS database, but attempts to connect to the third-party API and Amazon S3 fail with network timeouts. Additionally, the Lambda function's execution duration is high due to establishing new HTTPS connections on every execution.

Which two actions should the developer take to resolve these network timeouts and optimize connection performance?

  1. Configure a NAT Gateway in a public subnet, and update the private subnet's route table to route external traffic (0.0.0.0/00.0.0.0/0) through the NAT Gateway.Cevap
  2. Initialize the SDK and HTTP clients outside the Lambda handler function so that they can be reused across multiple execution context invocations.Cevap
  3. C
    Associate a public IP address with the Lambda function's Elastic Network Interfaces (ENIs) and place the function in a public subnet.
  4. D
    Pass static AWS credentials directly to the SDK client constructor inside the Lambda handler to authenticate requests to Amazon S3.
  5. E
    Re-initialize the SDK and HTTP clients inside the handler for every execution to ensure connection limits are not exceeded due to execution context reuse.

Cevap

Configure a NAT Gateway in a public subnet, route outbound traffic to it, and initialize SDK/HTTP clients outside the Lambda handler function.
Configuring a NAT Gateway in a public subnet and routing external traffic from the private subnet through it allows the Lambda function to access external networks like the third-party API and AWS services. Initializing the SDK and HTTP clients outside the handler allows the Lambda function to reuse these clients and connection pools across subsequent invocations within the same execution context, reducing overhead and improving latency.

Adım Adım Çözüm

1
Analyze the network failure context
The Lambda function is placed inside a private subnet of a VPC. It can reach the RDS database in the same VPC but cannot resolve public API DNS or reach external services.
Resources in private subnets do not have public IP addresses or routes to the internet, blocking HTTPS calls to external APIs and AWS endpoints.
2
Address the outbound internet connectivity issue
Create a NAT Gateway in a public subnet and add a route (0.0.0.0/00.0.0.0/0) in the private subnet's route table pointing to the NAT Gateway.
This enables secure outbound internet translation for resources within private subnets.
3
Optimize connection overhead
Declare the SDK and HTTP clients outside the handler code block.
Lambda reuses the execution context for subsequent warm starts. Code outside the handler is executed once during initialization (cold start), allowing subsequent invocations to reuse established connection pools.

Anahtar Kavram

VPC internet connectivity for Lambda and execution context optimization
Bu soruyu puanla