这个
Terraform code to create a Cloud SQL instance with Private IP
有一些错误。第一个是
${google_compute_network.private_network.self_link}
变量获取整个网络的名称,这意味着
www.googleapis.com/compute/v1/projects/PROJECT-ID/global/networks/testnw2
. 字段中不允许此值
google_compute_global_address.private_ip_address.network
,因此,您需要将$Google_Compute_Network.private_Network.self_link更改为$Google_Compute_Network.private_Network.name。
另一个错误是
google_sql_database_instance.instance.settings.ip_configuration.private_network
应该是
projects/PROJECT_ID/global/networks/NW_ID
. 所以你需要把字段改为
projects/[PROJECT_ID]/global/networks/${google_compute_network.private_network.name}
为了工作。
第三个错误,以及您在初始消息中共享的错误,需要设置
service account
在TerraForm代码中具有适当的特权以避免此错误。请检查共享代码的第一行。
第四个错误是您需要使用Google beta提供程序,而不是Google默认的提供程序来执行此操作。
正如我发表的评论中所讨论的,我看到
"An Unknown Error occurred"
错误在使用该地形代码之前,此错误是指在执行vpc对等时发生的错误。我理解这是令人沮丧的故障排除,因为它没有显示任何有用的信息,但如果您在谷歌云平台支持打开一张罚单,我们将能够使用我们的内部工具检查真正的错误。
正如所承诺的,这是我用来创建私有网络并在创建时将其附加到谷歌云SQL实例的代码。
provider "google-beta" {
credentials = "${file("CREDENTIALS.json")}"
project = "PROJECT-ID"
region = "us-central1"
}
resource "google_compute_network" "private_network" {
name = "testnw"
}
resource "google_compute_global_address" "private_ip_address" {
provider="google-beta"
name = "${google_compute_network.private_network.name}"
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 16
network = "${google_compute_network.private_network.name}"
}
resource "google_service_networking_connection" "private_vpc_connection" {
provider="google-beta"
network = "${google_compute_network.private_network.self_link}"
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = ["${google_compute_global_address.private_ip_address.name}"]
}
resource "google_sql_database_instance" "instance" {
provider="google-beta"
depends_on = ["google_service_networking_connection.private_vpc_connection"]
name = "privateinstance"
region = "us-central1"
settings {
tier = "db-f1-micro"
ip_configuration {
ipv4_enabled = "false"
private_network = "projects/PROJECT-ID/global/networks/${google_compute_network.private_network.name}"
}
}
}