I am trying to loop the container names using count in the Azure Event Hub in Azure Event Hub namespace. however I am getting to below error while terraform plan.
Below is the Terraform code
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
}
}
}
I am new to terraform syntax could someone help on this scenario ? How to write a forloop inside the terraform sub resource ?
Tried dynamic for_each block but hitting with new error. Am I trying with right syntax ?
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"]
}
}
}
}
Error :
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
In my view azurerm_eventhub with dynamic block won't work, usually we use dynamic blocks if and only if the resource you are trying to deploy has configuration within nested blocks. azurerm_eventhub does not. Instead of using dynamic, you must create multiple instances of azurerm_eventhub.
Sample code for dynamic usage
Step1: Instead of variable replace it as local
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: Write Dynamic block as follows
dynamic "SwarnaDemo" {
for_each = local.containers_list
content {
label = SwarnaDemo.value.name
size = SwarnaDemo.value.access_type
}
}
Hope it's helpful!