想要使用Terraform在gcp中的单独vpc上使用私有和公共ip创建云SQL实例

时间:2020-04-03 15:30:57

标签: google-cloud-platform terraform google-cloud-sql terraform-provider-gcp

我尝试使用Terraform在单独的vpc中使用私有和公共ip配置云SQL实例。可以从单独的vpc上为该实例分配私有ip,但无法与此同时分配公共ip。

这是我的代码。

resource "google_compute_global_address" "private_ip_address" {
  provider = google-beta

  name          = "private-ip-address"
  purpose       = "VPC_PEERING"
  address_type  = "INTERNAL"
  prefix_length = 16
  network       = "${var.vpc_self_link}"
}

resource "google_service_networking_connection" "private_vpc_connection" {
  provider = google-beta

  network                 = "${var.vpc_self_link}"
  service                 = "servicenetworking.googleapis.com"
  reserved_peering_ranges = [google_compute_global_address.private_ip_address.name]
}


# create database instance
resource "google_sql_database_instance" "instance" {
        name = "test-${var.project_id}"
        region = "us-central1"
        database_version = "${var.db_version}"

        depends_on = [google_service_networking_connection.private_vpc_connection]

        settings {
                tier = "${var.db_tier}"
                activation_policy = "${var.db_activation_policy}"
                disk_autoresize = "${var.db_disk_autoresize}"
                disk_size = "${var.db_disk_size}"
                disk_type = "${var.db_disk_type}"
                pricing_plan = "${var.db_pricing_plan}"

                database_flags {
                        name  = "slow_query_log"
                        value = "on"
                }

                ip_configuration {
                        ipv4_enabled = "false"
                        private_network = "projects/${var.project_id}/global/networks/${var.vpc_name}"
                }
        }
}

但是当我尝试传递以下参数-分配公共ip时,由于private_network标志而给出了错误。

ipv4_enabled = "true"

请让我知道如何通过自定义或单独的vpc(不是默认一个)来解决私有和公共ip的问题。

1 个答案:

答案 0 :(得分:0)

根据the documentation,您不能

ipv4_enabled-(可选)是否应为此Cloud SQL实例分配一个公共IPV4地址。必须启用ipv4_enabled或必须配置private_network。

打开功能请求。