Connecting Lambda Functions to Private Resources in a VPC
This lesson explains how attaching a function to a VPC lets it reach private resources but changes how it reaches the internet and AWS services, a frequent exam scenario.
Learning outcomes
After completing this lesson, you will be able to:
- Explain what attaching a function to a VPC changes.
- Identify the paths a VPC-attached function needs to the internet and AWS services.
- Explain how RDS Proxy and concurrency limits protect databases from Lambda connection bursts.
Attaching a function to a VPC
By default, a Lambda function runs outside any customer VPC and cannot reach private resources. To reach Amazon RDS, ElastiCache or other resources in private subnets, the function is attached to private subnets in several Availability Zones, with a security group. Lambda creates shared Hyperplane network interfaces for these connections, so attaching a function to a VPC no longer causes long cold starts.
The target's security group must allow traffic from the function's security group. A function attached to a VPC also loses direct internet access, so it needs a NAT gateway or VPC endpoints to reach the internet or AWS services such as Amazon S3.
Reaching the internet and AWS services
A Lambda function is attached to a VPC when it must reach private resources, such as an RDS database. It then sends all its traffic through the VPC, so it loses the internet access it has outside a VPC, and each other destination needs a path. S3 and DynamoDB are reached through a gateway VPC endpoint, which is free. Other AWS services, such as Secrets Manager or SQS, are reached through interface VPC endpoints. The public internet, such as a third-party API, is reached through a NAT gateway in a public subnet, with a route from the function's private subnets. The following table summarises the paths.
| Destination | Path |
|---|---|
| S3 or DynamoDB | Gateway VPC endpoint (free) |
| Other AWS services | Interface VPC endpoints |
| Public internet | NAT gateway in a public subnet |
A VPC-attached function never receives a public IP address, so placing it in a public subnet does not give it internet access.
The following diagram shows the path to each kind of destination.

Paths from a VPC-attached function
Database connections
Each concurrent Lambda execution environment opens its own database connections, so a burst of invocations can open hundreds of connections at once and exhaust a relational database's connection limit, causing errors even though the database has spare CPU. RDS Proxy solves this by pooling and sharing connections, so many invocations use a small number of database connections. Reserved concurrency on the function also caps how many environments, and therefore connections, it can create.
Too many connections
Protecting a database from Lambda
A Lambda function triggered by an SQS queue writes orders to an RDS database. During a sale, Lambda scales to hundreds of concurrent executions and the database runs out of connections.
| Fix | Effect |
|---|---|
| Set maximum concurrency on the SQS event source mapping | Limits how many functions process the queue at once |
| Add RDS Proxy | Pools and reuses database connections |
| Keep messages in the queue during the peak | Orders are processed steadily rather than all at once |
The queue absorbs the spike, the concurrency limit protects the database, and RDS Proxy makes the connections that are opened efficient. This is the standard pattern for Lambda in front of a relational database.
A function in a private subnet must call a public API. What does it need?
Show answer
A route to a NAT gateway in a public subnet, because functions in a VPC do not receive public IP addresses.
Exam Tips
Remember for the exam
- A Lambda function in a VPC needs a NAT gateway or VPC endpoints to reach the internet and AWS APIs.
- When many Lambda invocations exhaust RDS connections, use RDS Proxy.
- For a VPC function that only needs S3 or DynamoDB, add a gateway endpoint rather than a NAT gateway.