Sub topic 14
Connecting Functions to VPC Resources
This chapter assumes you know what a VPC, subnet, security group and route table are, and that you have a working function with an execution role. It covers how to let a Lambda function reach private resources, such as an RDS database, and what that does to internet access.
Why a function needs VPC access
By default, a function runs in a network that AWS manages. It can reach the public internet and public AWS service endpoints. It cannot reach resources that have only private IP addresses in your VPC, such as an RDS instance, an ElastiCache cluster or an internal load balancer.
To reach those, you attach the function to subnets in your VPC. Lambda then creates network interfaces for the function inside those subnets.
How the attachment works
When you configure VPC access, Lambda creates a Hyperplane elastic network interface (ENI) for each unique combination of subnet and security group in your configuration. Many execution environments share these ENIs. Lambda creates them when you create or update the function, not on each invocation. For that reason, the old advice that VPC attachment adds seconds to every cold start no longer applies. The setup cost is paid at deploy time.
Configuring it
Choose at least two subnets in different Availability Zones so the function survives the loss of one zone. Choose one or more security groups. The execution role also needs permission to manage ENIs. The managed policy AWSLambdaVPCAccessExecutionRole grants this.
In SAM, add VpcConfig and the managed policy:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Parameters:
PrivateSubnetA:
Type: AWS::EC2::Subnet::Id
PrivateSubnetB:
Type: AWS::EC2::Subnet::Id
FunctionSecurityGroup:
Type: AWS::EC2::SecurityGroup::Id
Resources:
DbFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
CodeUri: src/
Timeout: 10
Policies:
- AWSLambdaVPCAccessExecutionRole
VpcConfig:
SubnetIds:
- !Ref PrivateSubnetA
- !Ref PrivateSubnetB
SecurityGroupIds:
- !Ref FunctionSecurityGroupOn the database side, add an inbound rule to the database's security group that allows the database port from the function's security group. Referencing a security group, rather than an IP range, keeps the rule valid as ENIs change.
The internet access trap
Once attached to a VPC, a function loses its default internet access. A public IP address is never assigned to the function's ENI, even if you place it in a public subnet, so an internet gateway alone does not help. The usual symptom is a call to an AWS API or external service that hangs until the function timeout.
You have two ways to restore outbound access:
| Option | Reaches | Notes |
|---|---|---|
| NAT gateway in a public subnet, with private subnet routes pointing to it | Internet and all AWS services | Hourly and per-GB charges. Create one per zone for resilience. |
| VPC endpoint (gateway or interface) | Specific AWS services | Traffic stays on the AWS network. Gateway endpoints for S3 and DynamoDB have no hourly charge. |
If the function needs only AWS services, endpoints are usually cheaper and more secure than a NAT gateway.
Common errors
- Timeouts calling AWS or external APIs. The private subnet has no route to a NAT gateway or endpoint. Check the route table.
- Timeout connecting to the database. The database security group does not allow the function's security group on the database port.
- ENI permission errors on deploy. The execution role lacks the EC2 network interface permissions.
- Running out of IP addresses. Small subnets can be exhausted when concurrency is high. Use subnets with enough free addresses.
- Too many database connections. Many concurrent environments each open their own connection. Consider RDS Proxy to pool them.
Exercise
A function attached to a private subnet connects to RDS successfully, but its call to the S3 API hangs until the timeout. The route table has no NAT gateway route. Name two fixes and say which is cheaper if S3 is the only service the function calls.
Show answer
Add a NAT gateway in a public subnet and route the private subnet's 0.0.0.0/0 traffic to it, or create a VPC endpoint for S3. The S3 gateway endpoint is cheaper because it has no hourly or data processing charge, whereas a NAT gateway has both.