Dynamic block content update

When there is an update to the data map which is used in dynamic block, the next apply is doing the in-place update for the whole data map instead of the only change.

From this example https://www.hashicorp.com/blog/hashicorp-terraform-0-12-preview-for-and-for-each/

Create a vnet with dynamic subnet block. Then add an entry to subnet variable and do plan/apply to observe removal of all subnets and recreate.

Hi @sraddhananda,

How updates to blocks of a particular type are interpreted is up to the provider, depending on how it internally represents those blocks. You didn’t include any example output showing what you’re seeing so it’s hard for me to know exactly what’s going on for you, but if you can share an example output from terraform plan, ideally also with the configuration that produced it, I might be able to explain it more specifically.

Hi @apparentlymart,

I took the example under dynamic nested blocks from https://www.hashicorp.com/blog/hashicorp-terraform-0-12-preview-for-and-for-each/

I created a Vnet using the below code.

variable "subnets" {
  default = [
    {
      name   = "a"
      number = 1
    },
    {
      name   = "b"
      number = 2
    },
    {
      name   = "c"
      number = 3
    },
  ]
}

locals {
  base_cidr_block = "24.2.0.0/26"
}

resource "azurerm_virtual_network" "example" {
  name                = "dynamictest"
  resource_group_name = "sradtest4"
  address_space       = ["24.2.0.0/26"]
  location            = "northeurope"

  dynamic "subnet" {
    for_each = [for s in var.subnets: {
      name   = s.name
      prefix = cidrsubnet(local.base_cidr_block, 2, s.number)
    }]

    content {
      name           = subnet.value.name
      address_prefix = subnet.value.prefix
    }
  }
}

image

Then I added an entry in subnets variable to create one more subnet.

variable "subnets" {
  default = [
    {
      name   = "a"
      number = 1
    },
    {
      name   = "b"
      number = 2
    },
    {
      name   = "c"
      number = 3
    },
    {
      name   = "d"
      number = 3
    },
  ]
}

This is what I got from plan after adding an entry.

Instead of a dynamic block, if we use for_each resource it is working fine irrespective of configuration. Would you please guide on how to achieve this without recreating everything?

Hi @apparentlymart,

Looks like it is a provider issue. In virtual network resource, a hash value has been set for subnet block. And this hash value changes when there is an update to any of the object in the map. But it is actually updating the resource which needs change.
I’ll create a new issue for respective azurerm resources. Thank you!