Terraform:尝试使用列表创建一系列子网 cidrs,但收到错误“需要字符串”

Pyd*_*dam 4 google-cloud-platform terraform terraform-provider-gcp

如下创建了一个范围列表

subnet_names = ["subnet-lister", "subnet-kryten", "subnet-rimmer", "subnet-cat", "subnet-holly",]
subnet_cidrs = ["192.2.128.0/18", "192.2.0.0/17", "192.2.208.0/20", "192.2.192.0/20", "192.2.224.0/20",]
Run Code Online (Sandbox Code Playgroud)

有了这个在子网.tf

resource "google_compute_subnetwork" "subnet" {
  name          = "${var.subnet_names}-subnet"
  ip_cidr_range = var.subnet_cidrs
  network       = var.network_name
  region        = var.subnet_region
Run Code Online (Sandbox Code Playgroud)

下面是 variables.tf 中的(对于模块)

variable "subnet_names" {
  description = "The name to use for Subnet "
  type        =  list(string)
}

variable "subnet_cidrs" {
  description = "The cidr range for for Subnets"
  type        = list(string)
}
Run Code Online (Sandbox Code Playgroud)

但是从 Terraform 收到以下消息。

Error: Incorrect attribute value type

  on ..\..\..\Test-Modules\red\dwarf\subnets.tf line 3, in resource "google_compute_subnetwork" "subnet":
   3:   ip_cidr_range = var.subnet_cidrs

Inappropriate value for attribute "ip_cidr_range": string required.
Run Code Online (Sandbox Code Playgroud)

我对此很陌生,你能帮我弄清楚我出了什么问题。我似乎其他人使用了 cidr 范围的列表(请注意,这是针对 AWS 的)。GCP 不支持吗?

Ben*_*ley 5

看起来您要做的实际上是创建多个子网。为此,您应该使用一个map变量和一个循环。

variable "subnets" {
    type = map(string)
}

resource "google_compute_subnetwork" "subnet" {
  for_each      = var.subnets
  name          = each.key
  ip_cidr_range = each.value
  ...
}
Run Code Online (Sandbox Code Playgroud)

然后你可以提供子网,如:

subnets = {
    subnet-lister = "192.2.128.0/18",
    subnet-kryten = "192.2.0.0/17",
    ...
}
Run Code Online (Sandbox Code Playgroud)