我正在尝试使用Azure事件集线器命名空间中的Azure事件集线器中的计数来循环容器名称。然而,我到下面的错误,而地形规划。
下面是Terraform代码
resource "azurerm_resource_group" "test-rg" {
name = "test-rg"
location = "eastus"
}
variable "storageaccountname" {
type = string
default = "eusstestslogssa01"
}
variable "containers_list" {
type = list
default = [{ name = "eus-a-test-logs-bkp", access_type = "private" },
{ name = "insights-logs-auditevent", access_type = "private" },
{ name = "insights-logs-dataplanerequests", access_type = "private" }
]
}
variable "eventhubs" {
type = list
default = ["insights-logs-auditevent","insights-logs-dataplanerequests","insights-logs-eventhubvnetconnectionevent"]
}
resource "azurerm_storage_account" "storageaccount" {
name = var.storageaccountname
resource_group_name = azurerm_resource_group.test-rg.name
location = azurerm_resource_group.test-rg.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_storage_container" "container" {
count = length(var.containers_list)
name = var.containers_list[count.index].name
storage_account_name = azurerm_storage_account.storageaccount.name
container_access_type = var.containers_list[count.index].access_type
}
resource "azurerm_eventhub_namespace" "test-ens" {
depends_on = [
azurerm_storage_account.storageaccount,
]
name = "testens"
location = azurerm_resource_group.test-rg.location
resource_group_name = azurerm_resource_group.test-rg.name
sku = "Basic"
capacity = 1
minimum_tls_version = "1.1"
}
resource "azurerm_eventhub" "test-eventhubs" {
count = length(var.eventhubs)
name = var.eventhubs[count.index]
namespace_name = azurerm_eventhub_namespace.test-ens.name
resource_group_name = azurerm_resource_group.test-rg.name
partition_count = 4
message_retention = 1
capture_description {
enabled = true
encoding = "Avro"
destination {
count = length(var.containers_list)
name = "EventHubArchive.AzureBlockBlob"
archive_name_format = "{Namespace}/{EventHub}/{PartitionId}/{Year}-{Month}-{Day}T{Hour}:{Minute}:{Second}"
blob_container_name = var.containers_list[count.index].name
storage_account_id = azurerm_storage_account.storageaccount.id
}
}
}
我对terraform语法很陌生,有人能在这个场景中提供帮助吗?如何在terraform子资源中编写forloop?
尝试了动态for_each块,但是遇到了新的错误。我试着用正确的语法吗?
variable "capture" {
type = map(object({
name = string
archive_container_name = string
blob_container_name = string
storage_account_id = string
}))
default = {
"container1" = {
name = "EventHubArchive.AzureBlockBlob"
archive_container_name = "{Namespace}/{EventHub}/{PartitionId}/{Year}-{Month}-{Day}T{Hour}:{Minute}:{Second}"
blob_container_name = "eus-s-test-logs-bkp"
storage_account_id = "azurerm_storage_account.storageaccount.id"
}
"container2" = {
name = "EventHubArchive.AzureBlockBlob"
archive_container_name = "{Namespace}/{EventHub}/{PartitionId}/{Year}-{Month}-{Day}T{Hour}:{Minute}:{Second}"
blob_container_name = "insights-logs-auditevent"
storage_account_id = "azurerm_storage_account.storageaccount.id"
}
"container3" = {
name = "EventHubArchive.AzureBlockBlob"
archive_container_name = "{Namespace}/{EventHub}/{PartitionId}/{Year}-{Month}-{Day}T{Hour}:{Minute}:{Second}"
blob_container_name = "insights-logs-dataplanerequests"
storage_account_id = "azurerm_storage_account.storageaccount.id"
}
}
}
resource "azurerm_eventhub" "test-eventhubs" {
count = length(var.eventhubs)
name = var.eventhubs[count.index]
namespace_name = azurerm_eventhub_namespace.test-ens.name
resource_group_name = azurerm_resource_group.test-rg.name
partition_count = 4
message_retention = 1
capture_description {
enabled = true
encoding = "Avro"
dynamic "destination" {
for_each = var.capture
content {
name = destination.value["name"]
archive_name_format = destination.value["archive_container_name"]
blob_container_name = destination.value["blob_container_name"]
storage_account_id = destination.value["storage_account_id"]
}
}
}
}
错误:
terraform plan
╷
│ Error: Too many destination blocks
│
│ on eventhub.tf line 121, in resource "azurerm_eventhub" "test-eventhubs":
│ 121: content {
│
│ No more than 1 "destination" blocks are allowed
╵
╷
│ Error: Too many destination blocks
│
│ on eventhub.tf line 121, in resource "azurerm_eventhub" "test-eventhubs":
│ 121: content {
│
│ No more than 1 "destination" blocks are allowed
╵
╷
│ Error: Too many destination blocks
│
│ on eventhub.tf line 121, in resource "azurerm_eventhub" "test-eventhubs":
│ 121: content {
│
│ No more than 1 "destination" blocks are allowed
发布于 2022-11-24 21:57:47
在我看来,带有动态块的azurerm_eventhub不能工作,通常我们使用动态块是因为您试图部署的资源在嵌套块中具有配置。azurerm_eventhub没有。您必须创建多个azurerm_eventhub.实例,而不是使用动态。
用于动态使用的示例代码
Step1:代替变量,将其替换为本地
locals{
containers_list = [
{ name = "eus-a-test-logs-bkp", access_type = "private" },
{ name = "insights-logs-auditevent", access_type = "private" },
{ name = "insights-logs-dataplanerequests", access_type = "private" }
]
}
Step2:编写动态块,如下所示
dynamic "SwarnaDemo" {
for_each = local.containers_list
content {
label = SwarnaDemo.value.name
size = SwarnaDemo.value.access_type
}
}
希望能帮上忙!
https://stackoverflow.com/questions/74560290
复制