Serverless VPC Access
Serverless VPC Access connects a Cloud Run function directly to your VPC network, so it can reach Compute Engine VMs, Memorystore, and any other resource at its internal IP address. Requests and responses use internal DNS and internal IPs, so the traffic is never exposed to the internet. This page covers configuring a connector for a function, restricting what the connector can reach with firewall rules, and connecting to a Shared VPC.
The connector fundamentals (what a connector is, the /28 requirement, region matching) are shared with Cloud Run services - see Cloud Run access & networking. This page focuses on the function-specific pieces.
By default a Cloud Run function can call public external IPs and URLs, but it cannot reach a resource on its internal VPC IP. A request to an internal address just hangs and times out (the function returns an upstream request timeout). Attach a Serverless VPC Access connector and route egress through it, and the same internal IP becomes reachable. So a function that returns a VM's page instantly from the VM's external IP will time out on its internal IP - until it is redeployed with --vpc-connector.
Configuring a connector
- Enable the Serverless VPC Access API.
- Create a connector in your Google Cloud project.
- Attach the connector to a VPC network and region. The connector's region must match the region where your functions are deployed.
A Serverless VPC Access connector is the resource that handles traffic between the serverless functions environment and your VPC network. Configure it with an unused /28 subnet or a non-overlapping /28 CIDR range, used exclusively by the connector and no other resources. Once created, you use it by deploying each function with the connector name.
Restricting connector access
Restrict what a connector can reach in your VPC with firewall rules - either at the destination resource (ingress) or at the connector itself (egress).
Using ingress rules
- Connecting to a standalone VPC - or a Shared VPC with the connector in the host project - automatically creates an implicit firewall rule at priority 1000 that allows ingress from the connector's subnet/CIDR range to all resources in the VPC.
- Override the default: create an ingress rule with priority lower than 1000 to deny ingress from the connector network tag or CIDR range.
- Re-allow to a target: create another ingress rule with an even lower priority number, targeting the specific resource you want the connector to reach.
Firewall priority is inverted: the smaller the number, the higher the priority. 990 overrides the implicit 1000 allow, and 980 overrides the 990 deny. Get the ordering backwards and your override never takes effect.
Using egress rules
- Restrict a connector's outgoing traffic with egress rules on the connector in the VPC.
- Deny first: create an egress rule targeting the connector tag to stop it from sending outgoing traffic.
- Then allow to a destination: create another egress rule with a lower priority number, setting the destination range to the CIDR range of the resource you want the connector to reach.
Connecting to a Shared VPC network
Cloud Run functions can connect directly to a Shared VPC network with Serverless VPC Access, reaching Compute Engine VMs and any other internal-IP resource in the shared network. A connector can live in one of two places:
- One connector per service project - in each service project whose functions need access to the network.
- One shared connector in the host project - reused across service projects.
- Enable the Serverless VPC Access API.
- Create a connector in each service project or in the host project.
- Configure the connector and its subnet.
- Perform additional configuration depending on whether the connector is in the host or a service project (below).
- Configure your functions to use the connector to send traffic to the VPC network.
Connector in the host project
- Enable the Cloud Functions API for the service project - required to add IAM roles and for the service project to use Cloud Run functions.
- Grant
Serverless VPC Access Userto the service project's Cloud Functions Service Agent on the host project - provides access to the connector. - Make it discoverable: grant
Serverless VPC Access Vieweron the host project andCompute Network Vieweron the service project.
Connector in a service project
- Create firewall rules to allow requests from NAT and health-check IP ranges to reach and be reached by the connector - these ranges are used by the Google infrastructure underlying Cloud Run functions.
- Optionally limit the firewall rule scope to specific target resources in the network.
- Grant
Compute Network Userin the host project to the service project'scloudservicesandvpcaccessservice accounts - required for each service project that uses the connector.