Skip to main content
In this lesson you’ll learn how to authenticate Terraform to Azure using the Azure CLI (az login). This interactive flow is the simplest option for local development and ad-hoc runs. For automation or CI/CD, see the “Additional notes” section below for service principal guidance. Prerequisites
  • Terraform installed
  • Azure CLI installed
  • An Azure subscription
Example Terraform configuration used in this lesson providers.tf
main.tf
Step-by-step: authenticate Terraform to Azure
  1. Initialize the working directory
Open a terminal in the folder containing your Terraform files and run:
This downloads the provider plugins referenced in providers.tf and prepares the working directory.
  1. Try a plan/apply to see the credential error (if not authenticated)
If you run terraform plan or terraform apply before authenticating, the azurerm provider will fail because it cannot locate a subscription context. Example error:
This indicates Terraform cannot access Azure credentials for any subscription yet.
  1. Authenticate with the Azure CLI
Authenticate interactively:
This opens your browser and prompts you to sign in. Choose the account tied to the subscription you want to use.
A browser window showing a Microsoft Azure "Pick an account" sign-in dialog with a signed-in account and a "Use another account" option. The window sits over a colorful desktop background with a code editor visible behind it.
After signing in, the CLI will print the subscriptions available for that account. If you have multiple subscriptions, note the subscription ID you want to use. Get or list subscription IDs with:
  1. Target the subscription for Terraform
Terraform’s Azure provider can use the Azure CLI authentication token, but you must ensure the provider targets the correct subscription. You can either make the subscription active in the Azure CLI or set ARM_SUBSCRIPTION_ID as an environment variable. Set the active subscription in the Azure CLI:
Or set ARM_SUBSCRIPTION_ID directly (examples for common shells/OS): Note:
If you use setx on Windows, you may need to open a new terminal window for the environment variable to be available. In PowerShell, $env:... sets the variable only for the current session.
Do not commit credentials or secret values (for example, ARM_CLIENT_SECRET) into version control. For automation, prefer using secure variable stores or service principals with least privilege.
  1. Run Terraform apply
With az login complete and the subscription targeted (via az account set or ARM_SUBSCRIPTION_ID), run:
Terraform will authenticate using the Azure CLI token, create or update resources (in this example, a resource group), and write state to the configured backend (local by default). You should see output like “1 to add, 0 to change, 0 to destroy” before confirming the apply.
  1. Additional notes
Links and References That’s it — authenticate with az login and ensure ARM_SUBSCRIPTION_ID (or the CLI active subscription) is set so Terraform targets the correct Azure subscription.

Watch Video