A developer has configured an AWS Lambda function to run inside a private subnet of a custom VPC to query an Amazon RDS PostgreSQL database. The function must also retrieve API keys from AWS Secrets Manager and call an external third-party payment gateway over the internet. During testing, the function consistently times out when attempting to call the Secrets Manager service and the payment gateway. Which combination of network and code configurations will resolve these timeouts while maintaining access to the RDS database?
- Configure a NAT Gateway in a public subnet of the VPC, update the private subnet's route table to route non-VPC traffic () to the NAT Gateway, and retrieve the API keys dynamically in the function handler using the AWS SDK.Cevap
- BAssociate the Lambda function with a public subnet of the VPC, enable the auto-assign public IP setting on that subnet, and retrieve the API keys dynamically in the function handler using the AWS SDK.
- CMove the Lambda function out of the custom VPC to restore default internet access, hardcode the RDS database credentials and the third-party API keys as plaintext variables in the SDK client initialization, and execute the queries directly.
- DIncrease the Lambda function's timeout configuration to 15 minutes to allow for transient network delays, and use the global execution context to cache the database connection and the API keys across invocations.
Cevap
Configure a NAT Gateway in a public subnet of the VPC, update the private subnet's route table to route non-VPC traffic () to the NAT Gateway, and retrieve the API keys dynamically in the function handler using the AWS SDK.
To allow a Lambda function inside a private subnet of a VPC to access both internet-facing resources (the external payment gateway) and public AWS service endpoints (like AWS Secrets Manager), you must route outbound internet traffic through a NAT Gateway. The NAT Gateway must be placed in a public subnet that has a route to an Internet Gateway. The private subnet's route table is then updated to forward all outbound traffic () to the NAT Gateway. This configuration preserves the function's ability to communicate with the RDS database in the private subnet while resolving the connectivity timeouts to external services.
Adım Adım Çözüm
Anahtar Kavram
VPC Networking and Outbound Routing for AWS Lambda Functions