GoogleCloudPlatform / cloud-foundation-fabric

End-to-end modular samples and landing zones toolkit for Terraform on GCP.
Apache License 2.0
1.51k stars 868 forks source link

Compute-VM module requires subnetwork_project variable in network_interface #1398

Closed DrishaS23 closed 1 year ago

DrishaS23 commented 1 year ago

Working code is

in main.tf:

dynamic "network_interface" {
    for_each = var.network_interfaces
    iterator = config
    content {
      network    = config.value.network
      subnetwork = config.value.subnetwork
      network_ip = try(config.value.addresses.internal, null)
      subnetwork_project = config.value.subnetwork_project
      dynamic "access_config" {
        for_each = config.value.nat ? [""] : []
        content {
          nat_ip = try(config.value.addresses.external, null)
        }
      }

in variables.tf:

variable "network_interfaces" {
  description = "Network interfaces configuration. Use self links for Shared VPC, set addresses to null if not needed."
  type = list(object({
    nat        = optional(bool, false)
    network    = string
    subnetwork = string
    subnetwork_project = string
    addresses = optional(object({
      internal = string
      external = string
    }), null)
    alias_ips = optional(map(string), {})
    nic_type  = optional(string)
  }))
}

in terraform.tfvars:

network_interfaces = [{
    network    =  "vnet-1"
    subnetwork =  "subnet-1"
    subnetwork_project = "cft-prj"
} ]

Earlier we were getting the error:

google_compute_instance.default[0]: Creating... ╷ │ Error: Error creating network interfaces: cannot determine self_link for subnetwork "subnet-1": network_interface.0.subnetwork_project: required field is not set │ │ with google_compute_instance.default[0], │ on main.tf line 119, in resource "google_compute_instance" "default": │ 119: resource "google_compute_instance" "default" { │

ludoo commented 1 year ago

I'm not sure I understand what you are pasting above: can you just paste the configuration you used that triggered the error?

DrishaS23 commented 1 year ago

This is the config i used:

name = "test-vm" project_id = "cft-prj" zone = "us-central1-a" network_interfaces = [{ network = "vnet-1" subnetwork = "subnet-1" } ]

juliocc commented 1 year ago

The code is incomplete. But more importantly, the network and subnetwork fields are supposed to be self links.

We have a few complete examples in the docs and multiple blueprints showing how to deploy a simple vm. Here's one that can help you get started