google_sql_database_instance 在创建具有私有 IP 的多个实例时等待创建实例时出错

时间:2021-05-15 21:04:51

标签: terraform terraform-provider-gcp

好的,让我看看我是否能正确解释这一点。我正在尝试创建多个 Cloud SQL 实例,每个实例都具有私有 IP 地址:

vpc.tf

resource "google_compute_network" "vpc" {
  name = var.name
  # routing_mode            = var.routing_mode
  # auto_create_subnetworks = true
}

resource "google_compute_global_address" "private_ip_block" {
  name          = google_compute_network.vpc.name
  purpose       = "VPC_PEERING"
  address_type  = "INTERNAL"
  prefix_length = var.cidr
  network       = google_compute_network.vpc.name
}

resource "google_service_networking_connection" "vpc_connection" {
  network = google_compute_network.vpc.self_link
  service = "servicenetworking.googleapis.com"
  reserved_peering_ranges = [
    google_compute_global_address.private_ip_block.name
  ]
}

db.tf

resource "google_sql_database" "database" {
  name     = var.database_name
  instance = google_sql_database_instance.instance.name
}

resource "google_sql_database_instance" "instance" {
  region           = var.instance_region
  database_version = var.database_version
  settings {
    tier              = var.instance_settings["tier"]
    availability_type = "REGIONAL"
    backup_configuration {
      enabled                        = true
      start_time                     = "00:00"
      transaction_log_retention_days = 2
      backup_retention_settings {
        retention_unit   = "COUNT"
        retained_backups = 3
      }
    }
    ip_configuration {
      ipv4_enabled = false
      # private_network = var.vpc_name
      private_network = "projects/${var.project_id}/global/networks/${var.vpc_name}"
    }
  }

  deletion_protection = false
}

resource "google_sql_user" "user" {
  name     = "${var.database_name}-user"
  instance = google_sql_database_instance.instance.name
  type     = "CLOUD_IAM_USER"
  depends_on = [
    google_sql_database_instance.instance
  ]
}

这两个都是从 main.tf 调用的模块,我使用对象映射来创建实例,但只创建了 1 个 vpc。

如果我将 IP 地址设置为 public,这会成功,但是当分配到带有消息的私有网络时,一个实例将失败:

╷
│ Error: Error waiting for Create Instance: 
│ 
│ 
│   with module.db["my_database"].google_sql_database_instance.instance,
│   on modules/db/db.tf line 13, in resource "google_sql_database_instance" "instance":
│   13: resource "google_sql_database_instance" "instance" {
│ 
╵

在创建实例大约一分钟后发生故障。它们似乎都开始正常创建,然后大约一分钟后,其中一个停止记录到 stdout,另一个完成后,出现错误。 GCP 控制台只是说发生了未知错误。就像我说的,当 IP 是公共的时,这会正常工作,所以它必须是我的专用网络配置,对吗?如果他们都尝试分配相同的 IP 地址,是否存在竞争条件?我应该按顺序而不是在 for_each 中创建这些还是添加随机延迟?

1 个答案:

答案 0 :(得分:0)