<p>I believe you should be able to reference the namespace name like so:</p>
<p><code>namespace_name      = azurerm_eventhub_namespace.eventhub[0].name</code></p>
<p>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.</p>
<p>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):</p>
<pre><code>locals{
    hub = var.enable_eh ? {foo = &quot;bar&quot;, baz = &quot;qux&quot;} : {}
}
</code></pre>
<p>The for_each will omit the resources that are empty.</p>

CC BY-SA 4.0
2021-02-13T19:21:13.823Z
2021-02-13T19:21:13.823Z
zm31
<p>I think you should migrate from count to for_each, here is a wider context and howto:</p>
<p>Terraform provides a 'for_each' iterator (aka set/map or key named list) and a 'count' iterator (aka indexed list with strict ordering) which allows you to loop over elements of a list and perform multi resource deployments.
What many people fail to realize is that when using indexed list with the 'count' iterator, any change to the list ordering (like removing an item) causes replacement of all items in the list, those can be important cloud resources  like databases, instances, buckets etc.</p>
<p>High level overview here:
<a href="https://www.hashicorp.com/blog/new-terraform-tutorials-on-count-and-for-each" rel="nofollow noreferrer">https://www.hashicorp.com/blog/new-terraform-tutorials-on-count-and-for-each</a></p>
<p>With ‘count’, even when one of the list items needs just reconfiguration of substantial attributes, terraform might plan to destroy and/or replace the entire list of resources, making this update a clear 'no-go' in production environments.
Those resources would have better support (yet not perfect) for updating its items, if the list would have been implemented using the 'for-each' iterator, simply because the difference between a simple indexed list and a set or a map is that the item names (keys) are all guaranteed to be unique, and also do not have any particular ordering.</p>
<p>But many times, for various reasons, one would already implement a 'count' iterator in production and face the need to migrate it later to 'for each' iterator without destroying any item and while keeping terraform states clean and synchronized with the cloud.</p>
<p>here is a stable workaround for anyone facing such requirement, i think this one is not well documented yet.</p>
<p>terraform init -var-file=./tfvars/.tfvars -reconfigure
terraform state list → to grab the count list items</p>
<p>Change ‘count’ instructions to ‘for-each’ instructions (example):</p>
<p>resource &quot;google_container_node_pool&quot; &quot;nodes&quot; {</p>
<ul>
<li>count                = length(var.node_pools)</li>
</ul>
<ul>
<li>for_each           = {for i, v in var.node_pools:  i =&gt; v}</li>
</ul>
<ul>
<li>initial_node_count = var.node_pools[count.index].initial_node_count</li>
</ul>
<ul>
<li>initial_node_count = each.value.initial_node_count</li>
</ul>
<p>etc ….</p>
<p>Now, obviously at this point the terraform plan would show it's intention to destroy and re-create everything under that list of resources.</p>
<p>terraform plan -var-file=./tfvars/.tfvars  →  not looking good</p>
<p>Here is the golden transformation steps (example):</p>
<p>terraform state mv 'google_container_node_pool.nodes[0]' 'google_container_node_pool.nodes[&quot;0&quot;]'</p>
<p>In this example we manually, yet safely, transformed one of the items from numbered (indexed) list to named item in the list, keeping the old ordering as needed (although will not be important post transformation it is important during transformation).</p>
<p>After doing the above on all items in the list you can initiate plan again and watch the results:</p>
<p>terraform plan -var-file=./tfvars/.tfvars  →  “infrastructure matches the configuration”</p>
<p>Now you can safely commit, merge and apply your new terraform infrastructure code and continue working with a new key/named list, with the ‘for-each’ iterator handling item updates much better (although - nor perfect)</p>

CC BY-SA 4.0
2023-01-07T22:15:42.933Z
2023-01-07T22:15:42.933Z
Koren Lev
<p>I added a piece of code to support enable/disable to the feature - I used the <code>count</code>. Now I have the following problem:</p>
<pre><code>ERROR:
on modules/eventhub/main.tf line 110, in resource &quot;azurerm_eventhub&quot; &quot;events&quot;:

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

Inappropriate value for attribute &quot;namespace_name&quot;: string required.

# Create EventHub namespace
resource &quot;azurerm_eventhub_namespace&quot; &quot;eventhub&quot; {
  count               = var.enable_eh ? 1 : 0
  name                  = &quot;${var.name}-ns&quot;
  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 &quot;network_rulesets&quot; {
    for_each = var.network_rules != null ? [&quot;true&quot;] : []
    content {
      default_action = &quot;Deny&quot;
      dynamic &quot;ip_rule&quot; {
        for_each = var.network_rules.ip_rules
        iterator = iprule
        content {
          ip_mask = iprule.value
        }
      }

      dynamic &quot;virtual_network_rule&quot; {
        for_each = var.network_rules.subnet_ids
        iterator = subnet
        content {
          subnet_id = subnet.value
        }
      }
    }
  }

  tags = var.tags
}

resource &quot;azurerm_eventhub_namespace_authorization_rule&quot; &quot;events&quot; {
  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 &quot;azurerm_eventhub&quot; &quot;events&quot; {
  for_each            = local.hubs
  name                = each.key
  **namespace_name      = azurerm_eventhub_namespace.eventhub.*.name  &lt;------ problematic line**

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

}
</code></pre>
<p>I don't know what the issue is.</p>

CC BY-SA 4.0
2021-02-12T21:29:29.173Z
Call using array
again
<terraform><azure><cloud><terraform-module><variables>
Enable/disable resource in terraform using count
Nava Bar
# Enable/disable resource in terraform using count

(Asked by: username_0 on 2021-02-09)

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.

**Tags:** terraform, azure, cloud, terraform-module, variables

> **Comments:**
> **username_1**: Call using array  

---

## 2 answers

### Answer by username_2 on 2021-02-13. Score: 1 | Accepted answer

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.


---

### Answer by username_3 on 2023-01-07. Score: 0

I think you should migrate from count to for\_each, here is a wider context and howto:

Terraform provides a 'for\_each' iterator (aka set/map or key named list) and a 'count' iterator (aka indexed list with strict ordering) which allows you to loop over elements of a list and perform multi resource deployments.
What many people fail to realize is that when using indexed list with the 'count' iterator, any change to the list ordering (like removing an item) causes replacement of all items in the list, those can be important cloud resources like databases, instances, buckets etc.

High level overview here:
<https://www.hashicorp.com/blog/new-terraform-tutorials-on-count-and-for-each>

With ‘count’, even when one of the list items needs just reconfiguration of substantial attributes, terraform might plan to destroy and/or replace the entire list of resources, making this update a clear 'no-go' in production environments.
Those resources would have better support (yet not perfect) for updating its items, if the list would have been implemented using the 'for-each' iterator, simply because the difference between a simple indexed list and a set or a map is that the item names (keys) are all guaranteed to be unique, and also do not have any particular ordering.

But many times, for various reasons, one would already implement a 'count' iterator in production and face the need to migrate it later to 'for each' iterator without destroying any item and while keeping terraform states clean and synchronized with the cloud.

here is a stable workaround for anyone facing such requirement, i think this one is not well documented yet.

terraform init -var-file=./tfvars/.tfvars -reconfigure
terraform state list → to grab the count list items

Change ‘count’ instructions to ‘for-each’ instructions (example):

resource "google\_container\_node\_pool" "nodes" {

* count = length(var.node\_pools)

* for\_each = {for i, v in var.node\_pools: i => v}

* initial\_node\_count = var.node\_pools[count.index].initial\_node\_count

* initial\_node\_count = each.value.initial\_node\_count

etc ….

Now, obviously at this point the terraform plan would show it's intention to destroy and re-create everything under that list of resources.

terraform plan -var-file=./tfvars/.tfvars → not looking good

Here is the golden transformation steps (example):

terraform state mv 'google\_container\_node\_pool.nodes[0]' 'google\_container\_node\_pool.nodes["0"]'

In this example we manually, yet safely, transformed one of the items from numbered (indexed) list to named item in the list, keeping the old ordering as needed (although will not be important post transformation it is important during transformation).

After doing the above on all items in the list you can initiate plan username_1 and watch the results:

terraform plan -var-file=./tfvars/.tfvars → “infrastructure matches the configuration”

Now you can safely commit, merge and apply your new terraform infrastructure code and continue working with a new key/named list, with the ‘for-each’ iterator handling item updates much better (although - nor perfect)


---

