VPC with connectors

You can enable your Cloud Run service, job, or worker pool to send traffic to a VPC network by configuring a Serverless VPC Access connector.

Before you begin

  • If you don't already have a VPC network in your project, create one.

  • If you use Shared VPC, see Connecting to a Shared VPC network.

  • In the Google Cloud console, ensure that the Serverless VPC Access API is enabled for your project.

    Enable API

  • Each Serverless VPC Access connector requires its own dedicated /28 subnet, which cannot be changed after creation. If you don't use Shared VPC, you can either create a subnet for the connector to use, or specify an unused custom IP range for the connector to create a subnet for its use. If you choose the custom IP range, the subnet that is created is hidden and cannot be used in firewall rules and NAT configurations.

  • Serverless VPC Access connectors scale up automatically to meet traffic demand, but don't scale down. Once scaled, resources and associated costs remain allocated.

Limitation

  • High packet rates can exhaust shared CPU credits and connection tracking memory on f1 and e2-micro instances, causing timeouts even when bandwidth limits aren't reached. We recommend e2-standard-4 instances for production environments that involve high concurrency or frequent small requests to ensure sufficient dedicated resources.

Create a connector

To send requests to your VPC network and receive the corresponding responses without using the public internet, you can use a Serverless VPC Access connector.

If your connector is located in the same project as its VPC network, you can either create a connector using an existing subnet or create a connector and a new subnet.

If your connector is located in a service project and uses a Shared VPC network, the connector and its associated VPC network are in different projects. When a connector and its VPC network are in different projects, a Shared VPC network administrator must create the connector's subnet in the Shared VPC network before you can create the connector, and you must create the connector using an existing subnet.

To learn more about subnet requirements, see connector subnet requirements.

To learn about connector throughput, including machine type and scaling, see Throughput and scaling.

You can create a connector by using the Google Cloud console, Google Cloud CLI, or Terraform.

Console

  1. Go to the Serverless VPC Access overview page.

    Go to Serverless VPC Access

  2. Click Create connector.

  3. In the Name field, enter a name for your connector, matching Compute Engine naming conventions, with the additional requirements that the name must be less than 21 characters long, and that hyphens (-) count as two characters.

  4. In the Region field, select a region for your connector. This must match the region of your serverless service.

  5. In the Network field, select the VPC network to attach your connector to.

  6. In the Subnet field, select one of the following options:

    • Create a connector using an existing subnet: Select the existing subnet in the Subnet field.

    • Create a connector and a new subnet: Select Custom IP range in the Subnet field. Then, enter the first address in an unused /28 CIDR (for example 10.8.0.0/28) to use as the primary IPv4 address range of a new subnet that Google Cloud creates in the connector's VPC network. Ensure that the IP range does not conflict with any existing routes in the connector's VPC network. The name of the new subnet begins with the "aet-" prefix.

  7. (Optional) To set scaling options for additional control over the connector, click Show Scaling Settings to display the scaling form.

    1. Set the minimum and maximum number of instances for your connector, or use the defaults, which are 2 (min) and 10 (max). The connector scales up to the maximum specified if traffic usage requires it. You must use values between 2 and 10.
    2. In the Instance Type menu, choose the machine type to be used for the connector, or use the default e2-micro. Notice the cost sidebar on the right when you choose the instance type, which displays bandwidth and cost estimations.
  8. Click Create.

  9. A green check mark will appear next to the connector's name when it is ready to use.

gcloud

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. Update gcloud components to the latest version:

    gcloud components update
  3. Ensure that the Serverless VPC Access API is enabled for your project:

    gcloud services enable vpcaccess.googleapis.com
  4. Create the connector using one of the following options:

    For more details and optional arguments, see the gcloud reference.

    • Create a connector using an existing subnet:

      gcloud compute networks vpc-access connectors create CONNECTOR_NAME \
       --region REGION \
       --subnet SUBNET_NAME \
       --subnet-project HOST_PROJECT_ID \
       --min-instances MIN \
       --max-instances MAX \
       --machine-type MACHINE_TYPE

      Replace the following:

      • CONNECTOR_NAME: a name for your connector, matching Compute Engine naming conventions, with the additional requirements that the name must be less than 21 characters long, and that hyphens (-) count as two characters.
      • REGION: a region for your connector, matching the region of your serverless service or job.
      • SUBNET_NAME: the name of the existing subnet.
      • HOST_PROJECT_ID: the Shared VPC host project ID. If the connector and existing subnet are located the same project, omit the --subnet-project flag.
      • MIN: the minimum number of instances to use for the connector. Use an integer between 2(the default) and 9.
      • MAX: the maximum number of instances to use for the connector. Use an integer between 3 and 10 (the default).
      • MACHINE_TYPE: must be one of the following: f1-micro, e2-micro, or e2-standard-4.
    • Create a connector and a new subnet:

      gcloud compute networks vpc-access connectors create CONNECTOR_NAME \
       --region REGION \
       --network VPC_NETWORK \
       --range IP_RANGE
       --min-instances MIN \
       --max-instances MAX \
       --machine-type MACHINE_TYPE

      Replace the following:

      • CONNECTOR_NAME: a name for your connector, matching Compute Engine naming conventions, with the additional requirements that the name must be less than 21 characters long, and that hyphens (-) count as two characters.
      • REGION: a region for your connector, matching the region of your serverless service or job.
      • VPC_NETWORK: the name of the VPC network to attach your connector to. The connector and VPC network must be located in the same project.
      • IP_RANGE: provide an unused /28 CIDR (for example 10.8.0.0/28) to use as the primary IPv4 address range of a new subnet that Google Cloud creates in the connector's VPC network. Ensure that the IP range does not conflict with any existing routes in the connector's VPC network. The name of the new subnet begins with the "aet-" prefix.
      • MIN: the minimum number of instances to use for the connector. Use an integer between 2(the default) and 9.
      • MAX: the maximum number of instances to use for the connector. Use an integer between 3 and 10 (the default). If the connector scales up to the maximum number of instances, it does not scale back down.
      • MACHINE_TYPE: must be one of the following: f1-micro, e2-micro, or e2-standard-4.
  5. Verify that your connector is in the READY state before using it:

    gcloud compute networks vpc-access connectors describe CONNECTOR_NAME \
    --region REGION

    Replace the following:

    • CONNECTOR_NAME: the name of your connector; this is the name that you specified in the previous step.
    • REGION: the region of your connector; this is the region that you specified in the previous step.

    The output should contain the line state: READY.

Terraform

You can use a Terraform resource to enable the vpcaccess.googleapis.com API.

resource "google_project_service" "vpcaccess-api" {
  project = var.project_id # Replace this with your project ID in quotes
  service = "vpcaccess.googleapis.com"
}

You can use Terraform modules to create a VPC network and subnet and then create the connector.

module "test-vpc-module" {
  source       = "terraform-google-modules/network/google"
  version      = "~> 16.0"
  project_id   = var.project_id # Replace this with your project ID in quotes
  network_name = "my-serverless-network"
  mtu          = 1460

  subnets = [
    {
      subnet_name   = "serverless-subnet"
      subnet_ip     = "10.10.10.0/28"
      subnet_region = "us-central1"
    }
  ]
}

module "serverless-connector" {
  source     = "terraform-google-modules/network/google//modules/vpc-serverless-connector-beta"
  version    = "~> 16.0"
  project_id = var.project_id
  vpc_connectors = [{
    name        = "central-serverless"
    region      = "us-central1"
    subnet_name = module.test-vpc-module.subnets["us-central1/serverless-subnet"].name
    # host_project_id = var.host_project_id # Specify a host_project_id for shared VPC
    machine_type  = "e2-standard-4"
    min_instances = 2
    max_instances = 7
    }
    # Uncomment to specify an ip_cidr_range
    #   , {
    #     name          = "central-serverless2"
    #     region        = "us-central1"
    #     network       = module.test-vpc-module.network_name
    #     ip_cidr_range = "10.10.11.0/28"
    #     subnet_name   = null
    #     machine_type  = "e2-standard-4"
    #     min_instances = 2
    #   max_instances = 7 }
  ]
  depends_on = [
    google_project_service.vpcaccess-api
  ]
}

Configure your service

You can configure your Cloud Run service to use a Serverless VPC Access connector for sending egress traffic. You can do this by using the Google Cloud console, Google Cloud CLI, or YAML file when you create a new service or deploy a new revision:

Console

  1. In the Google Cloud console, go to Cloud Run:

    Go to Cloud Run

  2. Select Services from the Cloud Run navigation menu, and click Deploy container to configure a new service. If you are configuring an existing service, click the service, then click Edit and deploy new revision.

  3. If you are configuring a new service, fill out the initial service settings page, then click Containers, Networking, Security to expand the service configuration page.

  4. Click the Networking tab.

    • In the Network field, choose one of the following options from the menu:

  • To disconnect your service from a VPC network, select None.
  • To use an existing connector, select it from the drop-down list or select Custom to use an existing connector that's not shown in the drop-down list.
  • To create a new connector, select Add new VPC connector. See Create a Serverless VPC Access connector for details on creating a connector.
  • For Traffic routing, select one of the following:
    • Route only requests to private IPs to the VPC to send only traffic to internal addresses through the VPC network.
    • Route all traffic to the VPC to send all outbound traffic through the VPC network.
  • Click Create or Deploy.