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?
- Configure a NAT Gateway in a public subnet, and update the private subnet's route table to route external traffic () through the NAT Gateway.Answer
- Initialize the SDK and HTTP clients outside the Lambda handler function so that they can be reused across multiple execution context invocations.Answer
- CAssociate a public IP address with the Lambda function's Elastic Network Interfaces (ENIs) and place the function in a public subnet.
- DPass static AWS credentials directly to the SDK client constructor inside the Lambda handler to authenticate requests to Amazon S3.
- ERe-initialize the SDK and HTTP clients inside the handler for every execution to ensure connection limits are not exceeded due to execution context reuse.
Answer
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.
Step-by-Step Solution
Key Concept
VPC internet connectivity for Lambda and execution context optimization