Я пытаюсь создать несколько учетных записей хранения и несколько контейнеров в каждой учетной записи. Мне нужно создать это как модуль, чтобы я мог использовать его снова. Я думаю об этом, создав переменную типа
storageaccounts = [
{
name = "testbackupstorage11"
containers = ["logs", "web", "backups"]
},
{
name = "testbackupstorage12"
containers = ["logs-1", "web-1"]
}
]
. Я создал следующий код. Тем не менее, я думаю, что эта строка
count = length(var.storageaccounts.*.containers)
дает мне ошибку. Я хочу l oop через массив storageaccount, получить контейнеры и назначить «длину» ключа контейнеров для «count» внутри «azurerm_storage_container», чтобы этот блок создал несколько учетных записей хранения.
Тем не менее, это не работает, как ожидалось, скорее всего из-за * Я также проверил с
count = length(var.storageaccounts[count.index].containers)
, когда я делаю это, я получаю ошибку
on ..\modules\storage\main.tf line 21, in resource "azurerm_storage_container" "this":
21: count = length(var.storageaccounts[count.index].containers)
The "count" object can be used only in "resource" and "data" blocks, and only
when the "count" argument is set.
Как я могу сделать sh это? Или есть способ получше?
Вот полный код.
resource "random_id" "this" {
count = length(var.storageaccounts)
keepers = {
storagename = 1
}
byte_length = 6
prefix = var.storageaccounts[count.index].name
}
resource "azurerm_storage_account" "this" {
count = length(var.storageaccounts)
name = substr(lower(random_id.this[count.index].hex), 0, 24)
resource_group_name = var.resourcegroup
location = var.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_storage_container" "this" {
count = length(var.storageaccounts.*.containers)
name = var.storageaccounts[count.index].containers[count.index]
storage_account_name = azurerm_storage_account.this[count.index].name
container_access_type = "private"
}
provider "random" {
version = "2.2"
}
locals {
storageaccounts = [
{
name = "testbackupstorage11"
containers = ["logs", "web", "backups"]
},
{
name = "testbackupstorage12"
containers = ["logs-1", "web-1"]
}
]
}
module "storage" {
source = "../modules/storage"
resourcegroup = "my-test"
location = "eastus"
storageaccounts = local.storageaccounts
}
provider "azurerm" {
version = "=2.0.0"
features {}
}
//variable "prefix" {}
variable "location" {}
variable "resourcegroup" {}
variable "storageaccounts" {
default = []
type = list(object({
name = string
containers = list(string)
}))
}