The following code successfully deploys 2 x log analytic workspaces in different regions in azure in their own region based resource groups
我在努力理解这一 FOR木工程如何运作
是否有人能够打断。 我研究了正式文件,但我仍然看不到。
感谢
locals.tf
locals {
resource_groups = {
for k, v in var.workspaces : k => {
location = v.location
}
}
}
main.tf
resource "azurerm_resource_group" "workspaces" {
for_each = local.resource_groups
name = each.key
location = each.value.location
}
resource "azurerm_log_analytics_workspace" "workspaces" {
for_each = var.workspaces
name = each.key
location = each.value.location
resource_group_name = azurerm_resource_group.workspaces[each.key].name
sku = "PerGB2018"
retention_in_days = each.value.retention_in_days
}
variables.tf
variable "workspaces" {
type = map(object({
location = string
retention_in_days = number
}))
default = {
workspace1 = {
location = "australiasoutheast"
retention_in_days = 30
}
workspace2 = {
location = "australiaeast"
retention_in_days = 30
}
# Add more workspaces as needed
}
}
outputs.tf
# Define outputs
output "workspace_ids" {
value = azurerm_log_analytics_workspace.workspaces[*].id
}
output "workspace_names" {
value = azurerm_log_analytics_workspace.workspaces[*].name
}
output "resource_group_ids" {
value = azurerm_resource_group.workspaces[*].id
}
output "resource_group_names" {
value = azurerm_resource_group.workspaces[*].name
}
output "resource_group_locations" {
value = azurerm_resource_group.workspaces[*].location
}