我正在尝试使用 Terraform 和 Terragrunt 在 Azure 中创建一堆不同的资源。 其中,我正在部署订阅和资源组。
我有一个包含一些元数据的中央变量文件,并基于该文件部署资源。 我可以部署我想要的所有订阅,但我有一个问题,因为我想在这些订阅中部署资源组,但我不确定如何以最佳方式执行此操作,因为资源组资源没有订阅参数。
变量文件看起来像:
inputs = {
departments = [
{
name = "test",
region = "West Europe"
email = "something@something.com"
},
{
name = "test2"
region = "West Europe"
email = "someone@something.com"
}
]
}
所以在我的资源组模块中,我是这样定义的:
resource "azurerm_resource_group" "example" {
for_each = {for dep in var.departments: dep.name => dep}
name = "rg-${each.value.name}"
location = "${each.value.region}"
}
没关系,但我需要将上下文切换到正确的订阅,因此资源组被放置在正确的订阅中。 有什么想法吗?
答案 0 :(得分:0)
要在多个订阅中部署资源,您可以使用多个提供程序,here您可以获得更多详细信息和以下示例代码:
provider "azurerm" {
subscription_id = "xxxxxx"
tenant_id = "xxxxxx"
client_id = "xxxxxx"
client_secret = "xxxxxx"
}
provider "azurerm" {
alias = sub2
subscription_id = "xxxxxx"
tenant_id = "xxxxxx"
client_id = "xxxxxx"
client_secret = "xxxxxx"
}
resource "azurerm_resource_group" "example1" {
provider = azurerm
...
}
resource "azurerm_resource_group" "example1" {
provider = azurerm.sub2
...
}
如果你在 terraform 中使用 for_each,你可以在输入中添加别名选项:
inputs = {
departments = [
{
name = "test",
provider = "azurerm"
region = "West Europe"
email = "something@something.com"
},
{
name = "test2"
provider = "azurerm.sub2"
region = "West Europe"
email = "someone@something.com"
}
]
}
这只是一个例子,但它是解决方法。您可以根据需要更改代码。