如何在 Terraform 中按顺序创建 GCP SQL 数据库?
How do I create a GCP SQL database sequentially in Terraform?
对于 ,我需要能够在 GCP 中配置多个 SQL 数据库。这些数据库还需要位于具有私有 IP 地址的 VPC 上。
配置
resource "google_compute_network" "container_network" {
name = "container-network"
}
resource "google_compute_global_address" "sql" {
name = "sql-private-ip"
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 16
network = google_compute_network.container_network.self_link
}
resource "google_service_networking_connection" "sql" {
network = google_compute_network.container_network.self_link
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.sql.name]
}
resource "google_sql_database_instance" "master" {
provider = google-beta
count = 1
database_version = "MYSQL_5_7"
depends_on = [google_service_networking_connection.sql]
settings {
tier = "db-f1-micro"
ip_configuration {
ipv4_enabled = false
private_network = google_compute_network.container_network.id
}
}
}
当我将此配置用于单个数据库时,效果很好。如果我随后更改 count = 2
,那也可以正常工作。但是,如果我破坏基础设施并设置 count = 2
,它会失败。
Error: Error waiting for Create Instance: Failed to create subnetwork. <eye3 title='INVALID_ARGUMENT'/> generic::INVALID_ARGUMENT: New subnetwork overlaps with an IP range (10.28.0.0/24) in one of the active peers of an active peer (consumer-311061328248).
对我来说,这似乎是同时创建了两个数据库并尝试分配相同的 IP 范围。
有没有办法按顺序或同时创建数据库?我怀疑这将是一个 Terraform 问题,而不是一个 GCP 问题,尽管我对此持开放态度。
我正在测试看看是否有一些奇特的东西依赖于自己:
resource "google_sql_database_instance" "master" {
provider = google-beta
count = 2
database_version = "MYSQL_5_7"
depends_on = [google_sql_database_instance.master[count.index-1]]
...
}
但是不,不幸的是不允许,我们得到:Error: Invalid expression
A single static variable reference is required: only attribute access and
indexing with constant keys. No calculations, function calls, template
expressions, etc are allowed here.
除了我在评论中提到的使用多个 resource "google_sql_database_instance"
以及它们之间的依赖关系 (不是那个选项的忠实粉丝)...
我看到的唯一其他选项是将 terraform 并行度设置为 1,这将限制资源创建。
您可以在这里阅读更多相关信息:
https://www.terraform.io/docs/commands/apply.html#parallelism-n
对于 ,我需要能够在 GCP 中配置多个 SQL 数据库。这些数据库还需要位于具有私有 IP 地址的 VPC 上。
配置
resource "google_compute_network" "container_network" {
name = "container-network"
}
resource "google_compute_global_address" "sql" {
name = "sql-private-ip"
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 16
network = google_compute_network.container_network.self_link
}
resource "google_service_networking_connection" "sql" {
network = google_compute_network.container_network.self_link
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = [google_compute_global_address.sql.name]
}
resource "google_sql_database_instance" "master" {
provider = google-beta
count = 1
database_version = "MYSQL_5_7"
depends_on = [google_service_networking_connection.sql]
settings {
tier = "db-f1-micro"
ip_configuration {
ipv4_enabled = false
private_network = google_compute_network.container_network.id
}
}
}
当我将此配置用于单个数据库时,效果很好。如果我随后更改 count = 2
,那也可以正常工作。但是,如果我破坏基础设施并设置 count = 2
,它会失败。
Error: Error waiting for Create Instance: Failed to create subnetwork. <eye3 title='INVALID_ARGUMENT'/> generic::INVALID_ARGUMENT: New subnetwork overlaps with an IP range (10.28.0.0/24) in one of the active peers of an active peer (consumer-311061328248).
对我来说,这似乎是同时创建了两个数据库并尝试分配相同的 IP 范围。
有没有办法按顺序或同时创建数据库?我怀疑这将是一个 Terraform 问题,而不是一个 GCP 问题,尽管我对此持开放态度。
我正在测试看看是否有一些奇特的东西依赖于自己:
resource "google_sql_database_instance" "master" {
provider = google-beta
count = 2
database_version = "MYSQL_5_7"
depends_on = [google_sql_database_instance.master[count.index-1]]
...
}
但是不,不幸的是不允许,我们得到:Error: Invalid expression
A single static variable reference is required: only attribute access and indexing with constant keys. No calculations, function calls, template expressions, etc are allowed here.
除了我在评论中提到的使用多个 resource "google_sql_database_instance"
以及它们之间的依赖关系 (不是那个选项的忠实粉丝)...
我看到的唯一其他选项是将 terraform 并行度设置为 1,这将限制资源创建。
您可以在这里阅读更多相关信息:
https://www.terraform.io/docs/commands/apply.html#parallelism-n