无法在 Terraform 中输入列表变量值

Unable to Input list variable values in Terraform

我正在尝试使用 Terraform 创建 CosmosDB 并为子网使用列表变量类型,因为我们在 VNET 中有多个子网。但是我遇到了以下错误 -

var.subnet 是包含 2 个元素的字符串列表

│ 不能在字符串模板中包含给定值:需要字符串。

我的variable.tf和main.tf在这里-

# ================
# Variable.tf
# ==============
variable "vnet" {
  description = "Provide VNET Name"
  default     = "cosmosdb-icn-vnet"
}

variable "subnet" {
  description = "Specifies subnet name"
  type        = list(string)
}

# ====================
# main.tf
# ===================
resource "azurerm_cosmosdb_account" "db" {
  name                              = "cosmosdb-${var.environment}"
  location                          = var.location
  resource_group_name               = var.rgname
  offer_type                        = "Standard"
  enable_automatic_failover         = true
  is_virtual_network_filter_enabled = true
 
  consistency_policy {
    consistency_level = "Session"
  }

  tags = {
    ENVIRONMENT = var.environment
  }

  backup {
    type                = var.backuptype
    interval_in_minutes = "60"
    retention_in_hours  = "8"
  }

  virtual_network_rule {
    id                                   = "/subscriptions/${var.subscription}/resourceGroups/${var.rgname}/providers/Microsoft.Network/virtualNetworks/${var.vnet}/subnets/${var.subnet}"
    ignore_missing_vnet_service_endpoint = true

  }

  geo_location {
    location          = var.failover_location
    failover_priority = 0
  }

}

我正在使用以下命令申请

terraform apply -var='subnet=["deafult", "cosmosdb2.0"]'

您应该尝试将两个子网分开,从而在资源中创建两个 virtual_network_rule 块:

# ================
# Variable.tf
# ==============
variable "vnet" {
  description = "Provide VNET Name"
  default     = "cosmosdb-icn-vnet"
}

variable "subnets" {
  description = "Specifies subnet names"
  type        = list(string)
}

# ====================
# main.tf
# ===================
resource "azurerm_cosmosdb_account" "db" {
  name                              = "cosmosdb-${var.environment}"
  location                          = var.location
  resource_group_name               = var.rgname
  offer_type                        = "Standard"
  enable_automatic_failover         = true
  is_virtual_network_filter_enabled = true
 
  consistency_policy {
    consistency_level = "Session"
  }

  tags = {
    ENVIRONMENT = var.environment
  }

  backup {
    type                = var.backuptype
    interval_in_minutes = "60"
    retention_in_hours  = "8"
  }

  dynamic "virtual_network_rule" {
  for_each = var.subnets
    id                                   = "/subscriptions/${var.subscription}/resourceGroups/${var.rgname}/providers/Microsoft.Network/virtualNetworks/${var.vnet}/subnets/${virtual_network_rule.value}"
    ignore_missing_vnet_service_endpoint = true
  }

  geo_location {
    location          = var.failover_location
    failover_priority = 0
  }

}