terraform-google-modules / terraform-google-network

Sets up a new VPC network on Google Cloud
https://registry.terraform.io/modules/terraform-google-modules/network/google
Apache License 2.0
411 stars 1.23k forks source link

Importing proxy only subnet is successful but then not saved in state. terraform plan shows subnet being created #559

Closed thbriggs closed 4 months ago

thbriggs commented 4 months ago

TL;DR

If I try to import an existing proxy only subnet I get a message saying its successful but then it doesnt show up in state.

Expected behavior

subnet to become part of state for being managed by terraform

Observed behavior

terraform plan shows a creation of the subnet

Terraform Configuration

from main.tf
resource "google_compute_subnetwork" "subnet" {
  for_each                 = var.subnetwork
  name                     = each.key
  project                  = var.project
  region                   = var.region
  ip_cidr_range            = each.value.ip_cidr_range
  private_ip_google_access = each.value.private_ip_google_access
  network                  = var.network != null ? var.network : local.network
  purpose                  = lookup(each.value, "purpose", null)
  role                     = lookup(each.value, "role", null)

from variables.tf
variable "subnetwork" {
  description = "List of subnet definitions."
  type = map(object({
    ip_cidr_range            = string
    private_ip_google_access = bool
    purpose                  = optional(string)
    role                     = optional(string)
    subnetwork_logging       = bool

from subnetworks.tf which uses the modules listed int the previous files:

locals {
  subnetworks = {
    "test-2" = {
      ip_cidr_range            = "192.168.60.0/24"
      private_ip_google_access = false
      purpose                  = "REGIONAL_MANAGED_PROXY"
      role                     = "ACTIVE"
      subnetwork_logging       = false
      log_config = []
      use_secondary_ip_range = false
      secondary_ip_range     = []
    }
  }
}

Terraform Version

Terraform v1.4.6
on darwin_arm64

Additional information

No response

thbriggs commented 4 months ago

I realized I was importing the subnet wrong in the module after all.

This formatting worked. The bracketed, quoted subnet name was what I was missing. terraform import 'module.subnetwork.google_compute_subnetwork.subnet["test-2"]' projects/[project name]/regions/us-east4/subnetworks/test-2