skip to Main Content

I got the Terraform error, the details is as following

variables.tf:

variable "acr_registry_allowed_ip_ranges" {
  description = "List of IP CIDR ranges to allow access to the Azure Container Registry."
  type        = list(string)
  default     = ["203.0.113.5", "15.230.15.29/32"] # Replace with your list of IP CIDR ranges
}

acr_contrainer.tf:

resource "azurerm_container_registry" "main" {
  name                          = local.registry_name
  resource_group_name           = azurerm_resource_group.main.name
  location                      = azurerm_resource_group.main.location
  sku                           = "Premium"
  admin_enabled                 = true
  public_network_access_enabled = true

  tags = local.default_tags
  
  network_rule_set {
    default_action = "Deny"

    dynamic "ip_rule" {
      for_each = var.acr_registry_allowed_ip_ranges
      content {
        action   = "Allow"
        ip_range = ip_rule.value
      }
    }
  }
}

Got error:

│ Error: Unknown variable
│ 
│   on container_registry.tf line 16, in resource "azurerm_container_registry" "main":
│   16:       for_each = var.acr_registry_allowed_ip_ranges
│ 
│ There is no variable named "var".
╵

2

Answers


  1. Unknown variable issue while referencing IP CIDR ranges to container registry using terraform

    The issue seems to be with the way youre mactching network_rule_set and ip_rule in the configuration in a simple way ip_rule inside network_rule_set could not be dynamically configured using the dynamic block.

    As per the github when ip_rule attribute as a list of objects using a for loop, directlly defining the allowed IP rules without relying on a dynamic block whcih over comes the blocker you faced using ip_rule .

    Demo configuration:

    variable "acr_registry_allowed_ip_ranges" {
      description = "List of IP CIDR ranges to allow access to the Azure Container Registry."
      type        = list(string)
      default     = ["203.0.113.5/32", "15.230.15.29/32"] 
    }
    
    resource "azurerm_container_registry" "main" {
      name                          = "vinayacr143" 
      resource_group_name           = azurerm_resource_group.rg.name
      location                      = azurerm_resource_group.rg.location
      sku                           = "Premium"
      admin_enabled                 = true
      public_network_access_enabled = true
    
      network_rule_set {
        default_action = "Deny"
    
        ip_rule = [
          for ip in var.acr_registry_allowed_ip_ranges : {
            action   = "Allow"
            ip_range = ip
          }
        ]
      }
    }
    

    Deployment:

    enter image description here

    enter image description here

    Refer:

    Dynamic block with for_each within network_rule_set inside azurerm_container_registry won’t work · Issue #20721 · hashicorp/terraform-provider-azurerm · GitHub

    Terraform – Simplified Azure Container Registry (ACR) Deployment · Jorge Bernhardt

    Azure terraform module for container registry – dynamic block doesn’t remove IP addresses when emptying white listed IP list to complete zero – Stack Overflow

    Login or Signup to reply.
  2. This might not work. I don’t have access to Azure, to test before posting. We only use AWS and GCP. But this is the dynamic block syntax I usually use.

    resource "azurerm_container_registry" "main" {
      name                          = local.registry_name
      resource_group_name           = azurerm_resource_group.main.name
      location                      = azurerm_resource_group.main.location
      sku                           = "Premium"
      admin_enabled                 = true
      public_network_access_enabled = true
    
      tags = local.default_tags
      
      network_rule_set {
        default_action = "Deny"
        
        for_each = var.acr_registry_allowed_ip_ranges
        
        dynamic "ip_rule" {
          for_each = each.value.ip_rule
          content {
            action   = "Allow"
            ip_range = ip_rule.value.default
          }
        }
      }
    }
    

    So, you want to put the for_each that goes through your variable outside dynamic block, and inside, your for_each would be each.value.DYNAMIC_BLOCK_NAME and in the content block you would refer to each value as DYNAMIC_BLOCK_NAME.value.EACH_PARAMETER_KEY

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search