[英]Adding role assignments to multiple Azure subscriptions for a managed identity using terraform
我有一個托管在訂閱“sub-test1”中的 Azure function 應用程序,我想添加角色分配以授予托管系統身份(應用程序)訪問訂閱“sub-test1”(當前)的權限,我已經能夠通過以下方式做到這一點:
data "azurerm_subscription" "current" {}
data "azurerm_role_definition" "owner" {
name = "Owner"
}
resource "azurerm_role_assignment" "custom_role_assignment" {
name = "${var.random_guid}"
scope = data.azurerm_subscription.current.id
role_definition_id = "${data.azurerm_subscription.current.id}${data.azurerm_role_definition.owner.id}"
principal_id = azurerm_function_app.app.identity.0.principal_id
}
但我需要讓這個應用程序訪問租戶內的多個訂閱(動態編號),比如“sub-test2”、“sub-test3”、“sub-test4”等。 僅使用 terraform 的最佳方法是什么? 此外,這是否可以僅使用一個“azurerm_role_assignment”資源塊來完成,如上所示,還是每個訂閱都需要多個這樣的塊?
對於此要求,您需要有足夠的權限才能在訂閱中為其創建角色分配。 最簡單的方法是您需要擁有所有訂閱的所有者角色。 然后你可以改變這樣的代碼來實現你想要的:
data "azurerm_subscriptions" "example" {}
data "azurerm_role_definition" "example" {
name = "Owner"
}
resource "azurerm_role_assignment" "custom_role_assignment" {
count = length(data.azurerm_subscriptions.example.subscriptions.*.subscription_id)
name = "${var.random_guid}"
scope = "/subscriptions/${element(data.azurerm_subscriptions.example.subscriptions.*.subscription_id, count.index)}"
role_definition_id = "/subscriptions/${element(data.azurerm_subscriptions.example.subscriptions.*.subscription_id, count.index)}${data.azurerm_role_definition.example.id}"
principal_id = azurerm_function_app.app.identity.0.principal_id
}
這是不同的東西。 使用azurerm_subscriptions
而不是azurerm_subscription
來獲取所有訂閱。 但它只獲取訂閱的 GUID。 所以我們需要自己填寫訂閱的資源Id。 也用於角色定義。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.