Azure Managed Identity 和 Terraform 在 AzureUSGoverment 中不起作用

Azure Managed Identity and Terraform not working in AzureUSGoverment

我正在尝试使用 managed-identityAzureUSGovernment 云中的虚拟机向 Azure 和 运行 terraform 进行身份验证。我已经按照找到的指南 here 将 terraform 配置为使用 managed-identity.

然而,每当我 运行 terraform apply/plan 等时,我都会看到以下错误:

│ Error: Unable to list provider registration status, it is possible that this is due to invalid credentials or the service principal does not have permission to use the Resource Manager API, Azure error: resources.ProvidersClient#List: Failure responding to request: StatusCode=404 -- Original Error: autorest/azure: Service returned an error. Status=404 Code="SubscriptionNotFound" Message="The subscription 'xxxxxxxxx-xxxx-xxxxx-xxxxx-xxxxxxxxxxx' could not be found."

(xxxxxxxxx-xxxx-xxxxx-xxxxx-xxxxxxxxxxx 是我编辑订阅 ID)

下面是我的配置片段和用于引导的 workflow/process:

  1. 创建具有托管身份的虚拟机,为其分配 Owner 角色(仅用于测试目的)
  2. 运行 az cloud set -n AzureUSGovernment
  3. 运行 az login --identity
  4. 运行 地形代码。

其他需要注意的重要事项:

provider "azurerm" {
  features {}
  use_msi = true
  subscription_id = "MYSUB-ID"
  tenant_id = "MYTENANT-ID"
}

非常感谢任何帮助!谢谢!

我尝试使用我的环境对其进行测试,但我得到的错误与您得到的错误完全相同:

注意:这是因为我使用的订阅不在 Azure 政府云中,而是在 Azure 云中。请确保您使用的是为其创建了托管身份的正确订阅,并确保其所在的环境。

并且,在您检查了订阅和环境之后,您可以跳过这些步骤:

Run az cloud set -n AzureUSGovernment
Run az login --identity

相反,您可以直接使用 terraform 代码:​​

provider "azurerm" {
  features {}
  use_msi = true
  subscription_id = "948d4068-xxxx-xxxxxx-xxxxxxx-xxxxxxxx"
  tenant_id = "72f988bf-xxxx-xxxxx-xxxxxx-xxxxxxxxx"
  environment = "usgovernment"
}

resource "azurerm_resource_group" "test" {
    name="xterraformtest12345"
    location ="east us"
}

注:

如果您的订阅在 public 中,则无需设置环境,如果在其他订阅中,则您可以根据需要设置环境。

输出: 由于订阅在 public 云中而移除环境后