VPC Configuration and Network Isolation
What you'll learn: How to securely place serverless functions inside a VPC for private resource access, while understanding the performance and architectural trade-offs.
Why Put Functions in a VPC?
By default, serverless functions run in a cloud provider's managed environment with internet access but no access to your private resources (like databases in private subnets). When you configure a function to run inside your VPC, it gains direct network access to private resources—but loses direct internet access unless you configure a NAT gateway.
The VPC Configuration Model
Think of it like this: your function is normally in a hotel with free Wi-Fi (internet) but can't reach your office building (private VPC). When you place it in your VPC, it's now inside the office but behind a corporate firewall—it needs a NAT gateway to reach the outside world.
Configuration steps:
- Specify VPC ID, subnet IDs, and security group IDs in your function configuration
- The cloud provider creates elastic network interfaces (ENIs) in your subnets
- Functions use these ENIs to communicate with VPC resources
The Cold Start Penalty
Here's the catch: creating those ENIs takes time (5-10+ seconds on first invocation). This is the VPC cold start penalty. Subsequent invocations reuse ENIs, so they're fast—but infrequent functions pay this cost repeatedly.
Modern platforms (AWS Lambda with Hyperplane, for example) have reduced this significantly, but it's still a consideration.
NAT Gateway Implications
Functions in private subnets need a NAT gateway for outbound internet access (API calls, third-party services). This adds:
- Cost (hourly + per-GB charges)
- A potential single point of failure
- Architectural complexity
Key Takeaway: VPC configuration provides essential network isolation for accessing private resources, but introduces cold start delays and requires NAT gateways for internet access—balance security needs against performance and cost implications.