No history yet

Provider Functions and Registration

New Tools in AzureRM 4.x

The AzureRM 4.x provider introduces significant enhancements, moving beyond simple resource mapping. Key among these are provider-defined functions, which allow for complex string manipulation directly within your Terraform configurations. This eliminates the need for awkward workarounds with locals and built-in string functions.

Two powerful functions, normalise_resource_id and parse_resource_id, address common challenges with Azure resource identifiers. The normalise_resource_id function corrects case-sensitivity issues that can cause spurious diffs in plans, particularly with resources like Key Vault access policies where user principal names might vary in case. More versatile is , which deconstructs a full resource ID into its constituent parts.

locals {
  vnet_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-rg/providers/Microsoft.Network/virtualNetworks/my-vnet"
  
  // Deconstruct the VNet ID into a map of its parts
  parsed_vnet_id = parse_resource_id(local.vnet_id)
}

output "vnet_name" {
  value = local.parsed_vnet_id.name
}

output "resource_group_name" {
  value = local.parsed_vnet_id.resource_group_name
}

This functionality is critical when you need to extract a specific segment of an ID to pass to another resource, such as retrieving a resource group name from a resource ID imported via a data source.

Granular Provider Registration

Prior to version 4.0, the AzureRM provider attempted to register all supported Resource Providers during initialization. This "all-or-nothing" approach caused significant problems in restricted enterprise environments where a might have permissions to manage resources but not to register new providers across the entire subscription.

The introduction of the resource_provider_registrations feature flag provides granular control. By default, the provider will now only register providers for resources explicitly defined in your configuration. You can further refine this behaviour.

provider "azurerm" {
  features {
    // New block in 4.x
    resource_provider_registrations {
      // Explicitly list providers to register on init
      // Overrides the default behaviour of registering only what's used
      resource_providers_to_register = [
        "Microsoft.Compute",
        "Microsoft.Storage",
        "Microsoft.Network",
      ]
    }
  }
}

Setting resource_providers_to_register to an empty list ([]) disables automatic registration entirely, which is useful for environments where an operator with higher privileges handles all provider registrations centrally.

For enabling preview features or providers that are not yet part of the provider's default logic, you must manage registration explicitly. This is done using the azurerm_resource_provider_registration resource. This approach decouples preview feature enablement from the provider block, treating it as a manageable resource within the configuration itself.

resource "azurerm_resource_provider_registration" "aipreview" {
  name = "Microsoft.CognitiveServices"

  feature {
    name       = "AllowOpenAI"
    registered = true
  }
}

Upgrading from 3.x

Migrating from AzureRM 3.x to 4.x requires careful attention to these new behaviours, as they are breaking changes. The primary adjustments involve the provider registration logic. Configurations that relied on the implicit, broad registration of providers may fail during terraform plan if the Service Principal lacks the necessary permissions.

ConcernAzureRM 3.x BehaviourAzureRM 4.x Behaviour
Provider RegistrationRegisters all known providers on init.Registers only providers for resources in the configuration by default.
Permission RequirementMicrosoft.Resources/register/action needed on subscription scope.Microsoft.Resources/register/action is only needed for the specific providers being used.
Preview FeaturesNo standard mechanism.Managed explicitly via the azurerm_resource_provider_registration resource.
ID ParsingRequired split() or regex() workarounds in locals.Handled natively by parse_resource_id() and normalise_resource_id().

The upgrade path involves adding a features {} block to your azurerm provider configuration and deciding on your registration strategy. For most use cases, the new default behaviour is safer and more efficient, reducing initialization times and adhering to security best practices.

Time to check your understanding of these new provider features.

Quiz Questions 1/5

What is the primary purpose of the parse_resource_id function introduced in the AzureRM 4.x provider?

Quiz Questions 2/5

Prior to version 4.0, how did the AzureRM provider handle the registration of Resource Providers during initialization?

Mastering these functions and registration flags is essential for building scalable, secure, and maintainable infrastructure in modern Azure environments.