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
- 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.
| Authentication method | Best for | Notes / example |
|---|---|---|
Azure CLI (az login) | Interactive/local development | Simple to use; provider will use the logged-in account. |
| Service Principal | CI/CD and automation | Non-interactive; requires creating SP and setting ARM_CLIENT_ID, ARM_CLIENT_SECRET, ARM_SUBSCRIPTION_ID, ARM_TENANT_ID. |
| Managed Identity | Azure-hosted automation (VM, App Service, AKS) | Secure, no secrets needed; grant RBAC to the managed identity. |
- Initialize the working directory
- Attempt a plan (before authenticating)
subscription_id (or other credentials) is required. Example error:
- Authenticate with the Azure CLI Run:
- Configure which subscription Terraform should use
ARM_SUBSCRIPTION_ID environment variable (session-only)
- macOS / Linux (bash/zsh):
- Windows PowerShell (session-only):
- Windows Command Prompt (session-only):
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):
- Apply the Terraform configuration
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 loginsucceeded andaz account showreturns the expected subscription. - Confirm
ARM_SUBSCRIPTION_IDis set in your session or thataz account showlists the desired subscription.
- Verify
- 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.
- Azure CLI documentation: https://learn.microsoft.com/cli/azure/
- AzureRM Provider (Terraform): https://registry.terraform.io/providers/hashicorp/azurerm/latest
- Authenticating with the Azure Provider: https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/guides/azure_cli
az login, set the subscription (via environment variable or az account set), then use terraform plan and terraform apply to manage your Azure resources.