如何使用 Azure Terraform 中的串列/計數為用戶串列創建具有存盤帳戶的多個資源組?
我有兩個主檔案 (main.tf) 和一個模塊 (users.tf),我想為每個用戶創建一個資源組和一個存盤帳戶。
使用當前設定,我只能在主資源組中為每個用戶創建一個存盤帳戶。
這是我已經取得的一個例子:
主檔案
#azterraform {
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "2.81.0"
}
}
}
# Configure the Microsoft Azure Provider
provider "azurerm" {
features {}
}
# main Resource Group
resource "azurerm_resource_group" "main-rg" {
name = "RG-MAIN"
location = "easteurope"
}
# users module
module "users" {
source = "./modules/users"
resource_group = azurerm_resource_group.main-rg
for_each = toset( ["user1", "user2", "user3"] )
resource_unique_id = each.key
}
用戶.tf
# Create the Users Resource Group
resource "azurerm_resource_group" "users-rg" {
name = upper("RG-${var.resource_unique_id}-${var.config.suffix_nodash}")
location = var.config.location
tags = var.config.tags
}
# Grant contributor access to users ad-group
resource "azurerm_role_assignment" "users-contributor" {
scope = azurerm_resource_group.users-rg.id
role_definition_name = "Contributor"
principal_id = var.config.users-adgroup-id
}
# Grant contributor access to DDA Service principal
resource "azurerm_role_assignment" "DDA-sp-contributor" {
scope = azurerm_resource_group.users-rg.id
role_definition_name = "Contributor"
principal_id = var.config.dda-service-principal-id
}
# Create storage account in Users resource group
resource "azurerm_storage_account" "users-storage-account" {
name = lower("st${var.resource_unique_id}${var.config.suffix_nodash}")
resource_group_name = azurerm_resource_group.users-rg.name
location = azurerm_resource_group.users-rg.location
account_tier = "Standard"
account_replication_type = "LRS"
}
變數.tf 檔案:
variable "config" {
description = "(required) configuration variable value from the root module"
}
variable "resource_group" {
description = "(required) resource group for resources"
}
variable "resource_unique_id" {
description = "(required) resource unique identifier"
}
組態檔
{
"config": {
"region": "East Europe",
"suffix": "-eeu",
"suffix_nodash": "eeu",
"resource-acronym": "dda",
"tags": {
"Creator": "[email protected];",
"Managedby": "[email protected]; [email protected]; [email protected];",
"Project": "DDA",
"Projectversion": "1.0"
},
"location": "easteurope",
"azure_environment": {
"resource_manager_url": "https://management.azure.com/",
"authority": "https://login.microsoftonline.com/"
},
"users-adgroup-id": "abcd-abcd-abcd-abcd-abcd",
"dda-service-principal-id": "xyz-xyz-xyz-xyz-xyz"
}
}
uj5u.com熱心網友回復:
通過以下更改,代碼可以作業:
在定義“config”變數的根模塊級別添加一個 variable.tf 檔案
將“config”變數傳遞給 main.tf 中的“users”模塊
使用“-var-file”選項將 config.json 傳遞給 terraform
為了便于理解,請考慮在組態檔中使用原生 Terraform 而不是 JSON - 除非您對后者有充分的理由。
目錄結構
config.json
main.tf
variable.tf <<<<<<< Added this file
\modules
\users
users.tf
variable.tf
variable.tf(根模塊級別)
variable "config" {
description = "(required) configuration variable value from the root module"
}
主檔案
# users module
module "users" {
source = "./modules/users"
resource_group = azurerm_resource_group.main-rg
config = var.config <<<<<<<<<<<<<<<<<<<<<<<<<< Added
for_each = toset( ["user1", "user2", "user3"] )
resource_unique_id = each.key
}
從根模塊目錄執行
terraform init
terraform plan -var-file="./config.json"
terraform apply -var-file="./config.json"
Resource listing (after running terraform apply)
$ az resource list --query "[?contains(name, 'stuser')].{type:type, name:name, resourceGroup:resourceGroup}"
[
{
"name": "stuser1eeu",
"resourceGroup": "RG-USER1-EEU",
"type": "Microsoft.Storage/storageAccounts"
},
{
"name": "stuser2eeu",
"resourceGroup": "RG-USER2-EEU",
"type": "Microsoft.Storage/storageAccounts"
},
{
"name": "stuser3eeu",
"resourceGroup": "RG-USER3-EEU",
"type": "Microsoft.Storage/storageAccounts"
}
]
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/443203.html
標籤:天蓝色 地形 terraform-provider-azure
