DrTeeth
DrTeeth

Reputation: 1317

Why does terraform ask me to recreate subnets on every apply to a GCP project?

I am very familiar with Terraform against AWS. Trying to port a project over to using GCP now.

I have a fairly simple .tf file:

resource "google_compute_network" "vpc" {
  name                    = "${local.resource_prefix}-vpc"
  auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "public_subnet_1" {
  name          =  "${local.resource_prefix}-public-subnet-1"
  ip_cidr_range = local.subnet_public_1_cidr
  network       = google_compute_network.vpc.id
  region        = local.gcp_region

  private_ip_google_access = false
}

This creates fine on the first apply, but on every subsequent apply, it asks me to force replace the subnet:

  # google_compute_subnetwork.private_subnet_1 must be replaced
-/+ resource "google_compute_subnetwork" "private_subnet_1" {
      ~ creation_timestamp       = "2020-06-11T08:12:27.002-07:00" -> (known after apply)
      + enable_flow_logs         = (known after apply)
      + fingerprint              = (known after apply)
      ~ gateway_address          = "10.1.100.1" -> (known after apply)
      ~ id                       = "projects/(project-id)/regions/us-east1/subnetworks/foo-private-subnet-1" -> (known after apply)
        ip_cidr_range            = "10.1.100.0/24"
        name                     = "foo-private-subnet-1"
      ~ network                  = "https://www.googleapis.com/compute/v1/projects/(project-name)/global/networks/foo-vpc" -> "projects/(project-id)/global/networks/foo-vpc" # forces replacement
        private_ip_google_access = false
      ~ project                  = "(project-id)" -> (known after apply)
        region                   = "us-east1"
      ~ secondary_ip_range       = [] -> (known after apply)
      ~ self_link                = "https://www.googleapis.com/compute/v1/projects/(project-name)/regions/us-east1/subnetworks/foo-private-subnet-1" -> (known after apply)
    }

The network of the subnet appears to be what's forcing the replacement, but this is coming from the VPC attributes, and as best I can tell I'm following every online example I can find.

What am I missing? Why the mismatch between name and id in the network, or is it something else?

Upvotes: 0

Views: 979

Answers (2)

user13852739
user13852739

Reputation: 1

Please remove .tfstate file and .tfstate.backup file after each apply

Upvotes: 0

DrTeeth
DrTeeth

Reputation: 1317

I believe the answer is simply to replace:

  network       = google_compute_network.vpc.id

with

  network       = google_compute_network.vpc.self_link

self_link is a bit of an odd name for this, and not what I've seen in a couple of tutorials so far, but it seems to be working fine.

Upvotes: 1

Related Questions