Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Security and hardening

Terraform on an Azure VM or runner will not sign in with its user-assigned managed identity

Your error

terraform plan or apply is refused on an Azure VM or self-hosted runner that has ARM_USE_MSI=true2 more lines it can show

Fix it fast

Most common cause, 1 of 4

ARM_CLIENT_ID is missing, so the system-assigned identity is used

Export the four values the guide lists before any Terraform command, or put the same values in the provider block.

Fix
export ARM_USE_MSI=true
export ARM_CLIENT_ID=<client-id-of-the-user-assigned-identity>
export ARM_TENANT_ID=<tenant-id>
export ARM_SUBSCRIPTION_ID=<subscription-id>

# or in configuration
provider "azurerm" {
  features {}
  use_msi   = true
  client_id = "<client-id-of-the-user-assigned-identity>"
}
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

With ARM_USE_MSI=true (use_msi) the azurerm provider signs in as the machine's system-assigned identity unless ARM_CLIENT_ID (client_id) holds the user-assigned identity's client ID. The state backend needs use_msi, client_id and a blob data role of its own, and App Service or Functions hosts need ARM_MSI_ENDPOINT. Set these, plus ARM_TENANT_ID and ARM_SUBSCRIPTION_ID, then run init again.

  • Applies to Terraform 1.17, hashicorp/azurerm provider 5.14, azurerm state backend
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

On the machine that runs Terraform, ask the instance metadata service for a Resource Manager token on behalf of the user-assigned identity's client ID. A token back means the identity is attached and the ID is right, so look at the backend or the role. An error, or no answer at all, points at the ID or at a host that is not a VM.

Run this first
curl -s -H Metadata:true "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fmanagement.azure.com%2F&client_id=<client-id-of-the-identity>"

Then find the line you see in this list and open its fix:

  1. 1ARM_CLIENT_ID is missing, so the system-assigned identity is usedARM_USE_MSI is true, ARM_CLIENT_ID is empty, and the VM carries a system-assigned identity as well as the user-assigned one
  2. 2The azurerm state backend is not set up for the managed identityterraform init is the step that fails, before any provider call, or plan works with local state and breaks once the backend block is added
  3. 3Terraform runs on App Service or Functions, where the token endpoint differsTerraform runs inside an Azure Function App or App Service rather than on a VM or a VM-based agent
  4. 4The identity signs in but holds no role on the subscriptionThe metadata call in the first step returns a token, yet Terraform still cannot read or change resources

What does the error look like?

  • terraform plan or apply is refused on an Azure VM or self-hosted runner that has ARM_USE_MSI=true
  • terraform init cannot read the state blob even though the provider itself signs in
  • Terraform authenticates as the VM's system-assigned identity instead of the attached user-assigned one

Cause 1 of 4

ARM_CLIENT_ID is missing, so the system-assigned identity is used

You seeARM_USE_MSI is true, ARM_CLIENT_ID is empty, and the VM carries a system-assigned identity as well as the user-assigned one

Check

The azurerm managed identity guide says the provider picks the system-assigned identity by default. A user-assigned identity is chosen only when ARM_CLIENT_ID or the client_id argument carries its client ID. The value must be the identity's client ID, not its principal (object) ID or resource ID.

Check
env | grep ^ARM_

Fix

Export the four values the guide lists before any Terraform command, or put the same values in the provider block.

Fix
export ARM_USE_MSI=true
export ARM_CLIENT_ID=<client-id-of-the-user-assigned-identity>
export ARM_TENANT_ID=<tenant-id>
export ARM_SUBSCRIPTION_ID=<subscription-id>

# or in configuration
provider "azurerm" {
  features {}
  use_msi   = true
  client_id = "<client-id-of-the-user-assigned-identity>"
}

Verify

terraform plan lists the existing resources without an authentication error.

Verify
terraform plan

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 4

The azurerm state backend is not set up for the managed identity

You seeterraform init is the step that fails, before any provider call, or plan works with local state and breaks once the backend block is added

Check

The backend has its own authentication settings, separate from the provider block. For a compute-attached identity its documentation requires use_msi = true, and client_id whenever the identity is user-assigned. With use_azuread_auth the identity also needs Storage Blob Data Contributor on the state container, and Reader on the account if lookup_blob_endpoint is on.

Check
grep -n -A10 'backend "azurerm"' *.tf

Fix

Give the backend block its own identity settings (or rely on the same ARM_ variables) and assign the data role to the identity on the container.

Fix
terraform {
  backend "azurerm" {
    use_msi              = true
    use_azuread_auth     = true
    tenant_id            = "<tenant-id>"
    client_id            = "<client-id-of-the-user-assigned-identity>"
    storage_account_name = "<state-account>"
    container_name       = "tfstate"
    key                  = "prod.terraform.tfstate"
  }
}

Verify

terraform init -reconfigure finishes with the backend initialised and the state blob listed.

Verify
terraform init -reconfigure

Confirmed by developer.hashicorp.com

Did this fix it?

Cause 3 of 4

Terraform runs on App Service or Functions, where the token endpoint differs

You seeTerraform runs inside an Azure Function App or App Service rather than on a VM or a VM-based agent

Check

The provider uses a well-known managed identity endpoint, which covers VMs and most agents. The guide notes that some hosts, an Azure Function App for example, expose a different endpoint, and Terraform must be told where it is.

Fix

Pass the host's endpoint through ARM_MSI_ENDPOINT (or msi_endpoint in the provider block), keeping ARM_CLIENT_ID set for the user-assigned identity.

Fix
export ARM_USE_MSI=true
export ARM_CLIENT_ID=<client-id-of-the-user-assigned-identity>
export ARM_MSI_ENDPOINT=$MSI_ENDPOINT

Verify

terraform plan signs in from the App Service or Functions host and reads the subscription.

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 4 of 4

The identity signs in but holds no role on the subscription

You seeThe metadata call in the first step returns a token, yet Terraform still cannot read or change resources

Check

A managed identity starts with no access. The azurerm guide's first setup step is a role assignment that ties the identity to the subscription Terraform manages; without it Azure Resource Manager refuses the calls even though sign-in succeeded.

Fix

Someone holding Owner, or Contributor plus User Access Administrator, assigns a role such as Contributor on the subscription to the identity's principal ID. The guide's pattern, pointed at a user-assigned identity:

Fix
data "azurerm_subscription" "current" {}

data "azurerm_role_definition" "contributor" {
  name = "Contributor"
}

resource "azurerm_role_assignment" "terraform_runner" {
  scope              = data.azurerm_subscription.current.id
  role_definition_id = "${data.azurerm_subscription.current.id}${data.azurerm_role_definition.contributor.id}"
  principal_id       = azurerm_user_assigned_identity.runner.principal_id
}

Verify

Once the assignment shows on the subscription's access control page, terraform plan completes.

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Set ARM_USE_MSI, ARM_CLIENT_ID, ARM_TENANT_ID and ARM_SUBSCRIPTION_ID in the runner's environment once, so the provider and the backend read the same identity.
  • Attach only the user-assigned identity to build agents and leave system-assigned off, which removes the silent fallback.
  • Add the metadata curl from the first step as a pre-flight job on new runners.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Raheeb.

Explained for app owners

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.