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 showFix it fast
Most common cause, 1 of 4ARM_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.
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>"
}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.
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.
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:
- 1ARM_CLIENT_ID is missing, so the system-assigned identity is used
ARM_USE_MSI is true, ARM_CLIENT_ID is empty, and the VM carries a system-assigned identity as well as the user-assigned one - 2The azurerm state backend is not set up for the managed identity
terraform init is the step that fails, before any provider call, or plan works with local state and breaks once the backend block is added - 3Terraform runs on App Service or Functions, where the token endpoint differs
Terraform runs inside an Azure Function App or App Service rather than on a VM or a VM-based agent - 4The identity signs in but holds no role on the subscription
The 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=trueterraform init cannot read the state blob even though the provider itself signs inTerraform 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.
env | grep ^ARM_Fix
Export the four values the guide lists before any Terraform command, or put the same values in the provider block.
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.
terraform planConfirmed by raw.githubusercontent.com
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.
grep -n -A10 'backend "azurerm"' *.tfFix
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.
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.
terraform init -reconfigureConfirmed by developer.hashicorp.com
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.
export ARM_USE_MSI=true
export ARM_CLIENT_ID=<client-id-of-the-user-assigned-identity>
export ARM_MSI_ENDPOINT=$MSI_ENDPOINTVerify
terraform plan signs in from the App Service or Functions host and reads the subscription.
Confirmed by raw.githubusercontent.com
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:
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
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.