How to fix Please run az login to setup account in Terraform

TerraformBEGINNERMEDIUMUpdated Dec 30, 2025ยท 185 views

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
  1. Authenticate with Azure CLI locally
  2. Verify authentication succeeded
  3. Run Terraform again
  4. For CI/CD pipelines: Use Service Principal instead
terraform โ€” error
az login
Please 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

  1. Azure CLI session expired after 30 minutes of inactivity
  2. User has never authenticated with az login on this machine
  3. CI/CD pipeline running without proper Azure authentication setup
  4. Using az login --service-principal which Terraform does not support
  5. Environment variables not set in non-interactive environments
  6. Pipeline agent running in session without authentication context

How to fix "Please run az login to setup account"

1Authenticate with Azure CLI locally

Run the az login command to authenticate:

bash
az login

This will open a browser window where you can sign in with your Azure credentials. After successful authentication, return to your terminal.

2Verify authentication succeeded

Check that you are authenticated and can see your subscription:

bash
az account show

This should display your current Azure subscription details including the subscription ID and tenant.

3Run Terraform again

Now that you are authenticated with Azure CLI, Terraform can use those credentials:

bash
terraform plan
terraform apply

Terraform will use your Azure CLI authentication tokens automatically.

4For CI/CD pipelines: Use Service Principal instead

In automated environments, do not rely on az login. Instead, use Service Principal authentication by setting environment variables:

bash
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.

5Alternative: Configure provider to disable CLI authentication

If you want to force Terraform to use only environment variables and not attempt CLI authentication, add this to your provider block:

hcl
provider "azurerm" {
  features {}
  use_cli = false
}

Or set the environment variable:

bash
export ARM_USE_CLI=false
6In Azure DevOps: Use AzureCLI@2 task

In Azure DevOps pipelines, use the AzureCLI@2 task to handle authentication:

yaml
- task: AzureCLI@2
  inputs:
    azureSubscription: 'your-service-connection'
    scriptType: 'bash'
    scriptLocation: 'inlineScript'
    inlineScript: |
      terraform plan
      terraform apply

The 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.

Official resources & further reading