main.tf
terraform {
required_providers {
azurerm = {
source = “hashicorp/azurerm”
version = “>=2.91”
}
}
required_version = “>= 0.13”
}
data “azurerm_resource_group” “current” {
name = var.resource_group
}
resource “azurerm_servicebus_namespace” “default” {
name = var.resource_name
location = data.azurerm_resource_group.current.location
resource_group_name = data.azurerm_resource_group.current.name
sku = var.sku
capacity = var.capacity
tags = var.resource_tags
lifecycle {
ignore_changes = [
tags,
location
]
}
}
resource “azurerm_servicebus_queue” “default” {
for_each = toset(var.queues)
name = each.value
namespace_id = azurerm_servicebus_namespace.default.id
enable_partitioning = var.enable_partitioning
}
resource “azurerm_servicebus_topic” “default” {
for_each = toset(var.topics)
name = each.value
namespace_id = azurerm_servicebus_namespace.default.id
enable_partitioning = var.enable_partitioning
}
resource “azurerm_servicebus_subscription” “default” {
for_each=toset(var.servicebus_subscription[])
name = each.value
topic_id = azurerm_servicebus_topic.default[each.value[]].id
max_delivery_count = var.max_delivery_count
depends_on = [
azurerm_servicebus_topic.default
]
}
variables.tf
subscription_id = “test”
resource_group = “test”
resource_name = “catestbus01”
resource_tags = {
Key1 = “Value1”
Key2 = “Value2”
}
sku = “Premium”
topics = [“tf1”]
queues = [“test”]
ip_rules =
virtual_network_subnet_ids =
servicebus_subscription=[“tf”]
Unable to create service bus subscription and getting below error.
** Error: Invalid index
│
│ on …/…/main.tf line 55, in resource “azurerm_servicebus_subscription” “default”:
│ 55: topic_id = azurerm_servicebus_topic.default[each.value].id
│ ├────────────────
│ │ azurerm_servicebus_topic.default is object with 1 attribute “tf1”
│ │ each.value is “tf”
│
│ The given key does not identify an element in this collection value