繁体   English   中英

Terraform - 来自模块 output 的参考 for_each

[英]Terraform - Reference for_each from a module output

我正在尝试使用 Terraform 创建多个存储容器,然后将多个 blob 上传到每个容器。

我有创建多个容器工作的部分,但无法弄清楚在上传 blob 时如何引用每个容器的 for_each output。

存储容器模块(工程)

resource "azurerm_storage_container" "azure" {
  for_each              = toset(var.storage_containers)
  name                  = each.value
  storage_account_name  = var.storage_account_name
  container_access_type = var.storage_account_container_access_type
}

output "azurerm_storage_container_name" {
  value = toset(keys(azurerm_storage_container.azure))
}

子模块(作品)

module "storage_container" {
  source = "C:/TerraformModules/modules/azurerm/azurerm_storage_container"
  storage_account_name = module.storage_account.azurerm_storage_account_name
  storage_containers   = var.STORAGE_CONTAINER_NAMES
  tags                 = var.TAGS
}

上传 blob 的代码(不适用于尝试上传到每个容器中)

**In a variables.tf file**
variable "STORAGE_CONTAINER_DEFAULT_BLOBS" {
  description = "The default blobs in each storage container"
  type        = list(string)
}

**In a vars.auto.tfvars file**
STORAGE_CONTAINER_DEFAULT_BLOBS = ["one", "two", "three"]

**In a main.tf file**
resource "azurerm_storage_blob" "storage_blob" {
  for_each               = toset(var.STORAGE_CONTAINER_DEFAULT_BLOBS)
  name                   = each.value
  storage_account_name   = module.storage_account.azurerm_storage_account_name
  storage_container_name = module.storage_container[each.value].azurerm_storage_container_name
  type                   = "Block"
  source_content         = "blob file"
}

如果我要在storage_container_name中设置容器名称,它会起作用并且容器会获取每个 blob。 但我无法从模块中引用容器。

我有这个错误:

Error: Invalid index

  on storage_blobs.tf line 5, in resource "azurerm_storage_blob" "storage_blob":
   5:   storage_container_name = module.storage_container[each.value].azurerm_storage_container_name
    |----------------
    | each.value is "two"
    | module.storage_container is object with 1 attribute "azurerm_storage_container_name"

The given key does not identify an element in this collection value.

我需要达到的目标:

resource "azurerm_storage_blob" "storage_blob" {
  for_each               = toset(var.STORAGE_CONTAINER_DEFAULT_BLOBS)
  name                   = each.value
  storage_account_name   = module.storage_account.azurerm_storage_account_name
  storage_container_name = # How to reference the storage accounts created with the `storage_container ` module? #
  type                   = "Block"
  source_content         = "blob file"
}

storage_container_name只接受一个值,而不是值列表。 因此,如果您有 3 个STORAGE_CONTAINER_DEFAULT_BLOBS和 n 个var.storage_containers您必须在azurerm_storage_blob中迭代n*3次。

因此,您可以使用setproduct尝试以下操作:

resource "azurerm_storage_blob" "storage_blob" {

  for_each               = {for idx, val in setproduct(module.storage_container.azurerm_storage_container_name, var.STORAGE_CONTAINER_DEFAULT_BLOBS): idx=>val}

  name                   = each.value[1]
  storage_account_name   = module.storage_account.azurerm_storage_account_name
  storage_container_name = each.value[0]
  type                   = "Block"
  source_content         = "blob file"
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM