Hi,
I’m changing over to use for_each instead of Count and one of the reasons was for_each doesn’t delete other resources. I’m still getting subnets deleted and recreated when I delete one of my subnets in my map. Please see the code below.
variables.tf
variable “location” {
type = string
default = “UKSouth”
description = “Used to specifiy the location in Azure e.g. UKSouth”
}variable “resource_group_name” {
type = list(string)
description = “The name of the Resource Group which will contain Management Resources for the Enterprise”
default = [“AAATestOne”, “AAATestTwo”]
}variable “subnets” {
type = map(string)
default = {
“subnet1” = “10.1.1.0/24”
“subnet2” = “10.1.2.0/24”
“external” = “10.1.0.0/24”
}
}
main.tf
resource “azurerm_resource_group” “main” {
for_each = toset(var.resource_group_name)
name = each.value
location = var.location
}output “resourcegroups” {
value = azurerm_resource_group.main
}resource “azurerm_virtual_network” “main” {
name = “test-network”
resource_group_name = azurerm_resource_group.main[“AAATestOne”].name
address_space = [“10.1.0.0/16”]
location = “uksouth”dynamic “subnet” {
for_each = var.subnetscontent { name = subnet.key address_prefix = subnet.value }
}
}output “sub” {
value = azurerm_virtual_network.main
}
If I remove one of the subnets out of the subnet variable, say “external” = “10.1.0.0/24” then when I run terraform plan/apply terraform wants to delete and recreate the other two subnets??
Can someone give me some guidance I thought for_each stopped this issue?
Thanks
Russell