Skip to main content
In this lesson you’ll learn how to authenticate Terraform with Azure so you can run terraform plan and terraform apply. There are multiple authentication options (environment variables, service principals, managed identities, Azure CLI, etc.). For interactive and development workflows the simplest method is to authenticate with the Azure CLI using az login. Below is a minimal, reproducible example and the exact steps to get Terraform authorized to manage Azure resources. Below are the example Terraform files used in this lesson. main.tf
providers.tf
Prerequisites
  • Terraform installed.
  • Azure CLI installed and available on your PATH.
  • An Azure account with a subscription you can use.
If you are using a development environment such as Codespaces, the Azure CLI is often pre-installed.
Quick overview of authentication options Step-by-step
  1. Initialize the working directory
  1. Attempt a plan (before authenticating)
If you have not authenticated, the AzureRM provider will report that a subscription_id (or other credentials) is required. Example error:
  1. Authenticate with the Azure CLI Run:
This command opens a browser window for interactive sign-in (or prints a device login code for headless environments). After a successful login the CLI lists the subscriptions available to the account. To confirm the CLI can access your account:
  1. Configure which subscription Terraform should use
There are two common ways to make a subscription available to Terraform when authenticating via the Azure CLI: Option A — Set the ARM_SUBSCRIPTION_ID environment variable (session-only)
  • macOS / Linux (bash/zsh):
  • Windows PowerShell (session-only):
  • Windows Command Prompt (session-only):
To persist the variable across sessions on Windows, use setx or configure it in System Properties. Option B — Tell the Azure CLI which subscription to use (affects az commands and is honored by CLI auth):
Either of these options ensures the subscription ID is available when the AzureRM provider authenticates via the Azure CLI.
  1. Apply the Terraform configuration
Terraform will show a plan summary similar to:
Type yes to proceed. Terraform will create the resource group and write state to the configured backend (local by default). Notes and troubleshooting
  • If you still see errors about missing credentials:
    • Verify az login succeeded and az account show returns the expected subscription.
    • Confirm ARM_SUBSCRIPTION_ID is set in your session or that az account show lists the desired subscription.
  • To check CLI-auth usage, run Terraform with detailed logs:
  • For non-interactive automation (CI/CD), use a Service Principal or a managed identity instead of az login. See the official docs for best practices and examples.
Do not commit secrets or credentials (client secrets, subscription IDs, or other sensitive data) to version control. For automation, prefer Service Principal or Managed Identity with least-privilege RBAC and store credentials in a secure secrets manager.
References and further reading That’s it — authenticate with az login, set the subscription (via environment variable or az account set), then use terraform plan and terraform apply to manage your Azure resources.

Watch Video