gpt4 book ai didi

azure - 在 terraform 中同时创建多个资源

转载 作者:行者123 更新时间:2023-12-03 00:48:07 35 4
gpt4 key购买 nike

我正在编写我的第一个 terraform 脚本,我需要创建多个服务主体应用程序。我可以使用以下脚本来创建每个脚本:

resource "azuread_application" "main" {
name = var.name

available_to_other_tenants = false

identifier_uris = [format("http://%s", var.name)]
}

resource "azuread_service_principal" "auth" {
application_id = "${azuread_application.auth.application_id}"
}

resource "random_string" "password" {
length = 16
special = true
override_special = "/@\" "
}

resource "azuread_service_principal_password" "auth" {
service_principal_id = "${azuread_service_principal.auth.id}"
value = "${random_string.password.result}"
end_date_relative = "240h"
}

output "client_secret" {
value = "${random_string.password.result}"
description = "Client Secret"
}

provider "azurerm" {
version = "=1.24.0"
}

data "azurerm_subscription" "primary" {}

data "azurerm_client_config" "current" {}

resource "azurerm_role_assignment" "auth" {
scope = "${data.azurerm_subscription.primary.id}"
role_definition_name = "Reader"
principal_id = "${azuread_service_principal.auth.id}"
}

让我想创建其中的 6 个,什么时候是为所有 6 个 SP 重用此代码的最佳方法?

我知道可以做这样的事情:

resource "azuread_application" "auth" {
name = "${var.sp_names[count.index]}"

available_to_other_tenants = false

identifier_uris = [format("http://%s", ${var.sp_names[count.index]})]
count = "${length(var.sp_names)}
}

但是如果我这样做,我是否会将 ide 传递给 azure_service_principal block ?

最佳答案

要通过 Terraform 同时创建多个服务主体,您需要在资源中使用 count 属性。

这里是示例代码:

resource "azuread_application" "example" {
count = 2
name = "example-${count.index}"

available_to_other_tenants = false
oauth2_allow_implicit_flow = true
}

resource "azuread_service_principal" "example" {
count = 2
application_id = "${azuread_application.example[count.index].application_id}"
app_role_assignment_required = false
}

我看到您还想为服务主体设置密码和角色分配,那么您还需要在其资源中使用 count 属性,如上所示。

关于azure - 在 terraform 中同时创建多个资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58822422/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com