title: for_each on terraform with Azure Load Balancer
body: <p>I am trying to create an Azure Load Balancer to build multiple front end ip in the az load balancer that leverages public ips for azure availability zones:</p>
  <p>This is my file structure :</p>
  <pre><code>terraform-azurem-loadbalncer/
  ┣ locals.tf
  ┣ output.tf
  ┣ tlz-lb.tf
  ┣ tlz-pip.tf
  ┗ variables.tf
  </code></pre>
  <p>What I am trying to do is quite like the post <a href="https://stackoverflow.com/questions/58907705/how-to-use-terraform-dynamic-block-to-build-multiple-front-end-ip-in-the-az-load">over here</a>:
  This is my tlz-pip.tf</p>
  <pre><code>resource &quot;azurerm_public_ip&quot; &quot;tlz_public_ip&quot; {
    name                    = &quot;${local.prefix}-${local.resource_type}-${var.pip_name}-${var.environment}&quot;
    resource_group_name     = var.resource_group_name
    location                = var.location
    allocation_method       = var.allocation_method
    sku                     = var.pip_sku
    ip_version              = var.ip_version
    idle_timeout_in_minutes = try(var.idle_timeout_in_minutes, 30)
    domain_name_label       = var.generate_domain_name_label ? &quot;${local.prefix}-${local.resource_type}-${var.pip_name}-${var.environment}&quot; : var.domain_name_label
    reverse_fqdn            = try(var.reverse_fqdn, null)
    zones                   = try(var.zones, null)
    tags = merge(
      {
        &quot;Name&quot; = &quot;${local.prefix}-${local.resource_type}-${var.pip_name}-${var.environment}&quot;
      },
      var.tags
    )
  }
  </code></pre>
  <p>This is my tlz-lb.tf:</p>
  <pre><code>resource &quot;azurerm_lb&quot; &quot;tlz-lb-navigator-dev&quot; {
    name                = &quot;${local.prefix}-${local.resource_type}-${var.environment}-${var.location}&quot;
    location            = var.location
    resource_group_name = var.resource_group_name
    sku                 = var.lb_sku
  
    frontend_ip_configuration {
      name                 = var.frontend_ip_name
      public_ip_address_id = azurerm_public_ip.tlz_public_ip.id
    }
  
    dynamic &quot;frontend_ip_configuration&quot; {
       for_each = azurerm_public_ip.tlz_public_ip
       content {
         name                 = &quot;config_${each.value.name}&quot; 
         public_ip_address_id = each.value.id
      }
    }
  }
  resource &quot;azurerm_lb_backend_address_pool&quot; &quot;tlz-lb-backendpool-dev&quot; {
    name            = var.backend_address_pool
    loadbalancer_id = azurerm_lb.tlz-lb-navigator-dev.id
  }
  resource &quot;azurerm_lb_rule&quot; &quot;tlz-lb-rule&quot; {
    resource_group_name            = var.resource_group_name
    loadbalancer_id                = azurerm_lb.tlz-lb-navigator-dev.id
    name                           = var.load_balancing_rule
    protocol                       = &quot;Tcp&quot;
    frontend_port                  = 443
    backend_port                   = 443
    frontend_ip_configuration_name = var.frontend_ip_name
  }
  resource &quot;azurerm_lb_nat_rule&quot; &quot;tlz-nat-rule&quot; {
    count                          = var.nblinuxvms
    resource_group_name            = var.resource_group_name
    loadbalancer_id                = azurerm_lb.tlz-lb-navigator-dev.id
    name                           = var.nat_rule_name
    protocol                       = &quot;Tcp&quot;
    frontend_port                  = 80
    backend_port                   = 80
    frontend_ip_configuration_name = &quot;config_${azurerm_public_ip[count.index].tlz_public_ip.name}&quot;
  }
  resource &quot;azurerm_lb_probe&quot; &quot;tlz-lb-probe&quot; {
    resource_group_name = var.resource_group_name
    loadbalancer_id     = azurerm_lb.tlz-lb-navigator-dev.id
    name                = var.health_probe_name
    port                = 22
  }
  </code></pre>
  <p>And this is my variables.tf:</p>
  <pre><code>#Public IP configuration
  variable &quot;location&quot; {
    description = &quot;(Required) The location/region where the virtual network is created&quot;
    default     = &quot;centralus&quot;
  }
  variable &quot;environment&quot; {
    description = &quot;(Required) The environment platform in which resources will be deployed.&quot;
    default     = &quot;stage&quot;
  }
  variable &quot;public_ip_address_id&quot; {
    default     = &quot;&quot;
    description = &quot;public ip address id&quot;
  }
  variable &quot;resource_group_name&quot; {
  
    description = &quot;resource group name&quot;
  }
  variable &quot;tags&quot; {
    description = &quot;(Required) Map of tags to be applied to the resource&quot;
    type        = map(any)
  }
  variable &quot;pip_name&quot; {
    description = &quot;(Required) The name for public ip address.&quot;
  }
  variable &quot;allocation_method&quot; {
    default     = &quot;Dynamic&quot;
    description = &quot;(Required) Defines the allocation method for this IP address. Possible values are Static or Dynamic.&quot;
  }
  variable &quot;ip_version&quot; {
    description = &quot;The IP Version to use, IPv6 or IPv4.&quot;
    default     = &quot;IPv4&quot;
  }
  variable &quot;idle_timeout_in_minutes&quot; {
    description = &quot;Specifies the timeout for the TCP idle connection. The value can be set between 4 and 30 minutes.&quot;
    default     = 30
  }
  variable &quot;generate_domain_name_label&quot; {
    description = &quot;The flag to control creation of domain label.&quot;
    default     = false
  }
  variable &quot;domain_name_label&quot; {
    description = &quot;If a domain name label is specified, an A DNS record is created for the public IP in the Microsoft Azure DNS system.&quot;
    default     = null
  }
  variable &quot;reverse_fqdn&quot; {
    description = &quot; A fully qualified domain name that resolves to this public IP address.&quot;
    default     = &quot;&quot;
  }
  variable &quot;zones&quot; {
    description = &quot;A collection containing the availability zone to allocate the Public IP in.&quot;
    default     = null
  }
  
  
  #Load Balancer
  
  variable &quot;lb_sku&quot; {
    type        = string
    default     = &quot;Basic&quot;
    description = &quot;(Optional) The SKU of the Azure Load Balancer. Accepted values are Basic and Standard. Defaults to Basic.&quot;
  }
  variable &quot;pip_sku&quot; {
    type        = string
    default     = &quot;Basic&quot;
    description = &quot;(Optional) The SKU of the Public IP. Accepted values are Basic and Standard. Defaults to Basic.&quot;
  }
  variable &quot;frontend_ip_name&quot; {
    type        = string
    default     = &quot;&quot;
    description = &quot;(Required) Specifies the name of the frontend ip configuration.&quot;
  }
  variable &quot;backend_address_pool&quot; {
    type        = string
    default     = &quot;&quot;
    description = &quot;(Required) Specifies the name of the Backend Address Pool&quot;
  }
  variable &quot;load_balancing_rule&quot; {
    type        = string
    default     = &quot;&quot;
    description = &quot;(Required) Specifies the name of the LB Rule.&quot;
  }
  variable &quot;nat_rule_name&quot; {
    type        = string
    default     = &quot;&quot;
    description = &quot;(Required) Specifies the name of the NAT Rule.&quot;
  }
  variable &quot;health_probe_name&quot; {
    type        = string
    default     = &quot;&quot;
    description = &quot;(Required) Specifies the name of the Probe.&quot;
  }
  variable &quot;nblinuxvms&quot; {
    type        = number
    default     = &quot;2&quot;
    description = &quot;NUmber of VMs to be attached.&quot;
  }
  </code></pre>
  <p>This is the error I am getting for tlz-lb.tf:
  Error: Invalid reference</p>
  <pre><code>  on FARMERS-TLZ-TFE-PMR/terraform-azurerm-stage-lb/terraform-azurem-loadbalncer/tlz-lb.tf line 41, in resource &quot;azurerm_lb_nat_rule&quot; &quot;tlz-nat-rule&quot;:
    41:   frontend_ip_configuration_name = &quot;config_${azurerm_public_ip[count.index].name}&quot;
  
  A reference to a resource type must be followed by at least one attribute
  access, specifying the resource name.
  </code></pre>
  <p>I am not sure what might be causing this error since I have followed the post mentioned before, please help me out. Thanks.</p>