Terraform:未找到 Azure CLI 授权配置文件。请确保已安装 Azure CLI

Terraform: Azure CLI Authorization Profile was not found. Please ensure the Azure CLI is installed

我已经使用 azure link 和 terraform apt-get 安装了 az-cli。

时出现以下错误

Error: Error running plan: 1 error(s) occurred:

  • provider.azurerm: Error building AzureRM Client: Azure CLI Authorization Profile was not found. Please ensure the Azure CLI is installed and then log-in with az login.

尝试卸载并重新安装,但没有成功。

在我的例子中,这个问题是因为 Terraform 在使用 apt-get 安装时已经过时了。相反,我从 here 下载了最新版本,然后它运行得很好。

在我的例子中,这是因为我手动将 Terraform 状态从本地复制到 azurerm 后端。当我这样做时 "terraform way"(更改后端,再次初始化,然后 terraform 询问您是否要将旧状态复制到新后端),它开始工作了。

1.Generate AZ CLI Service principal using the following doc,
 https://www.terraform.io/docs/providers/azurerm/guides/service_principal_client_secret.html#configuring-the-service-principal-in-terraform

   2.Create main.tf like below;

   <pre>
   variable "client_secret" {
     type = string
   default = "312312321321313131.cNSUbpQ8tmX.0"
   }

   provider "azurerm" {
   version = "=2.5.0"

    subscription_id = "312213-a480-4f81-9c11-698ea4e5bdc1"
    client_id       = "12321321-3a4c-4b8a-b368-360a3b1d10fe"
    client_secret   = var.client_secret
    tenant_id       = "3123213-331a-48ba-b45d-58132312435c"
      features {}
    }
    </pre>

    3.terraform init - plan