0

I added a piece of code to support enable/disable to the feature - I used the count. Now I have the following problem:

ERROR:
on modules/eventhub/main.tf line 110, in resource "azurerm_eventhub" "events":

 110:   namespace_name      = azurerm_eventhub_namespace.eventhub.*.name
    |----------------
    | azurerm_eventhub_namespace.eventhub is tuple with 1 element

Inappropriate value for attribute "namespace_name": string required.

# Create EventHub namespace
resource "azurerm_eventhub_namespace" "eventhub" {
  count               = var.enable_eh ? 1 : 0
  name                  = "${var.name}-ns"
  location              = var.location
  resource_group_name   = var.resource_group
  sku                   = var.sku
  capacity              = var.capacity
  dedicated_cluster_id = azurerm_eventhub_cluster.eventhub[count.index].id #for eventHub Cluster
  auto_inflate_enabled     = var.auto_inflate != null ? var.auto_inflate.enabled : null
  maximum_throughput_units = var.auto_inflate != null ? var.auto_inflate.maximum_throughput_units : null

    dynamic "network_rulesets" {
    for_each = var.network_rules != null ? ["true"] : []
    content {
      default_action = "Deny"
      dynamic "ip_rule" {
        for_each = var.network_rules.ip_rules
        iterator = iprule
        content {
          ip_mask = iprule.value
        }
      }

      dynamic "virtual_network_rule" {
        for_each = var.network_rules.subnet_ids
        iterator = subnet
        content {
          subnet_id = subnet.value
        }
      }
    }
  }

  tags = var.tags
}

resource "azurerm_eventhub_namespace_authorization_rule" "events" {
  for_each            = local.authorization_rules
  name                = each.key
  namespace_name      = azurerm_eventhub_namespace.eventhub.*.name
  resource_group_name = var.resource_group

  listen = each.value.listen
  send   = each.value.send
  manage = each.value.manage

}

resource "azurerm_eventhub" "events" {
  for_each            = local.hubs
  name                = each.key
  **namespace_name      = azurerm_eventhub_namespace.eventhub.*.name  <------ problematic line**

  resource_group_name = var.resource_group
  partition_count     = each.value.partitions
  message_retention   = each.value.message_retention

}

I don't know what the issue is.

1
  • Call using array
    – again
    Feb 12 at 21:29
1

I believe you should be able to reference the namespace name like so:

namespace_name = azurerm_eventhub_namespace.eventhub[0].name

However, if the namespace_name field is a required parameter on the resources you are passing it to, you might run into other issues while you are trying to set enabled to false, as the resources depend on the output of the resource you are setting to count[0]. If you want them all to be conditional on the creation of the namespace, i would recommend continuing down your approach with the for_each over the local values.

If you need to the other resources to not be created unless the namespace is created you can create a condition on your local value like this (assuming map):

locals{
    hub = var.enable_eh ? {foo = "bar", baz = "qux"} : {}
}

The for_each will omit the resources that are empty.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.