How to Create a VPC in GCP: Step-by-Step Guide
To create a VPC in GCP, use the
gcloud compute networks create command with a network name and subnet mode. This sets up a private network where you can launch your cloud resources securely.Syntax
The basic command to create a VPC network in GCP is:
gcloud compute networks create NETWORK_NAME --subnet-mode=MODEWhere:
- NETWORK_NAME: Your chosen name for the VPC network.
- --subnet-mode: Defines how subnets are created. Common modes are
auto(automatic subnets in each region) orcustom(you create subnets manually).
bash
gcloud compute networks create NETWORK_NAME --subnet-mode=MODE
Example
This example creates a custom mode VPC named my-vpc. Custom mode means you will add subnets yourself later.
bash
gcloud compute networks create my-vpc --subnet-mode=custom
Output
Created [https://www.googleapis.com/compute/v1/projects/PROJECT_ID/global/networks/my-vpc].
Common Pitfalls
Common mistakes when creating VPCs include:
- Using
autosubnet mode when you want full control over subnets. - Not specifying a unique network name, causing conflicts.
- Forgetting to create subnets in custom mode, which means no usable IP ranges.
Always plan your subnet ranges before creating a custom VPC.
bash
## Wrong: Creating custom VPC but no subnets added gcloud compute networks create my-vpc --subnet-mode=custom ## Right: Add subnet after creating custom VPC gcloud compute networks create my-vpc --subnet-mode=custom gcloud compute networks subnets create my-subnet --network=my-vpc --region=us-central1 --range=10.0.0.0/24
Quick Reference
Tips for creating VPCs in GCP:
- Use
autosubnet mode for quick setup with default subnets. - Use
customsubnet mode for precise control over IP ranges and regions. - Always name your network clearly to avoid confusion.
- Create subnets immediately after custom VPC creation.
Key Takeaways
Use the gcloud command with --subnet-mode to create a VPC network in GCP.
Choose 'auto' mode for automatic subnets or 'custom' mode to manage subnets yourself.
Always create subnets after a custom mode VPC to have usable IP ranges.
Pick unique and clear network names to avoid conflicts.
Plan your IP ranges before creating custom VPCs to prevent overlap.