首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Terraform - GCP上的循环依赖问题

Terraform - GCP上的循环依赖问题
EN

Stack Overflow用户
提问于 2022-04-13 10:23:10
回答 2查看 158关注 0票数 1

我在GCP上提供多个资源,包括一个Cloud (Postgres) DB和一个VM实例。在terraform apply期间,我正在与Terraform的循环依赖进行斗争,如下所示:

  • 云SQL (Postgres)需要VM的IP来进行IP白名单。
  • VM使用需要Postgres的公共IP的启动脚本

因此,循环依赖..。你有什么建议在Terraform解决这个问题吗?

创建GCP的文件(包括一个需要Postgres的IP的启动脚本)

代码语言:javascript
复制
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"]
  }
}

创建Postgres的脚本(需要上面VM的IP )

代码语言:javascript
复制
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}"
      }
    }
  }
}
EN

回答 2

Stack Overflow用户

发布于 2022-04-13 10:31:21

克服这一问题的一种方法是使用静态公共IP,使用地址。在创建实例之前执行此操作,然后将其附加到实例。

这样,在创建实例之前,可以在Cloud中将IP白名单。

票数 1
EN

Stack Overflow用户

发布于 2022-04-13 16:40:10

正确的解决方案是在VM中安装Cloud代理。然后,您不需要白名单IP地址。这将消除循环依赖关系。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71855885

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档