Answers: <p>A dynamic block does not use the <code>each</code> keyword, but the value of the <code>iterator</code> argument instead. If omitted, it will use the name of the dynamic block instead. Or as <a href="https://developer.hashicorp.com/terraform/language/expressions/dynamic-blocks" rel="nofollow noreferrer">the documentation</a> states it:</p>
  <blockquote>
  <p>The <code>iterator</code> argument (optional) sets the name of a temporary variable that represents the current element of the complex value. If omitted, the name of the variable defaults to the label of the <code>dynamic</code> block.</p>
  </blockquote>
  <p>It's generally fine to omit the iterator argument and just go with the (what should be a) unique name of the block. In your case, replace <code>each.value</code> with <code>virtual_network_rule.value</code>, so you get:</p>
  <pre><code>dynamic &quot;virtual_network_rule&quot; {
    for_each = var.virtual_network_subnet_ids
  
    content {
      id = virtual_network_rule.value
    }
  }
  </code></pre>
  <p>If you do want to use the <code>iterator</code> argument, it would look something like this:</p>
  <pre><code>dynamic &quot;virtual_network_rule&quot; {
    for_each = var.virtual_network_subnet_ids
    iterator = &quot;rule&quot;
  
    content {
      id = rule.value
    }
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2022-11-15T01:01:24.053Z
Answers: 2022-11-15T01:01:24.053Z
Answers: Oldskool
Body: <p>I'm trying to create a set of subnet rules for a cosmosdb account.</p>
  <p>I've tried a dynamic and a for_each on the resource and I get the same error each time - am I going about this the wrong way?</p>
  <p>Currently trying this:</p>
  <pre><code>resource &quot;azurerm_cosmosdb_account&quot; &quot;db&quot; {
    name                = local.name
    location            = var.location
    resource_group_name = var.resource_group_name
  
    offer_type       = &quot;Standard&quot; #(Required) Specifies the Offer Type to use for this CosmosDB Account - currently this can only be set to Standard.
    enable_free_tier = var.enable_free_tier
    kind             = var.kind
  
    identity {
      type = &quot;SystemAssigned&quot;
    }
  
    ip_range_filter = join(&quot;,&quot;, local.network_rules.ip_rules)
  
    dynamic &quot;virtual_network_rule&quot; {
      for_each = var.virtual_network_subnet_ids
      content {
        id = each.value
      }
    }
  }
  </code></pre>
  <p>Currently getting:</p>
  <pre><code>Error: each.value cannot be used in this context
  │ 
  │   on .terraform/modules/azurerm_cosmosdb_account/azurerm_cosmosdb_account/main.tf line 27, in resource &quot;azurerm_cosmosdb_account&quot; &quot;db&quot;:
  │   27:       id = each.value
  │ 
  │ A reference to &quot;each.value&quot; has been used in a context in which it
  │ unavailable, such as when the configuration no longer contains the value in
  │ its &quot;for_each&quot; expression. Remove this reference to each.value in your
  │ configuration to work around this error.
  ╵
  </code></pre>
  
Tags: <terraform><azure>
Title: cosmosdb_account virtual_network_rule for_each
UserDisplayName: Stuart.Sklinar
ThreadText: # cosmosdb_account virtual_network_rule for_each
  
  (Asked by: username_0 on 2022-09-30)
  
  I'm trying to create a set of subnet rules for a cosmosdb account.
  
  I've tried a dynamic and a for\_each on the resource and I get the same error each time - am I going about this the wrong way?
  
  Currently trying this:
  
  ```
  resource "azurerm_cosmosdb_account" "db" {
    name                = local.name
    location            = var.location
    resource_group_name = var.resource_group_name
  
    offer_type       = "Standard" #(Required) Specifies the Offer Type to use for this CosmosDB Account - currently this can only be set to Standard.
    enable_free_tier = var.enable_free_tier
    kind             = var.kind
  
    identity {
      type = "SystemAssigned"
    }
  
    ip_range_filter = join(",", local.network_rules.ip_rules)
  
    dynamic "virtual_network_rule" {
      for_each = var.virtual_network_subnet_ids
      content {
        id = each.value
      }
    }
  }
  
  ```
  
  Currently getting:
  
  ```
  Error: each.value cannot be used in this context
  │ 
  │   on .terraform/modules/azurerm_cosmosdb_account/azurerm_cosmosdb_account/main.tf line 27, in resource "azurerm_cosmosdb_account" "db":
  │   27:       id = each.value
  │ 
  │ A reference to "each.value" has been used in a context in which it
  │ unavailable, such as when the configuration no longer contains the value in
  │ its "for_each" expression. Remove this reference to each.value in your
  │ configuration to work around this error.
  ╵
  
  ```
  
  **Tags:** terraform, azure
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2022-11-15. Score: 1 | Accepted answer
  
  A dynamic block does not use the `each` keyword, but the value of the `iterator` argument instead. If omitted, it will use the name of the dynamic block instead. Or as [the documentation](https://developer.hashicorp.com/terraform/language/expressions/dynamic-blocks) states it:
  
  > The `iterator` argument (optional) sets the name of a temporary variable that represents the current element of the complex value. If omitted, the name of the variable defaults to the label of the `dynamic` block.
  
  It's generally fine to omit the iterator argument and just go with the (what should be a) unique name of the block. In your case, replace `each.value` with `virtual_network_rule.value`, so you get:
  
  ```
  dynamic "virtual_network_rule" {
    for_each = var.virtual_network_subnet_ids
  
    content {
      id = virtual_network_rule.value
    }
  }
  
  ```
  
  If you do want to use the `iterator` argument, it would look something like this:
  
  ```
  dynamic "virtual_network_rule" {
    for_each = var.virtual_network_subnet_ids
    iterator = "rule"
  
    content {
      id = rule.value
    }
  }
  
  ```
  
  
  ---
  
  