Alex
Alex

Reputation: 23

Terraform - Cyclic dependency issue on GCP

I am provisioning multiple resources on GCP including a Cloud SQL (Postgres) DB and one VM instance. I am struggling with a cyclic dependency on Terraform during terraform apply as:

Hence, the cyclic dependency... Do you have any suggestion to tackle this in Terraform?

File that creates the GCP VM (includes a startup script that requires the IP of the Postgres DB)

data "template_file" "startup_script_airbyte" {
  template = file("${path.module}/sh_scripts/airbyte.sh")
  vars = {
    db_public_ip = "${google_sql_database_instance.postgres.public_ip_address}"
    db_name_prefix = "${var.db_name}"
    db_user = "${var.db_user}"
    db_password = "${var.db_password}"
  }
}

resource "google_compute_instance" "airbyte_instance" {
  name                    = "${google_project.data_project.project_id}-airbyte"
  machine_type            = local.airbyte_machine_type
  project                 = google_project.data_project.project_id
  metadata_startup_script = data.template_file.startup_script_airbyte.rendered #file("./sh_scripts/airbyte.sh")
  allow_stopping_for_update = true

  depends_on = [
    google_project_service.data_project_services,
  ]

  boot_disk {
    initialize_params {
      image = "ubuntu-2004-focal-v20210415"
      size  = 50
      type  = "pd-balanced"
    }
  }
  network_interface {
    network = "default"
    access_config {
      network_tier = "PREMIUM"
    }
  }

  service_account {
    email  = google_service_account.airbyte_sa.email
    scopes = ["cloud-platform"]
  }
}

Script that creates the Postgres DB (requires IP of the VM above)

resource "google_sql_database_instance" "postgres" {
  name = "postgres-instance-${random_id.db_name_suffix.hex}"
  project = google_project.data_project.project_id
  database_version = "POSTGRES_13"
  settings{
    tier = "db-f1-micro"
    backup_configuration {
      enabled = true
      start_time = "02:00"
    }
    database_flags {
      name  = "cloudsql.iam_authentication"
      value = "on"
    }

    database_flags {
      name  = "max_connections"
      value = 30000
    }
    
    #Whitelisting the IPs of the GCE VMs in Postgres
    ip_configuration {
      ipv4_enabled = "true"
      authorized_networks {
        name = "${google_compute_instance.airbyte_instance.name}"
        value = "${google_compute_instance.airbyte_instance.network_interface.0.access_config.0.nat_ip}"
      }
    }
  }
}

Upvotes: 1

Views: 679

Answers (2)

John Hanley
John Hanley

Reputation: 81356

The correct solution is to install the Cloud SQL Auth Proxy in the VM. Then you do not need to whitelist IP addresses. This will remove the cyclic dependency.

Upvotes: 0

Marcin
Marcin

Reputation: 238199

One way to overcome this would be to get static public IP, using google_compute_address. You do this before you create your instance, and then attach it to the instance.

This way the IP can be whitelisted in Cloud SQL, before the instance is created.

Upvotes: 1

Related Questions