-1

enter code hereI have an issue with my Terraform config for GCP resources.

I have an InstanceTemplate, MIG, LB.

I am looking to pass the LB Frontent IP to the metadata of the Instance being created during the initial terraform apply but the Cycle Error is flagging up.

I am aware that the there is various dependencies that are active in the solution.

Any help would be appreciated.

Error recieved is as follows

│ Error: Cycle: local.lb_ip (expand), google_compute_instance_template.test_instance_template, google_compute_region_instance_group_manager.test_development_instances, google_compute_region_backend_service.backend, google_compute_forwarding_rule.test_test_forwarding_rule
│

InstanceTemplate code:

# instance template
resource "google_compute_instance_template" "test_template" {
  name_prefix = "${var.instance_name}-instance"
  description = "This template is used to create TestAPP instance in MIG"

  tags   = ["test-app"]
  labels = var.labels

  instance_description = "TEST MIG instance"
  machine_type         = var.instance_machine_type
  scheduling {
    automatic_restart   = true
    on_host_maintenance = "MIGRATE"
  }
  # The boot disk image used by the instances
  disk {
    source_image = "debian-cloud/debian-11"
    type         = "PERSISTENT"
    disk_type    = "pd-ssd"
    disk_size_gb = 60
    mode         = "READ_WRITE"
    boot         = true
  }
  metadata = {
    "startup-script" = templatefile("./templates/startup.sh.tpl",
      {
        PUBLIC_ADDRESS = local.lb_ip
      })
  }
  network_interface {
    network    = google_compute_network.test-network.id
    subnetwork = google_compute_subnetwork.test-subnet.id
  }
  service_account {
    email  = google_service_account.test_service_account.email
    scopes = ["cloud-platform"]
  }
  lifecycle {
    create_before_destroy = true
  }
}

Locals

locals {
  lb_ip           = google_compute_forwarding_rule.test_test_forwarding_rule.ip_address 
}

I know there is no issue with the application using the metadata value passed as I was able to replace PUBLIC_ADDRESS = local.lb_ip with PUBLIC_ADDRESS = var.test_ip ( a random IP ).

So I know the issue is creation of the LB.

Michael98
  • 9
  • 2

0 Answers0