Question

Difficulty: MediumServerless Development with AWS Lambda

A developer is building a serverless order-processing application. An AWS Lambda function is triggered by an Amazon SQS queue. The function must connect to an Amazon RDS PostgreSQL database located in a private VPC subnet to retrieve customer data, and call a third-party payment gateway API over the internet to authorize transactions. The developer also wants to minimize connection latency to the database. Which combination of configurations must the developer implement to meet these requirements? (Select two.)

  1. Deploy the Lambda function in the private subnets of the VPC and configure a NAT Gateway in a public subnet with appropriate route tables to allow outbound internet traffic.Answer
  2. Instantiate the database client connection pool outside of the Lambda handler function.Answer
  3. C
    Deploy the Lambda function in the public subnets of the VPC to grant it direct access to the internet and the RDS database.
  4. D
    Re-establish a new database connection inside the Lambda handler function for every incoming event.
  5. E
    Hardcode the database credentials and payment gateway API key directly in the Lambda function's source code.

Answer

To meet the requirements, the developer must deploy the Lambda function in private subnets using a NAT Gateway in a public subnet for outbound internet connectivity, and instantiate the database connection client outside of the handler function to reuse connections.
Deploying the Lambda function in private subnets with a NAT Gateway in the public subnet allows both outbound internet access for API calls and secure local access to the private RDS database. Initializing the database connection pool outside the handler ensures that the connections are reused across events due to execution context recycling, optimizing latency.

Step-by-Step Solution

1
Configure the Lambda function VPC settings.
The Lambda function is placed in the private subnets of the VPC.
This allows the Lambda function to have network access to the RDS database residing in the private subnet.
2
Set up a NAT Gateway in the public subnet.
Outbound route tables in the private subnets point to the NAT Gateway.
This enables the Lambda function in the private subnet to securely reach the third-party payment gateway over the internet.
3
Declare and initialize the database connection client outside the event handler function.
The database connection pool persists in the global execution context across subsequent invocations.
This reduces connection setup time and latency by reusing existing connections instead of establishing a new one for every event.

Key Concept

AWS Lambda VPC networking and execution context reuse
Rate this question