Bird
Raised Fist0
GCPcloud~5 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What happens when you create a VPC network in auto subnet mode in GCP?
easy
A. Subnets are automatically created in all regions with predefined IP ranges.
B. No subnets are created; you must create them manually.
C. Only one subnet is created in the default region.
D. Subnets are created but without any IP ranges assigned.

Solution

  1. Step 1: Understand auto subnet mode behavior

    In auto mode, GCP automatically creates subnets in every region with default IP ranges.
  2. Step 2: Compare with other modes

    Unlike custom mode, auto mode does not require manual subnet creation or IP range assignment.
  3. Final Answer:

    Subnets are automatically created in all regions with predefined IP ranges. -> Option A
  4. Quick Check:

    Auto mode = automatic subnet creation [OK]
Hint: Auto mode means automatic subnets in all regions [OK]
Common Mistakes:
  • Thinking auto mode requires manual subnet creation
  • Assuming only one subnet is created
  • Believing subnets have no IP ranges assigned
2. Which of the following is the correct way to create a custom subnet in GCP using gcloud CLI?
easy
A. gcloud compute networks subnets create my-network --subnet-mode=custom
B. gcloud compute networks create my-network --subnet-mode=auto
C. gcloud compute networks create my-subnet --range=10.0.0.0/24
D. gcloud compute networks subnets create my-subnet --network=my-network --range=10.0.0.0/24

Solution

  1. Step 1: Identify command to create a subnet

    The command to create a subnet is gcloud compute networks subnets create with network and IP range specified.
  2. Step 2: Check option correctness

    gcloud compute networks subnets create my-subnet --network=my-network --range=10.0.0.0/24 correctly uses the subnet creation command with network and IP range parameters.
  3. Final Answer:

    gcloud compute networks subnets create my-subnet --network=my-network --range=10.0.0.0/24 -> Option D
  4. Quick Check:

    Subnet creation uses 'networks subnets create' with range [OK]
Hint: Use 'networks subnets create' with --range for custom subnets [OK]
Common Mistakes:
  • Using 'networks create' to create subnets
  • Missing the --range parameter for IP range
  • Confusing network and subnet names
3. Given a VPC network in custom subnet mode with two subnets:
subnet-a: 10.1.0.0/16
subnet-b: 10.2.0.0/16
What happens if you try to create a third subnet with IP range 10.1.128.0/17?
medium
A. The subnet is created successfully without issues.
B. The subnet is created but traffic is blocked between subnets.
C. Creation fails due to overlapping IP ranges with subnet-a.
D. The subnet is created but assigned a different IP range automatically.

Solution

  1. Step 1: Check IP range overlap

    Subnet-a uses 10.1.0.0/16 which covers 10.1.0.0 to 10.1.255.255. The new subnet 10.1.128.0/17 overlaps this range.
  2. Step 2: Understand subnet creation rules

    GCP does not allow overlapping IP ranges in subnets within the same VPC network.
  3. Final Answer:

    Creation fails due to overlapping IP ranges with subnet-a. -> Option C
  4. Quick Check:

    Overlapping IP ranges cause subnet creation failure [OK]
Hint: Check IP ranges for overlap before subnet creation [OK]
Common Mistakes:
  • Assuming subnets can overlap IP ranges
  • Thinking GCP auto-adjusts overlapping ranges
  • Believing traffic is blocked but subnet created
4. You created a VPC network in custom subnet mode but forgot to create any subnets. What is the result when you try to deploy a VM instance in this network?
medium
A. The VM instance deploys successfully with an automatic subnet created.
B. The VM deployment fails because no subnet exists in the network.
C. The VM deploys but without an internal IP address.
D. The VM deploys but is not reachable from other resources.

Solution

  1. Step 1: Understand custom subnet mode requirements

    In custom mode, subnets must be created manually before deploying resources.
  2. Step 2: Check VM deployment dependency

    VMs require a subnet to get an IP address; without subnets, deployment fails.
  3. Final Answer:

    The VM deployment fails because no subnet exists in the network. -> Option B
  4. Quick Check:

    Custom mode needs subnets before VM deployment [OK]
Hint: No subnet means VM deployment fails in custom mode [OK]
Common Mistakes:
  • Assuming auto subnet creation in custom mode
  • Thinking VM can deploy without internal IP
  • Believing VM deploys but is unreachable
5. You want to create a VPC network that spans multiple regions with subnets having specific IP ranges you control. Which subnet mode should you choose and why?
hard
A. Custom mode, because it lets you manually create subnets with specific IP ranges in each region.
B. Custom mode, because it automatically creates subnets in all regions with default IP ranges.
C. Auto mode, because it allows you to edit IP ranges after subnet creation.
D. Auto mode, because it creates subnets automatically with your chosen IP ranges.

Solution

  1. Step 1: Identify requirement for specific IP ranges

    You want control over IP ranges, so automatic default ranges won't work.
  2. Step 2: Choose subnet mode matching control needs

    Custom mode allows manual subnet creation with chosen IP ranges per region.
  3. Step 3: Eliminate incorrect options

    Auto mode does not allow choosing IP ranges; it creates default subnets automatically.
  4. Final Answer:

    Custom mode, because it lets you manually create subnets with specific IP ranges in each region. -> Option A
  5. Quick Check:

    Custom mode = manual subnet creation with chosen IP ranges [OK]
Hint: Custom mode for manual subnets with specific IP ranges [OK]
Common Mistakes:
  • Confusing auto mode as allowing custom IP ranges
  • Thinking auto mode subnets can be edited after creation
  • Believing custom mode auto-creates subnets