【问题标题】:Terraform authentication multi-subscription using multiple service principals使用多个服务主体的 Terraform 身份验证多订阅
【发布时间】:2021-07-08 11:22:13
【问题描述】:

我希望我的 terraform 脚本能够使用多个服务主体对多个 Azure 订阅进行身份验证。

这是我的想法:

  • 创建服务主体(应用注册)。
  • 在 azure 容器实例中部署 terraform 脚本
  • 将“contributor”角色授予我的订阅服务主体 (x)
  • 当我想在此订阅中创建资源时,使用环境变量配置 terraform 脚本以选择正确的凭据。
$ export ARM_SUBSCRIPTION_ID=159f2485-xxxx-xxxx-xxxx-xxxxxxxxxxxx     # Client subscription
$ export ARM_CLIENT_ID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx           # client_id of the service principal
$ export ARM_CLIENT_SECRET=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
$ export ARM_TENANT_ID=72f988bf-xxxx-xxxx-xxxx-xxxxxxxxxxxx           # the same tenant for all clients

这是正确的吗?

在使用 terraform cloud 时,您是否有更安全的方式来对多个订阅进行身份验证? (最好没有client_secret)

【问题讨论】:

  • 关于这个问题的任何更新?它能解决你的问题吗?

标签: terraform terraform-provider-azure azure-managed-identity terraform-cloud


【解决方案1】:

如果容器实例可以运行 Terraform 脚本,那么步骤没有问题。您授予服务主体权限并为不同的订阅更改环境变量ARM_SUBSCRIPTION_ID,然后 Terraform 脚本适用于不同的订阅。

更安全的方法是使用authentication with Azure CLI。如果您使用 CLI 命令设置不同的订阅:

az account set --subscription="SUBSCRIPTION_ID"

然后,Terraform 脚本也适用于不同的订阅。这样就不需要将secret设置为环境变量了。

【讨论】:

  • 此命令将在 terraform 容器内执行(在我的 Azure 订阅中),但 TF 脚本将在 Terraform Cloud 上执行,所以我认为运行 TF 的服务器必然会要求 client_secret在不同的订阅上进行身份验证
  • @admin 你看到我在答案中提供的变量了吗?您可以使用具有不同订阅 ID 的多个提供商。试试看。
  • @admin 关于这个问题的任何更新?它解决了你的问题吗?
  • 抱歉,我正在等待创建第二个订阅来测试它。我会尽快通知您。
猜你喜欢
  • 1970-01-01
  • 2018-11-03
  • 1970-01-01
  • 1970-01-01
  • 2020-02-05
  • 2014-05-04
  • 1970-01-01
  • 2017-07-26
  • 2018-11-29
相关资源
最近更新 更多