【问题标题】:Adding role assignments to multiple Azure subscriptions for a managed identity using terraform使用 terraform 将角色分配添加到托管标识的多个 Azure 订阅
【发布时间】:2020-11-17 04:47:44
【问题描述】:

我有一个托管在订阅“sub-test1”中的 Azure 函数应用,我想添加角色分配以授予托管系统标识(应用)访问订阅“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”资源块来完成,还是每个订阅都需要多个这样的块?

【问题讨论】:

  • 还有问题吗?它解决了你的问题吗?如果解决了您的问题,请采纳。

标签: azure azure-functions terraform terraform-provider-azure


【解决方案1】:

对于此要求,您需要有足够的权限才能在订阅中为其创建角色分配。最简单的方法是您需要拥有所有订阅的所有者角色。然后你可以改变这样的代码来实现你想要的:

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。也用于角色定义。

【讨论】:

    猜你喜欢
    • 2022-01-18
    • 1970-01-01
    • 2020-08-09
    • 2020-11-18
    • 2021-12-28
    • 2019-11-21
    • 1970-01-01
    • 2019-01-13
    • 2021-06-14
    相关资源
    最近更新 更多