Home Infrastructure as Code (IaC) Different Ways to Authenticate Terraform with Azure

Different Ways to Authenticate Terraform with Azure

296
0

Before Terraform can deploy or manage Azure resources, it needs a way to authenticate — to prove who you are. Once authenticated, Azure checks your permissions (authorization) to determine what you’re allowed to do.
In this post, we’ll walk through the different ways Terraform can authenticate with Azure, starting from the easiest (Azure CLI) to more advanced options used in production and CI/CD pipelines.


☁️ Why Authentication Matters

Terraform communicates with Azure using the Azure Provider, which calls Azure’s REST APIs to create, update, and delete resources.
Azure must first verify who is running those commands and then what permissions they have.
That’s why Terraform requires proper authentication before performing any action.


1️⃣ Authenticate Using Azure CLI (Recommended for Beginners)

The easiest way to authenticate Terraform with Azure is through the Azure Command-Line Interface (CLI).
If you’ve already installed and logged in using Azure CLI, Terraform can automatically use those credentials.

Steps:

  1. Log in to Azure: az login --use-device-code 💡 If prompted, select your subscription number (e.g., 1) when shown in the list.
  2. Verify your login: az account show --output table
  3. Terraform automatically picks up your Azure CLI session when you initialize the provider.

Example Terraform configuration:

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 4.0"
    }
  }
}

provider "azurerm" {
  features {}
}

Run:

terraform init
terraform plan

✅ Terraform uses your current Azure CLI login automatically — no need to manage credentials manually.


2️⃣ Authenticate Using a Service Principal (For Automation)

When running Terraform in automation (like GitHub Actions, scripts, or shared servers), it’s best to use a Service Principal — an identity created for apps and tools to access Azure securely.

Steps:

  1. Create a Service Principal: az ad sp create-for-rbac --name terraform-sp --role="Contributor" --scopes="/subscriptions/<YOUR_SUBSCRIPTION_ID>"
    • The command outputs something like this:
      { "appId": "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
      "displayName": "terraform-sp",
      "password": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
      "tenant": "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" }
  2. Set these as environment variables:
    export ARM_CLIENT_ID="appId"
    export ARM_CLIENT_SECRET="password"
    export ARM_TENANT_ID="tenant"
    export ARM_SUBSCRIPTION_ID="<YOUR_SUBSCRIPTION_ID>"

✅ Terraform automatically detects and uses these values when authenticating with Azure.
This is ideal for non-interactive setups like build servers or CI/CD pipelines.


3️⃣ Authenticate Using Managed Identity (For Azure Resources)

If Terraform runs inside Azure (for example, in a VM), you can use a Managed Identity.
This lets Terraform authenticate without any credentials or secrets.

Steps:

  1. Enable a system-assigned or user-assigned managed identity for your Azure resource (e.g., VM or App Service).
  2. Assign the role (like Contributor or Reader) to that identity in Azure.
  3. Terraform automatically uses this identity to authenticate with Azure.

✅ This method is secure and ideal for production because it eliminates secrets and manual credentials.


💡 Bonus: Authentication in Azure Pipelines (Service Connections)

If you’re using Azure DevOps Pipelines to run Terraform, the easiest and most secure approach is to use a Service Connection. A Service Connection in Azure DevOps stores a Service Principal securely and allows your pipelines to access Azure resources automatically.

Steps:

  1. In Azure DevOps → Project Settings → Service connections → New service connection → Azure Resource Manager.
  2. Choose Service Principal (automatic) and name it something like Terraform-Azure-Connection.
  3. Reference it in your Terraform pipeline YAML:
    steps:
    - task: TerraformTaskV4@4
    displayName: Terraform Init & Plan
    inputs:
    provider: 'azurerm'
    command: 'plan'
    environmentServiceNameAzureRM: 'Terraform-Azure-Connection'
    The pipeline automatically injects authentication variables (client ID, secret, tenant, and subscription) into Terraform’s environment.

✅ Perfect for Azure DevOps automation — no secrets in code, no manual login.


🧠 Summary

MethodWhere to UseAuthentication TypeRecommended For
Azure CLILocal setupInteractiveBeginners, personal use
Service PrincipalAny environmentApp credentialsCI/CD and automation
Managed IdentityInside AzureIdentity-basedProduction environments
Service ConnectionAzure DevOps PipelinesManaged SPEnterprise pipelines

🎯 What’s Next

Now that you’ve learned how to authenticate Terraform with Azure, you’re ready to deploy your first resource.

Previous articleWhy Linux Is an Essential Skill for Every DevOps Engineer
Next articleAuthenticating Terraform with Azure CLI and Deploying a Virtual Network and Subnet
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here