开发人员应该能够使用 terraform (terraform apply
) 创建自己的 Azure 服务总线命名空间。
但在我们当前的配置下,开发人员 A 创建了服务总线命名空间 A-service-bus-dev
,但如果开发人员 B 创建了 B-service-bus-dev
,A-service-bus-dev
被销毁并被开发人员 B 新创建的服务总线命名空间所取代。
情况不应该是这样,因为每个开发人员都需要自己的服务总线实例。
开发人员只需更改 main.tf
中的本地值 servicebus_namespace
。
Terraform 是否可以实现这种所需的行为?需要改变什么才能到达那里?
Terraform 状态文件保存在 Azure 内的存储容器(由帐户模块创建)内。
开发环境没有自动部署,每个开发人员都应该使用terraform apply
手动创建实例。
如果您需要更多信息,请告诉我。
<小时/>main.tf
locals {
subscription_id = "ID"
macroservice_name = "name"
servicebus_namespace = "A-service-bus-dev"
}
provider "azurerm" {
features {}
subscription_id = local.subscription_id
}
resource "azurerm_resource_group" "this" {
name = local.macroservice_name
location = "West Europe"
}
module "account" {
source = "../../modules/account"
location = "West Europe"
resource_group = local.macroservice_name
environment_name = "development"
storage_account = "dev"
storage_container = "terraformstate"
subscription_id = local.subscription_id
}
module "servicebus" {
source = "../../modules/servicebus"
location = "West Europe"
resource_group = azurerm_resource_group.this.name
servicebus_namespace = local.servicebus_namespace
servicebus_objects = [
{
topic_name = "x"
subscription_name = "x"
},
{
topic_name = "y"
subscription_name = "y"
}
]
}
modules/servicebus/servicebus.tf
resource "azurerm_servicebus_namespace" "this" {
name = var.servicebus_namespace
location = var.location
resource_group_name = var.resource_group
sku = "Standard"
}
resource "azurerm_servicebus_topic" "this" {
for_each = {for k, v in var.servicebus_objects : k => v}
name = each.value.topic_name
namespace_id = azurerm_servicebus_namespace.this.id
enable_partitioning = true
}
resource "azurerm_servicebus_subscription" "this" {
for_each = azurerm_servicebus_topic.this
name = var.servicebus_objects[index(var.servicebus_objects.*.topic_name, each.value.name)].subscription_name
topic_id = each.value.id
max_delivery_count = 1
dead_lettering_on_message_expiration = true
}
我添加了帐户模块来将 Terraform 状态保存在 Azure 中,认为这会导致所需的行为,但事实并非如此。
最佳答案
解决方案之一是在模块 block 中使用 for_each
参数循环遍历 set/dict。此外,为了简化开发人员的流程,您可以从 yaml 文件导入 set/dict。
即:
locals{
foo = "foo.yaml"
bar = fileexists(foo) ? yamldecode(file(foo)) : {}
}
...
module "servicebus" {
for_each = local.bar
source = "../../modules/servicebus"
location = azurerm_resource_group.this.location
resource_group = azurerm_resource_group.this.name
servicebus_namespace = each.value
servicebus_objects = [
{
topic_name = "x"
subscription_name = "x"
},
{
topic_name = "y"
subscription_name = "y"
}
]
}
关于azure - 如何使用 Terraform 为每个开发人员创建 Azure 服务总线命名空间实例并将其保存在状态中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74531111/