Terraform - 来自模块输出的引用 for_each
Terraform - Reference for_each from a module output
我正在尝试使用 Terraform 创建多个存储容器,然后将多个 blob 上传到每个容器。
我可以创建多个容器,但不知道如何在上传 blob 时引用每个容器的 for_each 输出。
存储容器模块(有效)
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"
}
我正在尝试使用 Terraform 创建多个存储容器,然后将多个 blob 上传到每个容器。
我可以创建多个容器,但不知道如何在上传 blob 时引用每个容器的 for_each 输出。
存储容器模块(有效)
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"
}