0
0
GCPcloud~5 mins

Subnet modes (auto, custom) in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you create a network in Google Cloud, you need to decide how the network assigns IP ranges to subnets. Subnet modes control whether subnets are created automatically or manually. This helps organize your network and control IP address allocation.
When you want Google Cloud to create subnets for you automatically in each region.
When you need to control exactly which IP ranges your subnets use in specific regions.
When you want to add or remove subnets later without affecting others.
When you want to isolate parts of your network with custom IP ranges.
When you want a simple network setup without manual subnet management.
Config File - main.tf
main.tf
provider "google" {
  project = "example-project"
  region  = "us-central1"
}

resource "google_compute_network" "auto_network" {
  name                    = "auto-subnet-network"
  auto_create_subnetworks = true
}

resource "google_compute_network" "custom_network" {
  name                    = "custom-subnet-network"
  auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "custom_subnet" {
  name          = "custom-subnet"
  ip_cidr_range = "10.10.0.0/24"
  region        = "us-central1"
  network       = google_compute_network.custom_network.id
}

This Terraform file creates two networks in Google Cloud.

  • auto_network: This network uses auto_create_subnetworks = true, so Google Cloud automatically creates subnets in each region with default IP ranges.
  • custom_network: This network disables automatic subnet creation with auto_create_subnetworks = false. You then manually create a subnet named custom_subnet with a specific IP range 10.10.0.0/24 in the us-central1 region.

This setup shows the difference between auto and custom subnet modes.

Commands
This command initializes Terraform in the current directory. It downloads the Google Cloud provider plugin and prepares Terraform to manage resources.
Terminal
terraform init
Expected OutputExpected
Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command applies the Terraform configuration to create the networks and subnet in Google Cloud. The -auto-approve flag skips manual approval to speed up the process.
Terminal
terraform apply -auto-approve
Expected OutputExpected
google_compute_network.auto_network: Creating... google_compute_network.custom_network: Creating... google_compute_network.auto_network: Creation complete after 3s [id=projects/example-project/global/networks/auto-subnet-network] google_compute_network.custom_network: Creation complete after 3s [id=projects/example-project/global/networks/custom-subnet-network] google_compute_subnetwork.custom_subnet: Creating... google_compute_subnetwork.custom_subnet: Creation complete after 4s [id=projects/example-project/regions/us-central1/subnetworks/custom-subnet] Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approve the apply step without prompting
This command shows details about the auto subnet mode network, including the automatically created subnets and their IP ranges.
Terminal
gcloud compute networks describe auto-subnet-network
Expected OutputExpected
name: auto-subnet-network autoCreateSubnetworks: true subnetworks: - https://www.googleapis.com/compute/v1/projects/example-project/regions/us-central1/subnetworks/auto-subnet-network-us-central1 - https://www.googleapis.com/compute/v1/projects/example-project/regions/us-east1/subnetworks/auto-subnet-network-us-east1
This command shows details about the custom subnet mode network. It will list only the manually created subnet with its IP range.
Terminal
gcloud compute networks describe custom-subnet-network
Expected OutputExpected
name: custom-subnet-network autoCreateSubnetworks: false subnetworks: - https://www.googleapis.com/compute/v1/projects/example-project/regions/us-central1/subnetworks/custom-subnet
Key Concept

If you remember nothing else from this pattern, remember: auto subnet mode creates subnets for you automatically, while custom subnet mode lets you control subnet IP ranges manually.

Common Mistakes
Setting auto_create_subnetworks to true but also trying to create custom subnets manually.
Google Cloud will create default subnets automatically, so manual subnets may conflict or cause confusion.
Use auto_create_subnetworks = false if you want to create custom subnets manually.
Not specifying the region when creating a custom subnet.
Subnets must be created in a specific region; missing this causes errors.
Always specify the region field when defining a custom subnet.
Summary
Use auto subnet mode to let Google Cloud create subnets automatically in all regions.
Use custom subnet mode to manually create subnets with specific IP ranges in chosen regions.
Terraform can manage both network types by setting auto_create_subnetworks true or false.