Lesson 8 of 12 AWS Lambda

Connecting Functions to Private Resources in a VPC

This lesson explains how to give a function access to private resources such as databases. By default a function runs in an AWS-managed network with internet access but no access to your VPC; attaching it to a VPC reverses that, which is the source of many timeout questions.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain what changes when a function is attached to a VPC.
  • Choose between a NAT gateway and VPC endpoints for giving a VPC-attached function access to the internet and AWS services.
  • Explain why Amazon RDS Proxy is recommended when functions connect to a relational database.
  • Describe when Amazon EFS provides shared file storage for functions.

Running a function in a VPC

A function attached to a VPC can reach private resources such as RDS databases, ElastiCache clusters or internal services. When it is attached, you choose subnets and security groups, and Lambda creates Hyperplane elastic network interfaces in those subnets. These interfaces are shared by all execution environments that use the same subnets and security groups, and they are created when the function is configured rather than when it is invoked, so VPC attachment no longer adds long delays to cold starts.

Three settings make the attachment work. The subnets should be private subnets in several Availability Zones, so the function keeps running if one zone fails. The execution role needs the permissions in AWSLambdaVPCAccessExecutionRole, which let Lambda create and manage the network interfaces. And the security group of the resource being reached, such as the database, must allow inbound traffic from the function's security group on the required port. The following list summarises these points.

  • Use private subnets in several Availability Zones for resilience.
  • The execution role needs the AWSLambdaVPCAccessExecutionRole permissions to manage network interfaces.
  • Hyperplane interfaces are shared across environments, so VPC attachment no longer causes long cold starts.
  • The target resource's security group must allow the function's security group on the required port.

Reaching the internet and AWS services

When a function is attached to a VPC, Lambda creates elastic network interfaces in the chosen subnets, and the function's traffic leaves through them. These interfaces never receive public IP addresses, even in a public subnet, so the function loses the internet access it had outside the VPC. Calls to public AWS service endpoints, such as the S3 or Secrets Manager API, and to third-party APIs then time out unless the VPC provides another path.

For S3 and DynamoDB, a gateway VPC endpoint adds a route to the service in the subnets' route tables at no charge, and traffic stays on the AWS network. Most other AWS services, such as Secrets Manager, SQS and KMS, are reached through an interface VPC endpoint, which places a private IP address for the service in your subnets; with private DNS enabled, the SDK's normal endpoint name resolves to it, so no code change is needed. Interface endpoints are billed per hour and per gigabyte. Destinations on the public internet, such as a payment provider's API, need a NAT gateway in a public subnet and a default route to it from the function's private subnets. A NAT gateway can also reach AWS services, but endpoints keep the traffic private and often cost less.

The following table summarises the path for each destination.

DestinationPathCost
Amazon S3 or DynamoDBGateway VPC endpoint in the subnets' route tablesFree
Other AWS services, such as Secrets Manager, SQS or SESInterface VPC endpoint (AWS PrivateLink)Hourly and per GB
Public internet APIsNAT gateway in a public subnet, with a route from the private subnetsHourly and per GB

Example. After attaching a function to private subnets to reach RDS, its calls to the SES API time out. The fix is a NAT gateway route or an SES interface endpoint; raising the timeout only makes the failure slower.

Connecting to relational databases

Relational databases allow a limited number of open connections, and each connection uses memory on the database server. Lambda scales by adding execution environments, and each environment opens its own connection, so a burst of 500 concurrent invocations can try to open 500 connections and exhaust the database's limit, causing errors for every client.

Amazon RDS Proxy solves this by sitting between the functions and the database. Functions connect to the proxy, which keeps a smaller pool of database connections and shares them between invocations. The proxy also shortens failover, because it keeps client connections open while the database switches to a standby, and it can authenticate functions with IAM while it holds the database credentials from Secrets Manager. Two code and configuration practices help as well: opening the connection outside the handler lets warm invocations reuse it, and reserved concurrency caps how many connections one function can open. The following list summarises these measures.

  • RDS Proxy pools and shares connections between functions, shortens failover, and can authenticate with IAM and Secrets Manager.
  • Open connections outside the handler so warm invocations reuse them.
  • Reserved concurrency caps how many connections one function can open.

Shared file storage

A VPC-attached function can mount an Amazon EFS file system through an access point, for large or shared files such as machine learning models that exceed /tmp or must be shared between functions. The EFS security group must allow NFS (port 2049) from the function's security group.

Worked example

A function that lost its internet access

A function that calls DynamoDB and a payment provider's public API is attached to private subnets so that it can also query an RDS database. Afterwards, the database queries succeed but every other call times out.

CallWhy it failsFix
DynamoDBPrivate subnets have no route to the public DynamoDB endpointGateway endpoint for DynamoDB in the subnets' route tables (free)
Payment provider APINo route to the internetNAT gateway in a public subnet, with a default route from the private subnets
RDSWorks: the database security group allows the function's security groupNone needed

Moving the function into public subnets would not help, because Lambda network interfaces never receive public IP addresses. Raising the timeout would only make each failure take longer.

Check yourself

A function is moved into the private subnets of a VPC and can no longer call a public third-party API. What must the VPC provide?

Show answer

A route from the private subnets to a NAT gateway in a public subnet. Functions in a VPC have no public IP address, so a route to an internet gateway alone is not enough.

Exam Tips

Remember for the exam

  • A VPC function cannot reach an AWS API: add a NAT gateway or a VPC endpoint.
  • For S3 or DynamoDB from private subnets, a gateway endpoint is the cheapest fix.
  • Lambda functions in a VPC never receive public IP addresses, so public subnets do not help.
  • Too many database connections from Lambda: use RDS Proxy.
  • Allow access to a database by referencing the function's security group in the database's security group.