How to fix Please run az login to setup account in Terraform
The "Please run az login to setup account" error occurs when Terraform cannot authenticate to Azure because the Azure CLI session has expired or is not authenticated. This error requires running az login to authenticate before Terraform can access your Azure resources.
Quick fix
Fixes Please run az login to setup account
az login- Authenticate with Azure CLI locally
- Verify authentication succeeded
- Run Terraform again
- For CI/CD pipelines: Use Service Principal instead
az loginPlease run az login to setup account
What this error means
This error occurs when the Azure provider in Terraform attempts to authenticate using Azure CLI credentials, but no valid authentication tokens are found. Terraform relies on the Azure CLI to authenticate to Azure by default when using the azurerm provider. The Azure CLI session can expire after 30 minutes of inactivity, or the user may never have logged in with az login. Additionally, in CI/CD pipelines or automated environments, the authentication context may not persist between jobs or executions.
Typical symptoms
- terraform plan or terraform apply fails with "Please run az login" message
- Error appears even though az account show works fine
- In CI/CD pipelines, Terraform authentication suddenly fails mid-deployment
- Error occurs after 30 minutes of inactivity in local development
- Service Principal authentication in pipelines fails with this message
Common causes
- Azure CLI session expired after 30 minutes of inactivity
- User has never authenticated with az login on this machine
- CI/CD pipeline running without proper Azure authentication setup
- Using az login --service-principal which Terraform does not support
- Environment variables not set in non-interactive environments
- Pipeline agent running in session without authentication context
How to fix "Please run az login to setup account"
Run the az login command to authenticate:
az loginThis will open a browser window where you can sign in with your Azure credentials. After successful authentication, return to your terminal.
Check that you are authenticated and can see your subscription:
az account showThis should display your current Azure subscription details including the subscription ID and tenant.
Now that you are authenticated with Azure CLI, Terraform can use those credentials:
terraform plan
terraform applyTerraform will use your Azure CLI authentication tokens automatically.
In automated environments, do not rely on az login. Instead, use Service Principal authentication by setting environment variables:
export ARM_CLIENT_ID="your-client-id"
export ARM_CLIENT_SECRET="your-client-secret"
export ARM_SUBSCRIPTION_ID="your-subscription-id"
export ARM_TENANT_ID="your-tenant-id"Terraform will automatically use these variables for authentication. Do not use az login --service-principal as Terraform does not support Service Principal authentication through the Azure CLI.
If you want to force Terraform to use only environment variables and not attempt CLI authentication, add this to your provider block:
provider "azurerm" {
features {}
use_cli = false
}Or set the environment variable:
export ARM_USE_CLI=falseIn Azure DevOps pipelines, use the AzureCLI@2 task to handle authentication:
- task: AzureCLI@2
inputs:
azureSubscription: 'your-service-connection'
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
terraform plan
terraform applyThe AzureCLI@2 task automatically sets up the authentication context for Terraform.
Advanced notes
Token Expiration: Azure CLI tokens expire after 30 minutes of inactivity. If you see this error after a long pause, simply run az login again.
Service Principal Limitation: When using a Service Principal, you cannot authenticate through az login --service-principal with Terraform. You must use the ARM_* environment variables instead.
Multi-Tenant: If you need to work across multiple Azure tenants, specify the tenant ID in the provider block or set ARM_TENANT_ID. You can also use az login --allow-no-subscriptions to force reporting of all tenants.
Docker/Kubernetes: In containerized environments, the Azure CLI session does not persist. Use Service Principal authentication with environment variables instead.
Cross-Cloud: If using Azure China, Germany Cloud, or Government Cloud, first configure az cloud set --name appropriately before running az login.
Frequently asked questions
- What does "Please run az login to setup account" mean?
- This error occurs when the Azure provider in Terraform attempts to authenticate using Azure CLI credentials, but no valid authentication tokens are found. Terraform relies on the Azure CLI to authenticate to Azure by default when using the azurerm provider. The Azure CLI session can expire after 30 minutes of inactivity, or the user may never have logged in with az login.
- How do I fix "Please run az login to setup account"?
- The "Please run az login to setup account" error occurs when Terraform cannot authenticate to Azure because the Azure CLI session has expired or is not authenticated. This error requires running az login to authenticate before Terraform can access your Azure resources.
- What causes "Please run az login to setup account"?
- Azure CLI session expired after 30 minutes of inactivity User has never authenticated with az login on this machine CI/CD pipeline running without proper Azure authentication setup
- Is the "Please run az login to setup account" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.