错误:构建ARM配置时出错:仅支持作为用户(不支持作为服务主体)使用Azure CLI进行身份验证

bvjxkvbb  于 2023-03-19  发布在  其他
关注(0)|答案(1)|浏览(123)

我正在执行terraform init,然后收到下一个错误。我想创建一个资源组并将terraform.tfstate存储在存储帐户中:
若要使用服务主体向Azure进行身份验证,您可以使用单独的“使用服务主体进行身份验证”│ auth方法-有关说明可在此处找到:https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/guides/service_principal_client_secret│ │或者,你可以通过使用用户帐户使用Azure CLI进行身份验证。
这是我的文件main.tf

data "azurerm_subscription" "main" {}

output "main" {
  value = data.azurerm_subscription.main.display_name
}

resource "azurerm_resource_group" "TerraformGroup" {
  name = "TerraformTest1"
  location = "South Central US"

还有我的文件versions.tf

terraform {
  required_version = ">= 0.15.0"

  required_providers {
     azurerm    = ">= 3.46.0"
  }

  backend "azurerm" {
    resource_group_name  = "rg01"
    storage_account_name = "digital"
    container_name       = "tfstate"
    key                  = "terraform.tfstate"
  }

}

provider "azurerm" {
  features {
  }
}

当我删除后端时,它可以工作,但我想要的是将terraform.state存储在一个存储帐户中

更新

根据@SiddheshDesai的回答,使用此main.tf将terraform.tfstate上传到存储帐户

terraform {  
required_providers {    
azurerm  =  {    
source = "hashicorp/azurerm"    
version = ">= 3.46.0"    
}  
}} 

provider  "azurerm" {
features {}
} 

terraform {  
backend  "azurerm" {  
resource_group_name  =  "<RG_NAME_STORAGE_ACCOUNT>"  
storage_account_name  =  "<STORAGE_ACCOUNT_NAME>"  
container_name  =  "CONTAINER_NAME"  
key  =  "terraform.tfstate"  
access_key = "<ACCESS_KEY_STORAGE_ACCOUNT>"  
}}

我只需要创建一个服务主体并运行
az登录--服务主体-u<client_id>-p< secret >--承租人<tenant_id>
在地形命令之前

bnl4lu3b

bnl4lu3b1#

为了在Terraform中部署存储帐户服务主体,您需要添加服务主体客户端ID、客户端密码、订阅ID和租户ID作为Terraform代码。

Azure CLI用于通过使用az log in登录Azure帐户来验证用户名和密码,然后terraform使用您使用Azure CLI登录的同一帐户,但它不适用于服务主体身份验证。

  • 我使用下面的代码创建了一个存储帐户,该帐户带有Container和blob,其中包含使用服务主体身份验证的tf状态文件。*
    地形代码:-

main.tf

terraform {

required_providers {

azurerm  =  {

source = "hashicorp/azurerm"

version = "3.8.0"

}

}

}

  

provider  "azurerm" {

subscription_id  =  "<subscription-id>"

tenant_id  =  "<tenant-id>"

client_id  =  "<client-id>"

client_secret  =  "<client-secret>"

features {}

}

  

resource  "azurerm_resource_group"  "appgrp" {

name  =  "rg-name"

location  =  "North Europe"

}

  

resource  "azurerm_storage_account"  "siliconstrg0985" {

name  =  "siliconstrg0985"

resource_group_name  =  "app-grp"

location  =  "North Europe"

account_tier  =  "Standard"

account_replication_type  =  "LRS"

account_kind  =  "StorageV2"

depends_on  =  [

azurerm_resource_group.appgrp

]

}

  

resource  "azurerm_storage_container"  "data" {

name  =  "data"

storage_account_name  =  "siliconstrg0985"

container_access_type  =  "blob"

depends_on  =  [

azurerm_storage_account.siliconstrg0985

]

}

  

resource  "azurerm_storage_blob"  "maintf" {

name  =  "main.tf"

storage_account_name  =  "siliconstrg0985"

storage_container_name  =  "data"

type  =  "Block"

source  =  "main.tf"

depends_on  =  [

azurerm_storage_container.data

]

}

  

terraform {

backend  "azurerm" {

resource_group_name  =  "app-grp"

storage_account_name  =  "siliconstrg0985"

container_name  =  "data"

key  =  "terraform.tfstate"

}

}

输出:-

传送门:-

相关问题