azurerm) and AzAPI (azapi). You’ll also learn how to handle multiple Azure subscriptions or tenants using provider aliases.
Here’s what we will cover:
- What Terraform providers are and how Terraform uses them to interact with external platforms.
- How to declare
required_providersand apply safe version constraints. - A comparison of the AzureRM (
azurerm) and AzAPI (azapi) providers and when to use each. - How to handle multiple subscriptions/tenants with provider aliases.

What is a Terraform provider?
A provider is a plugin that implements resources and data sources for a target platform. Providers translate HCL into API calls (CRUD operations) against cloud platforms, SaaS services, or HTTP APIs. Common examples:hashicorp/aws— AWShashicorp/azurerm— Azure Resource Managerazure/azapi— Direct Azure REST API interactions
- Providers are downloaded by
terraform initfrom the Terraform Registry or other configured sources. - Each provider exposes resource types and data sources you reference in HCL.
- You declare provider requirements in the
terraformblock (required_providers) and configure provider instances withproviderblocks.
How Terraform discovers and configures providers
Use theterraform block to declare provider sources and version constraints. Example:
sourcepoints to the namespace/provider on the registry (or to a custom hostname).versionconstrains which provider versions Terraform will install.
terraform init, Terraform resolves and downloads the provider binaries and records checksums and versions in .terraform.lock.hcl.
Version constraints and best practices
Common constraint styles:- Exact:
= 3.45.0 - Tilde (pessimistic):
~> 3.0— allows>= 3.0.0and< 4.0.0
- Pin provider major versions (e.g.,
~> 3.0) to avoid surprises from breaking changes. - Commit
.terraform.lock.hclto version control to guarantee consistent provider installs across machines and CI. - Upgrade providers intentionally: update constraints, run
terraform init -upgrade, and test in non-production environments first.
Always commit
.terraform.lock.hcl and pin provider versions to ensure reproducible provider installs across developer machines and CI.Provider configuration and aliases
Provider instances are configured withprovider blocks. For azurerm, you typically include an (often-empty) features block:
provider meta-argument:
AzureRM vs AzAPI: when to use each
Use the table below for a quick comparison and guidance.
Examples:
-
AzureRM (preferred when resource is supported and stable):
- Use
azurermfor most production workloads - Better ergonomics and state handling
- Use
-
AzAPI (use when you need low-level control):
- Managing preview features or brand-new Azure services not yet modeled in
azurerm - Applying raw ARM templates or specifying
type+ APIversiondirectly
- Managing preview features or brand-new Azure services not yet modeled in
body:
- Prefer
azurermfor GA resources for better Terraform-native support. - Use
azapiwhen you need immediate access to new API versions, preview features, or raw ARM payloads not yet covered byazurerm. - It’s common to mix both providers in a single configuration — choose the right tool for the resource and be mindful of implicit dependencies (outputs, references, or ordering) between resources managed by different providers.
When mixing
azurerm and azapi, explicitly manage dependencies (e.g., using depends_on) if the ordering matters and verify resource state interactions to avoid drift or race conditions.Additional operational notes
terraform initresolves providers and records them in.terraform.lock.hcl. Commit this lock file to source control.- Upgrading providers:
- Change the version constraint in the
terraformblock. - Run
terraform init -upgrade. - Run
terraform planand test in non-production.
- Change the version constraint in the
- Use provider aliases to manage multiple subscriptions, tenants, or credentials in a single project.
- Consult official docs and registry pages for provider-specific behaviors:
- Terraform Providers: https://www.terraform.io/docs/cli/providers/index.html
- AzureRM provider registry: https://registry.terraform.io/providers/hashicorp/azurerm/latest
- AzAPI provider registry: https://registry.terraform.io/providers/azure/azapi/latest
Summary
- Providers connect Terraform to external platforms and are required for managing resources.
- Declare providers via
required_providersand configure instances withproviderblocks. - Pin provider versions and commit
.terraform.lock.hclfor reproducibility. - Use
azurermfor stable, supported Azure resources andazapifor new/preview or niche resources that require direct ARM API access. - Use provider aliases to manage multiple subscriptions, tenants, or credential sets in the same configuration.