Provisioning XPN

This page describes how to provision shared VPC networks (XPN) for your projects. XPN allows you to share VPC networks across different projects that belong to the same Cloud Organization. You provision an XPN host project that owns the network you want to share, and then associate service projects that will use the network.

To learn more about XPN, read XPN Overview.

XPN Beta limitations

  • Quota of 100 XPN host projects per Cloud Organization.
  • Quota of 100 service projects attached to any given XPN host project.
  • External load balancing is not supported across projects. This means that the frontend of a load balancer must exist in the same project as the backends, but the backend instances in service projects can be created in the shared VPC network of the XPN host project.
  • GKE clusters in a service project associated with an shared VPC network are not supported.
  • GAE Flexible in a service project associated with an shared VPC network is not supported.
  • Deployment manager is limited to manage resources within a single project.

Permissions

To provision XPN, specific administrators must configure XPN at the respective levels:

  • An Organization admin is the ultimate administrator of the root of the hierarchy where projects live (the Cloud Organization node). An Organization admin nominates an XPN admin.
  • A XPN admin is responsible for choosing the project that will host the shared resources and the service projects in the Organization that can use the XPN host project.
  • A Service project admin is the administrator of a service project that uses the VPC shared network.
XPN provisioning steps (click to enlarge)
XPN provisioning steps (click to enlarge)

Nominating XPN admins for the Organization

The Organization admin must run these commands.

The Organization admin nominates one or more XPN admins for the Organization. The XPN admin is responsible of setting a project as the XPN host project that will host the shared VPC networking resources. More than one XPN host project can exist per Organization, but each service project can only be associated to a single XPN host project. The XPN admin role is granted by the Organization admin using a binding at the Organization level. You cannot bind the XPN admin role to a project.

Refer to the IAM documentation for more information. The Organization admin can grant XPN admin role at an Organization level to a user via the Google Cloud Platform Console.

You can also grant this permission using the gcloud command-line tool:

  1. Find your Organization ID.

    gcloud organizations list
    

  2. Apply XPN admin role to a member.

    gcloud organizations add-iam-policy-binding [ORG_ID] \
        --member 'user:[EMAIL_ADDRESS]' \
        --role "roles/compute.xpnAdmin"
    

Configuring and enabling an XPN host project

The XPN admin must run this command.

The nominated XPN admin either creates a new XPN host project or selects an already created project of which they are already an owner.

  1. Enable a project as the XPN host project that will share VPC networking resources. Note that the Compute Engine API must be enabled for the project.

    gcloud beta compute xpn enable [XPN_HOST_PROJECT_ID]
    

  2. Confirm that the project is enabled.

    gcloud beta compute xpn organizations list-host-projects [ORG_ID]
    

When a project is enabled as an XPN host project, this project is automatically marked as being important and not easily deleted. GCP automatically creates a "lien," or lock, on projects designated as XPN host projects. This prevents the project from being deleted while it is still an XPN host project. However, the host project owner can still delete the lien and delete the project.

If a project is disabled as an XPN host project, then GCP removes the lien automatically. The lien protects against deleting the host project while it is a host project.

We strongly recommend that the Organization admin sets up an Org policy such that removing the lien requires Org admin permissions.

Protecting an XPN Host project against deletion

The steps in this section must be completed by the Organization policy admin. These steps only need to be done once. The Organization policy applies to existing and future XPN host projects.

In order to safeguard against outages from accidental project deletion, a lien is automatically placed on any project that is enabled as an XPN host project. A lien is basically a lock that prevents project deletion unless a project owner first removes the lien. The lien is automatically removed when the project is deprovisioned as an XPN host project.

However, by default, this lien can be removed by the XPN host project owner unless an organization-level policy is created to prevent it. You should create such a policy to ensure an additional level of insurance against outage.

The following script sets an organization policy so that only Organization owners or users with the organization-level resourcemanager.lienModifier role can delete the XPN host project lien.

You must have the roles/orgpolicy.policyAdmin role for your Organization.

  1. Determine your Organization ID.

    gcloud organizations list
    

  2. Enforce the compute.restrictXpnProjectLienRemoval policy for your Organization.

    gcloud alpha resource-manager org-policies enable-enforce \
        --organization [ORG_ID] compute.restrictXpnProjectLienRemoval
    

Creating shared VPC networks and associating service projects

The XPN Admin can either create new VPC networking resources in the XPN host project or share VPC networking resources that already exist. VPC networking resources include the VPC networks/subnets to be shared and the associated routes, firewall rules, and VPNs. You can create VPC networking resources via existing VPC networking operations.

  1. Make project [XPN_SERVICE_PROJECT_ID] a service project of host project [XPN_HOST_PROJECT_ID]. Note that the Compute Engine API must be enabled for the projects.

    gcloud beta compute xpn associated-projects add [XPN_SERVICE_PROJECT_ID] \
    --host-project [XPN_HOST_PROJECT_ID]
    

  2. Confirm that the service project has been associated with the host project.

    gcloud beta compute xpn get-host-project [XPN_SERVICE_PROJECT_ID]
    

  3. (Optional) View the service projects associated with the host project.

    gcloud beta compute xpn list-associated-resources [XPN_HOST_PROJECT_ID]
    

Use IAM to give specific accounts permission to create instances and other resources in the shared VPC networks. There are two different ways of doing this, depending on the control required in the use of subnet:

Applying the compute.networkUser role at XPN host project level

This command provide users of the service project with permission to use all the existing and future subnets in a XPN host project. This is done by providing all the required users/principals with the NetworkUser role at XPN host project level.

gcloud projects add-iam-policy-binding [XPN_HOST_PROJECT_ID] \
    --member "group:email_group1@gmail.com" \
    --role "roles/compute.networkUser"

If there are service accounts in the service projects that require NetworkUser permission in XPN host project, for example, service accounts to create instances in a Managed Instance Group associated to a shared VPC network, you will need to repeat the command above for the service account.

For more information about how Managed Instance Group with service accounts work in XPN, please refer to Managed Instance Groups with service accounts in XPN.

Applying the compute.networkUser role at subnet level

Provide users with permission to use specific subnets in a XPN host project. This is done by providing all the required users/principals with NetworkUser role for specific subnets.

  1. Get the current JSON permissions file:

    gcloud beta compute networks subnets get-iam-policy [SUBNET_NAME] \
        --project [XPN_HOST_PROJECT_ID] \
        --format json
    

  2. Save it to a file called my-subnet-policy.json.

  3. Add the following binding:

    {
      "bindings": [
      {
         "members": [
               "group:email_group1@gmail.com"
            ],
            "role": "roles/compute.networkUser"
      }
      ],
      "etag": "[ETAG_STRING]"
    }
    

  4. Use the updated file to set the new policy.

    gcloud beta compute networks subnets set-iam-policy [SUBNET_NAME] my-subnet-policy.json \
        --project [XPN_HOST_PROJECT_ID]
    

Similarly, if there are service accounts in the service projects that require NetworkUser permission in specific subnets in the XPN host project, you add the service account using the above steps.

For more information about how Managed Instance Group with service accounts Managed Instance Groups with service accounts in XPN.

The recommendation is to provide NetworkUser role for a group, not for individual users, so that if a new user is added in the group, that user will automatically have the NetworkUser role. Similarly, if you want ANY user in your domain to be able to use shared VPC networks and automatically grant such permission to any new user within the domain, you can use member "Domain".

Discovering subnets that can be used. Creating resources.

These steps should be completed by a Service project admin. Service project admins can be project owners, editors, or users that have been granted the compute.instanceAdmin role.

  1. Discover which subnets you can use.

    gcloud alpha compute networks subnets list-usable --filter PROJECT~[XPN_HOST_PROJECT_ID]
    

  2. Creates instances or instances templates in the shared subnets.

    Existing commands can address resources in projects other than the current one via fully qualified resource URLs. The fully qualified resource URL includes the XPN host project of the VPC network or subnet resource.

Creating an instance in a shared subnet

This command creates an instance in a shared subnet.

gcloud compute instances create vm1 \
    --project [XPN_SERVICE_PROJECT_ID] \
    --subnet projects/[XPN_HOST_PROJECT_ID]/regions/[REGION]/subnetworks/[SUBNET] \
    --zone [ZONE]

Creating an instance template in the shared VPC network

This command creates the template for the whole shared VPC network, but can only be used in subnets where the user is authorized.

gcloud compute instance-templates create [NAME] \
    --project [XPN_SERVICE_PROJECT_ID] \
    --network projects/[XPN_HOST_PROJECT_ID]/global/networks/[NETWORK]

This command creates a template for instances within a specific shared subnet only.

gcloud compute instance-templates create [NAME] \
    --project [XPN_SERVICE_PROJECT_ID] \
    --subnet projects/[XPN_HOST_PROJECT_ID]/regions/us-central1/subnetworks/[SUBNET]

Creating an Internal load balancer forwarding rule

Creates a forwarding rule associated to a shared VPC network subnet for one subnet only. When you create the Internal load balancing forwarding rule, you must specify the shared subnet as a fully or partially qualified URL.

This command is only part of setting up an Internal load balancer. The rest of the instructions are the same the normal case for setting up an Internal load balancer.

gcloud compute forwarding-rules create [NAME] \
    --project [XPN_SERVICE_PROJECT_ID] \
    --load-balancing-scheme internal \
    --region \
    --ports PORT,[PORT,…] \
    --backend-service [BACKEND_SERVICE_NAME] \
    --subnet projects/[XPN_HOST_PROJECT_ID]/regions/[REGION]/subnetworks/[SUBNET] \
    [--address] \
    [--protocol]

Managed instance groups with service accounts in XPN

Managed instance groups use service accounts to perform actions like instance creation. See Managed Instance Groups and IAM for details.

The XPN Admin must grant the service account [XPN_SERVICE_PROJECT_NUMBER]@cloudservices.gserviceaccount.com the compute.networkUser role for the XPN host project or for specific XPN subnets. This is done by setting an IAM policy at the XPN project level that binds the service account with the with compute.networkUser role.

  1. Get the host project number.

    gcloud projects describe [XPN_SERVICE_PROJECT_ID]
    

  2. Grant the service account the compute.networkUser role.

    gcloud projects add-iam-policy-binding [XPN_HOST_PROJECT_ID] \
        --member "serviceAccount:[XPN_SERVICE_PROJECT_NUMBER]@cloudservices.gserviceaccount.com" \
        --role "roles/compute.networkUser"
    

More generally, any service accounts that are used in operations that require network use will need to be given compute.networkUser role in the XPN host project. Any and all of the IAM roles can be given to the service account.

gcloud projects add-iam-policy-binding [XPN_HOST_PROJECT_ID] \
    --member "user:[USER_ID]@[XPN_SERVICE_PROJECT_ID].iam.gserviceaccount.com" \
    --role "roles/compute.networkUser"

For information on how to apply IAM policies to service accounts, see granting roles to service accounts.

What's next

Send feedback about...

Compute Engine Documentation