Skip to content
Fix Azure China Authentication Errors in Terraform: A Step‑by‑Step Guide
Cloud Computing 8 min read

Fix Azure China Authentication Errors in Terraform: A Step‑by‑Step Guide

This article walks through configuring the Azure Provider in Terraform, explains the four authentication methods, details how to set up a Service Principal with a client secret, and shows how adding the "environment = \"china\"" parameter resolves authentication failures when using Azure China.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations

After a period of not using Terraform for Azure, the author revisits Azure Provider authentication and encounters issues.

Terraform requires Azure credentials; the Azure Provider supports four authentication methods. The article focuses on the fourth method: authenticating using a Service Principal with a Client Secret.

First, create a Service Principal in Azure (see official docs). Then configure provider.tf with subscription_id, client_id, client_secret, tenant_id, and optionally version.

provider "azurerm" {
  version = "=2.4.0"
  subscription_id = "00000000-0000-0000-0000-000000000000"
  client_id       = "00000000-0000-0000-0000-000000000000"
  client_secret   = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  tenant_id       = "00000000-0000-0000-0000-000000000000"
  features {}
}

The main.tf defines a resource group:

resource "azurerm_resource_group" "azure-tf-rg" {
  name     = "terraform-eval"
  location = "chinaeast2"
  tags = {
    env      = "dev"
    location = "China East2"
  }
}

Running terraform init succeeds, but terraform plan fails with an authentication error because the default environment points to the public Azure cloud, while the Service Principal was created in Azure China.

Adding environment = "china" to the provider block resolves the issue.

provider "azurerm" {
  version       = "=2.4.0"
  environment   = "china"
  subscription_id = "00000000-0000-0000-0000-000000000000"
  client_id       = "00000000-0000-0000-0000-000000000000"
  client_secret   = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  tenant_id       = "00000000-0000-0000-0000-000000000000"
  features {}
}

After the change, terraform plan shows one resource to add, and terraform apply creates the resource group successfully.

This issue only appears when using Azure China, US Government, or Germany clouds; the public Azure cloud does not exhibit it.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Terraformcloud infrastructureAzureazurermService Principal
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.