Overview and What You Will Learn
Shared VPC lets one Google Cloud project (the host project) own a VPC network, while other projects (service projects) create resources such as VMs inside that network's subnets. A central network team controls IP ranges, firewall rules and routes, and product teams keep control of their own project's resources and billing. It requires a Google Cloud organization, because the host and service projects must belong to the same one.
In this lab you will:
- Create a host project and a service project and enable Shared VPC
- Build a custom-mode VPC with one subnet and a firewall rule in the host project
- Attach the service project and grant access to a single subnet instead of the whole network
- Launch a VM in the service project that uses the host's subnet, then verify and clean up
Why This Matters in Production
Without Shared VPC, every product team creates its own VPC, and the company ends up with overlapping IP ranges, inconsistent firewall rules and a pile of VPC peerings to keep connected. A platform team at a company like Spotify or Grab wants one place to plan IP space and enforce network security, without making every VM request go through a ticket. Shared VPC separates the two concerns cleanly: the network team administers the network, and product teams only deploy into the subnets they were granted.
Core Principles
In a Shared VPC, network administration lives in the host project and resource administration lives in each service project. Resources in a service project get their internal IP addresses from the host's subnets, and traffic between them stays on internal addresses.
and routes] end subgraph SA[Service project A] A([Checkout VMs]) end subgraph SB[Service project B] B([Payments VMs]) end A -->|uses subnet| N B -->|uses subnet| N F --- N classDef net fill:#326CE5,color:#FFFFFF classDef app fill:#D1FAE5,color:#1E293B class N,F net class A,B app
Three roles matter, and they live at different levels:
| Role | Where it is granted |
|---|---|
Shared VPC Admin (roles/compute.xpnAdmin) |
Organization or folder, to enable hosts and attach projects |
Network User (roles/compute.networkUser) |
A subnet (preferred) or the host project, for people who deploy into the network |
| Compute Instance Admin | The service project, for people who create VMs |
Two rules to remember: a project can be attached to only one host project, and a host project cannot itself be a service project.
Detailed Step-by-Step Practical Lab
You need a Google Cloud organization, a billing account, and a user with Project Creator, Billing Account User and Shared VPC Admin permissions. Replace the placeholder IDs below with your own.
Milestone 1: Create the Projects
Project IDs are globally unique, so add a random suffix. Compute Engine needs billing linked before you can enable it.
export ORG_ID=123456789012export BILLING_ACCOUNT=0A1B2C-3D4E5F-6A7B8Cexport SUFFIX=$(date +%s | tail -c 6)export HOST_PROJECT=net-host-$SUFFIXexport SERVICE_PROJECT=team-checkout-$SUFFIX gcloud projects create $HOST_PROJECT --name="Network Host" --organization=$ORG_IDgcloud projects create $SERVICE_PROJECT --name="Checkout Team" --organization=$ORG_ID gcloud billing projects link $HOST_PROJECT --billing-account=$BILLING_ACCOUNTgcloud billing projects link $SERVICE_PROJECT --billing-account=$BILLING_ACCOUNT gcloud services enable compute.googleapis.com --project=$HOST_PROJECTgcloud services enable compute.googleapis.com --project=$SERVICE_PROJECTMilestone 2: Enable the Host Project
Enabling Shared VPC turns the project into a host that can share its networks. This step needs the Shared VPC Admin role.
gcloud compute shared-vpc enable $HOST_PROJECTMilestone 3: Build the Network in the Host Project
Use custom subnet mode so you choose every range deliberately instead of accepting auto-created subnets in every region.
gcloud compute networks create vpc-shared \ --project=$HOST_PROJECT \ --subnet-mode=custom gcloud compute networks subnets create subnet-checkout-mumbai \ --project=$HOST_PROJECT \ --network=vpc-shared \ --region=asia-south1 \ --range=10.0.1.0/24 gcloud compute firewall-rules create allow-iap-ssh \ --project=$HOST_PROJECT \ --network=vpc-shared \ --direction=INGRESS \ --allow=tcp:22 \ --source-ranges=35.235.240.0/20Firewall rules belong to the host project, so service project teams cannot weaken them. A team that needs a new rule asks the network team, or is granted a narrowly scoped role by them.
Milestone 4: Attach the Service Project
Attaching a service project makes the host's shared subnets available to it, subject to the IAM grants you add next.
gcloud compute shared-vpc associated-projects add $SERVICE_PROJECT \ --host-project=$HOST_PROJECT # Confirm the attachmentgcloud compute shared-vpc associated-projects list $HOST_PROJECTMilestone 5: Grant Access to One Subnet
Grant roles/compute.networkUser on the specific subnet, not on the whole host project. Subnet-level grants are the simplest way to keep each team inside its own address range.
gcloud compute networks subnets add-iam-policy-binding subnet-checkout-mumbai \ --project=$HOST_PROJECT \ --region=asia-south1 \ --member="user:rahul@devops.in" \ --role="roles/compute.networkUser" # The same person also needs to create VMs in the service projectgcloud projects add-iam-policy-binding $SERVICE_PROJECT \ --member="user:rahul@devops.in" \ --role="roles/compute.instanceAdmin.v1"In real environments, grant roles to groups rather than individuals. Also grant roles/compute.networkUser on the subnet to any service account that will create resources there, including the Compute Engine default service account for autoscaled instance groups and the service accounts used by GKE in the service project. Missing this grant is a very common cause of "permission denied on subnetwork" errors.
SecurityGranting Network User on the entire host project lets a team place resources in every shared subnet. Scope it to a subnet so a compromised or careless team cannot land workloads in another team's network segment.
Milestone 6: Create a VM Using the Shared Subnet
Create the VM in the service project, but point it at the subnet owned by the host project. The full resource path is required because the subnet is in a different project.
gcloud compute instances create vm-checkout-service \ --project=$SERVICE_PROJECT \ --zone=asia-south1-a \ --machine-type=e2-medium \ --image-family=debian-12 \ --image-project=debian-cloud \ --subnet=projects/$HOST_PROJECT/regions/asia-south1/subnetworks/subnet-checkout-mumbai \ --no-address--no-address gives the VM no external IP, which is the right default for a shared network. Use Cloud NAT in the host project if it needs outbound internet, and IAP to connect.
Milestone 7: Verify the Result
Confirm that the VM lives in the service project but uses the host's network, and see what the service project can use.
# The VM's subnet should point at the host projectgcloud compute instances describe vm-checkout-service \ --project=$SERVICE_PROJECT \ --zone=asia-south1-a \ --format="value(networkInterfaces[0].subnetwork)" # Which host does the service project belong to?gcloud compute shared-vpc get-host-project $SERVICE_PROJECT # Subnets this caller is allowed to use from the service projectgcloud compute networks subnets list-usable --project=$SERVICE_PROJECT # Connect through IAP to prove the firewall rule worksgcloud compute ssh vm-checkout-service \ --project=$SERVICE_PROJECT \ --zone=asia-south1-a \ --tunnel-through-iapMilestone 8: Clean Up
Remove resources in reverse order. The host cannot be disabled until every service project is detached.
gcloud compute instances delete vm-checkout-service \ --project=$SERVICE_PROJECT --zone=asia-south1-a --quiet gcloud compute shared-vpc associated-projects remove $SERVICE_PROJECT \ --host-project=$HOST_PROJECT gcloud compute firewall-rules delete allow-iap-ssh --project=$HOST_PROJECT --quietgcloud compute networks subnets delete subnet-checkout-mumbai \ --project=$HOST_PROJECT --region=asia-south1 --quietgcloud compute networks delete vpc-shared --project=$HOST_PROJECT --quiet gcloud compute shared-vpc disable $HOST_PROJECT gcloud projects delete $SERVICE_PROJECT --quietgcloud projects delete $HOST_PROJECT --quietProduction Best Practices & Common Pitfalls
- Keep the host project network-only. Do not run workloads in it, so its permissions stay tight and its blast radius small.
- Grant Network User per subnet, to groups. Individuals and project-wide grants become unmanageable.
- Plan IP space first. Subnet ranges cannot overlap with each other or with on-premises networks you will connect later.
- Use one host per environment. Separate hosts for production and non-production keep firewall changes in dev away from prod.
- Centralize egress. Put Cloud NAT and firewall policy in the host project.
- Use an organization policy. Constrain which projects can be attached and which can be hosts.
- Remember the one-host rule. A service project belongs to one host, so choose the structure before teams build on it.
Common MistakeGiving every team Network User on the whole host project "to avoid permission errors". It works immediately and quietly removes the network segmentation that was the reason for adopting Shared VPC.
TipIf a managed service fails to create resources in the shared subnet, check which service account it uses. Many Google-managed services need Network User granted to their own service agent in the host project.
RememberNetwork permissions and resource permissions are separate. Admin rights in a service project do not let anyone change the VPC, and rights in the host do not let anyone create VMs in a service project.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud compute shared-vpc enable HOST |
Make a project a Shared VPC host |
gcloud compute shared-vpc associated-projects add SVC --host-project=HOST |
Attach a service project |
gcloud compute shared-vpc associated-projects list HOST |
List attached service projects |
gcloud compute shared-vpc get-host-project SVC |
Show a service project's host |
gcloud compute networks subnets list-usable --project=SVC |
List subnets the caller can use |
gcloud compute shared-vpc disable HOST |
Remove host status (after detaching all) |
| Symptom | Likely cause and fix |
|---|---|
Required 'compute.subnetworks.use' permission |
Missing Network User on the subnet, grant it to the user or service account |
| Cannot enable Shared VPC | Missing Shared VPC Admin at organization or folder level |
| Project cannot be attached | Different organization, or already attached to another host |
| Subnet not listed as usable | Project not attached, or no IAM grant on that subnet |
| VM cannot reach another VM | Firewall rule is missing in the host project, add it there |