A developer is configuring an AWS Lambda function to process real-time financial transactions from an Amazon Kinesis Data Stream. The function must validate each transaction by invoking a third-party payment gateway's public API endpoint over the internet, and then save the transaction status to an Amazon RDS database located in a private subnet of a VPC.
The developer configures the Lambda function to run inside the same VPC and private subnet as the RDS database. During testing, the developer observes that the Lambda function is successfully triggered by the Kinesis stream but consistently times out after 15 seconds without executing the API call or database write.
What is the root cause of this issue and how should it be resolved?
- AThe execution role associated with the Lambda function is missing a trust policy allowing Kinesis to assume it. The developer must add the kinesis.amazonaws.com service principal to the trust relationship policy of the role.
- BThe Lambda function's timeout is set to a default value that is too low to process the batch size. The developer must increase the Lambda function execution timeout to 15 minutes to allow the function to complete the API calls and database writes.
- The Lambda function lacks internet access because it is deployed in a private VPC subnet without a route to a NAT Gateway. The developer must add a NAT Gateway in a public subnet and update the private subnet's route table to route external traffic to the NAT Gateway.Answer
- DThe producer application is using a partition key with low entropy to write records to Kinesis. The developer must change the partition key to a high-entropy value like a transaction ID to distribute the load and prevent Lambda execution bottlenecks.
Answer
The Lambda function lacks internet access because it is deployed in a private VPC subnet without a route to a NAT Gateway. The developer must add a NAT Gateway in a public subnet and update the private subnet's route table to route external traffic to the NAT Gateway.
When a Lambda function is configured to connect to a VPC, it is assigned an ENI to communicate with VPC resources like RDS. However, it loses its default public internet connectivity. To allow the function to call a third-party API over the internet, a NAT Gateway must be configured in a public subnet of the VPC, and the route table of the Lambda function's private subnet must have a route pointing 0.0.0.0/0 to the NAT Gateway.
Step-by-Step Solution
Key Concept
VPC Networking for AWS Lambda when consuming Kinesis Data Streams and accessing public APIs