Question

Difficulty: MediumServerless Development with AWS Lambda

A developer is implementing an AWS Lambda function that retrieves user profile records from a MongoDB database hosted on an Amazon EC2 instance in a private VPC subnet. After processing the records, the Lambda function publishes notifications to an Amazon SNS topic. The Lambda function is configured to access resources inside the same private VPC subnet.

During integration testing, the developer identifies two issues:
1. The Lambda function times out with network errors when attempting to publish messages to the public Amazon SNS endpoint.
2. The function experiences significant latency because it initiates a new database connection for every incoming request.

Which two configuration changes or development practices should the developer implement to resolve these issues? (Select two.)

  1. Initialize the MongoDB client and connection pool outside of the Lambda handler function.Answer
  2. Create an interface VPC endpoint for Amazon SNS and associate it with the VPC subnets used by the Lambda function.Answer
  3. C
    Deploy the Lambda function in a public subnet and enable public IP assignment in the Lambda configuration.
  4. D
    Increase the Lambda function's execution timeout limit to allow the connection pool to reset after each invocation.
  5. E
    Update the Lambda function's IAM execution role trust policy to allow the MongoDB EC2 instance's IAM role to assume it.

Answer

Initialize the MongoDB client and connection pool outside of the Lambda handler function, and create an interface VPC endpoint for Amazon SNS and associate it with the VPC subnets used by the Lambda function.
Reusing database connections via execution context reuse (by declaring the client outside the handler) and creating an interface VPC endpoint for Amazon SNS are AWS-recommended best practices for Lambda functions that need secure database access and public service access within a VPC.

Step-by-Step Solution

1
Address the connection latency issue by reusing the database connection client across invocations.
Declaring and initializing the MongoDB client outside of the Lambda handler function ensures that the connection pool remains active in the execution context and is reused for subsequent warm invocations.
This avoids the latency overhead of performing a database connection handshake on every request.
2
Address the public internet access issue for publishing to Amazon SNS from a private subnet.
Creating an interface VPC endpoint for Amazon SNS in the VPC provides a private route using AWS PrivateLink to reach the SNS API directly from the private subnet.
Lambda functions in private subnets cannot reach public AWS service endpoints without a NAT Gateway or a VPC endpoint.

Key Concept

AWS Lambda VPC networking and execution context reuse best practices.
Estimated Time:2m 0s
Rate this question