0
0
Terraformcloud~5 mins

Azure provider setup in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Setting up the Azure provider in Terraform lets you create and manage Azure resources using code. This solves the problem of manually configuring cloud resources by automating the process.
When you want to create virtual machines in Azure using Terraform.
When you need to manage Azure storage accounts as code.
When you want to automate the deployment of Azure networking components.
When you want to keep your Azure infrastructure configuration in version control.
When you want to reuse and share Azure infrastructure code across projects.
Config File - main.tf
main.tf
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.0"
    }
  }
  required_version = ">= 1.0"
}

provider "azurerm" {
  features {}
}

The terraform block specifies the Azure provider source and version to use.

The provider "azurerm" block configures the Azure provider with default features enabled.

This setup allows Terraform to authenticate and manage Azure resources.

Commands
This command initializes the Terraform working directory. It downloads the Azure provider plugin and prepares Terraform to manage Azure resources.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding hashicorp/azurerm versions matching "~> 3.0"... - Installing hashicorp/azurerm v3.64.0... - Installed hashicorp/azurerm v3.64.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, you must run "terraform init" again.
This command checks the Terraform configuration files for syntax errors and validates the provider setup before applying changes.
Terminal
terraform validate
Expected OutputExpected
Success! The configuration is valid.
This command shows the execution plan. It previews the actions Terraform will take to create or update Azure resources based on the configuration.
Terminal
terraform plan
Expected OutputExpected
No changes. Infrastructure is up-to-date. This means that Terraform did not detect any differences between your configuration and real physical resources that exist. As a result, no actions need to be performed.
Key Concept

If you remember nothing else from this pattern, remember: the Azure provider block in Terraform connects your code to Azure so you can manage resources automatically.

Common Mistakes
Not running 'terraform init' before other commands
Terraform won't download the Azure provider plugin and will fail to run plans or applies.
Always run 'terraform init' first to initialize the working directory and download required providers.
Omitting the 'features {}' block inside the provider configuration
The Azure provider requires the 'features' block even if empty; omitting it causes configuration errors.
Include 'features {}' inside the 'provider "azurerm"' block to satisfy provider requirements.
Summary
Create a 'main.tf' file with the Azure provider configuration including the 'features {}' block.
Run 'terraform init' to download the Azure provider plugin and prepare Terraform.
Use 'terraform validate' to check the configuration syntax and provider setup.
Run 'terraform plan' to preview changes before applying them to Azure.