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
The issue seems to be with the way youre mactching
network_rule_set
andip_rule
in the configuration in a simple wayip_rule
insidenetwork_rule_set
could not be dynamically configured using thedynamic
block.As per the github when
ip_rule
attribute as a list of objects using afor
loop, directlly defining the allowed IP rules without relying on adynamic
block whcih over comes the blocker you faced usingip_rule
.Demo configuration:
Deployment:
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
This might not work. I don’t have access to
Azure
, to test before posting. We only useAWS
andGCP
. But this is thedynamic
block syntax I usually use.So, you want to put the
for_each
that goes through your variable outsidedynamic
block, and inside, yourfor_each
would beeach.value.DYNAMIC_BLOCK_NAME
and in thecontent
block you would refer to each value asDYNAMIC_BLOCK_NAME.value.EACH_PARAMETER_KEY