DivZ
DivZ

Reputation: 738

Having issues with provider definition when configuring Terraform for Azure in YAML

I'm working on an ambitious project to define Terraform configuration in YML for Azure but am having issues on handling the provider block. Here are the details.

Why YML? Two main reasons:

  1. I'd like to organize configuration templates across multiple directories
  2. I'd like to take away the ability for people to define terraform resources that are not processed by our custom terraform modules. A YAML configuration would prevent that.

I will have multiple YML files that look like this across multiple directory and subdirectories.

provider:
  name: "azurerm"
  features: {}
  skip_provider_registration: true
  alias: sub-test-01
  subscription_id: "d6025e9c-ce0b-4632-b9a8-8e04c49a1d6e"
codeowners_team: "test-team"
subscription_name: "sub-test-01"
parent_management_group_id: "/providers/Microsoft.Management/managementGroups/DEFAULT"
resource_provider_registrations:
  - "Microsoft.Cache"
  - "Microsoft.DBforMySQL"
default_subscription_access_entra_group_name: "group-test-sub-users"
access:
  subscription:
    - entra_group_name: "group-test-sub-users"
      role: "Contributor"
  resource_group:
    - resource_group_name: "rg-test-01"
      entra_group_name: "group-test-sub-users"
      role: "Owner"
  resource:
    - resource_id: "/resourceGroups/rg-test-01/providers/Microsoft.Storage/storageAccounts/storagetest01"
      spn: "spn-subs-test-temp"
      role: "Storage Account Contributor"

The HCL equivalent to this would look like:

provider "azurerm" {
    features {}
    skip_provider_registration = true
    alias = "sub-test-01"
    subscription_id = "d6025e9c-ce0b-4632-b9a8-8e04c49a1d6e"
}

module "sub-iam-test-01" {
    source = "../modules/subscription"
    subscription_name = "sub-test-01"
    codeowners_team = "test-team"
    parent_management_group_id = "/providers/Microsoft.Management/managementGroups/DEFAULT"
    resource_provider_registrations = ["Microsoft.Cache", "Microsoft.DBforMySQL"]
    default_subscription_access_entra_group_name = "group-test-sub-users"
    access = {
        subscription = [
            {
                entra_group_name = "group-test-sub-users"
                role = "Contributor"
            }
        ],
        resource_group = [
            {
                resource_group_name = "rg-test-01"
                entra_group_name = "group-test-sub-users"
                role = "Owner"
            }
        ],
        resource = [
            {
                resource_id = "/resourceGroups/rg-test-01/providers/Microsoft.Storage/storageAccounts/storagetest01"
                spn = "spn-subs-test-temp"
                role = "Storage Account Contributor"
            }
        ]
    }
    providers = {
        azurerm = azurerm.sub-test-01
    }
}

The ../modules/subscription custom module is handling the configuration in HCL for each property defined here - role assignments, management group associations, provider registrations, and a few other custom things.

Example folder structure:

- tf
   - subscriptions
      - project_alpha
         - alpha_subscription_01.yml
         - alpha_subscription_02.yml
      - project_beta
         - beta_subscription_01.yml
         - beta_subscription_02.yml
         - beta_subscription_03.yml
   - providers.tf
   - main.tf

Basically, I'm struggling with that main.tf

locals {
  subscription_yml_files = [for file in flatten(fileset(path.module,"subscriptions/**/*.yml")): yamldecode(file(file))]
}

module "subscriptions" {
  source = "../modules/subscription"
  for_each = local.subscription_yml_files

  subscription_name = each.value.name
  codeowners_team = each.value.codeowners_team
  parent_management_group_id = each.value.parent_management_group_id
  resource_provider_registrations = each.value.resource_provider_registrations
  default_subscription_access_entra_group_name = each.value.default_subscription_access_entra_group_name

  access = each.value.access

}

The idea was to have one main.tf file that processes all existing YML configurations

Up until this point, everything is working OK. However, I cannot figure out how to handle the provider "azurerm" block in YML. As far as I understand, provider definition cannot live inside a module. And since each one of these YML files are targeting specific Azure subscriptions (with the subscription_id property), it is mandatory.

Is it possible to somehow configure the provider with Azure subscription info inside the custom module in ../modules/subscription? Are there any alternate solutions?

Please advise.

Upvotes: 1

Views: 178

Answers (0)

Related Questions