TFNFR12 - Dynamic for Optional Nested Objects

ID: TFNFR12 - Category: Code Style - Dynamic for Optional Nested Objects

An example using AzAPI:

resource "azapi_resource" "main" {
  type      = "Microsoft.ContainerService/managedClusters@2024-09-01"
  name      = var.name
  parent_id = var.parent_id
  location  = var.location
  body = {
    properties = { ... }
  }

  dynamic "identity" {
    for_each = var.client_id == "" || var.client_secret == "" ? [1] : []

    content {
      type         = var.identity_type
      identity_ids = var.user_assigned_identity_ids
    }
  }
  response_export_values = []
}

Please refer to the coding style in the example. Nested blocks under conditions, MUST be declared as:

for_each = <condition> ? [<some_item>] : []