Home > OS >  Terraform dynamic group creation/loop issues
Terraform dynamic group creation/loop issues

Time:11-17

I've searched and played around quite a bit and I've not come across the solution.

I am trying to manage subscription providers and preview features via the "azurerm_resource_provider_registration" resource.

i've got it working fine if I want to manage just one provider with multiple sub features using the following:

tfvars file

provider_name = "Microsoft.Network"

provider_feature_name = {
    feature1 = {
        feature_name = "BypassCnameCheckForCustomDomainDeletion"
        registered = true
    }
    feature2 = {
        feature_name = "AllowTcpPort25Out"
        registered = true
    }
}

main.tf

resource "azurerm_resource_provider_registration" "provider_registration" {
  name = var.provider_name 
  
  dynamic "feature"  {
    for_each   = var.provider_feature_name
    content {
      name       = feature.value.feature_name
      registered = feature.value.registered
    }
  }
}

works great if I only ever want to manage one provider and it's features.

The problem comes when/if I want to add an additional "provider_name". I've tried a separate provider_name block but I keep getting a "unexpected block here" error. if I introduce a block like so;

vars.tf

provider_name = {
    provider1 = {
        provider_name = "Microsoft.Network" {
          feature1 = {
            feature_name = "test"
            registered = true
          }
        }
     }
    provider2 = {
        provider_name = "Microsoft.Storage" {
          feature2 = {
            feature_name = "test2"
            registered = true
          }
        }

     }     
}

main.tf

resource "azurerm_resource_provider_registration" "provider_registration" {


  for_each = var.provider_name

  name = each.value.provider_name

  dynamic "feature" {
    for_each = var.provider_feature_name
    content {
      name       = feature.value.feature_name
      registered = feature.value.registered
    }
  }

I can get it loop but cannot get it to associate only feature1 to provider 1 etc as these features are exclusive to that provider. It associates feature1 to provider 1 & 2.

If I try to introduce a for_each or dynamic group for the "name" value, it comes up with "blocks of type provider not expected here" and/or "argument name is required but no definition was found"

In short, how can I get my main to loop over each provider_name and only associate the sub block of features to that provider (with potential for multiple features per provider type). is it just not possible for this type of resource? or am I just not understanding the loop/for_each documentation correctly.

any help is appreciated

thank you.

CodePudding user response:

First we need to cleanup and optimize the input structure. I have speculated on what the values should be since there are two different hypothetical structures specified in the question, but the structure itself is accurate.

providers = {
  "Microsoft.Network" = {
    features = { "BypassCnameCheckForCustomDomainDeletion" = true }
  }
  "Microsoft.Storage" = {
    features = { "AllowTcpPort25Out" = true }
  }
}

Now we can easily utilize this structure with a for_each meta-argument in the resource.

resource "azurerm_resource_provider_registration" "provider_registration" {
  for_each = var.providers

  name = each.key

  dynamic "feature" {
    for_each = each.value.features

    content {
      name       = feature.key
      registered = feature.value
    }
  }
}

and this results in two provider registrations with the corresponding feature mapped to each.

  • Related