Terraform计划未能使Azure刷新状态失败



Terraform计划,然后成功的Azure登录返回以下错误。不确定为什么Terraform在刷新状态时会抱怨无效的凭据,即使凭证已成功执行。

    terraform plan
  `[0m[1mRefreshing Terraform state in-memory prior to plan...[0m The refreshed state will be used to calculate this plan, but will not be persisted to local or remote state storage.
[0m
[31m
[1m[31mError: [0m[0m[1mError refreshing state: 1 error occurred:
    * provider.azurerm: Error building AzureRM Client: Error populating Client ID from the Azure CLI: No Authorization Tokens were found - please re-authenticate using `  `az login`.

在Terraform中为Azure,Azure CLI和Azure Service Principt在Terraform中进行身份验证是我们通常使用的两种方式。

要使用Azure CLI,通常,我们不会在Terraform中设置提供商块,或者仅设置提供商,仅设置提供商:

provider "azurerm" {
  version = "=1.28.0"
}

我建议您不要在Terraform文件中设置提供商。如果租户有多个订阅,则当您通过Azure CLI登录时,您也可以设置特殊订阅。

要使用Azure Service主体,您需要在Terraform中设置提供商块,如下所示:

provider "azurerm" {
  version = "=1.28.0"
  subscription_id = "00000000-0000-0000-0000-000000000000"
  client_id       = "00000000-0000-0000-0000-000000000000"
  client_secret   = "xxxxxxx"
  tenant_id       = "00000000-0000-0000-0000-000000000000"
}

我认为,错误可能表明您在Terraform提供商中设置了客户ID,并且与您成功登录的CLI不同。

正如您所说,您已经通过Azure CLI成功登录,因此最简单的方法就是删除Terraform文件中的提供商。

最新更新