azure - 通过 Terraform 获取系统分配的托管标识的 Azure 客户端/应用程序 ID

标签 azure terraform azure-sql-database terraform-provider-azure

我正在尝试使用 mssql_user Microsoft SQL Server Provider的资源让 Terraform 在我的 Azure SQL 数据库中为我的 Web 应用程序的托管标识创建用户。我没有 Azure Active Directory 的Directory Readers 角色,因此我need to supply the client id我的托管身份(或应用程序 ID),而不仅仅是其名称。

resource "mssql_user" "example" {
  server {
    host = "example-sql-server.database.windows.net"
    azure_login { }
  }
  database  = "my-database"
  username  = azurerm_user_assigned_identity.example.name
  object_id = azurerm_user_assigned_identity.example.client_id
  roles     = ["db_datareader"]
}

这对于用户分配的托管标识效果很好,其 azurerm_user_assigned_identity 资源公开 client_id 属性。但是,对于系统分配的托管身份,identity block only exposes a principal_id ,而不是 client_id。将其与 mssql_user 资源结合使用会导致创建 Azure SQL 数据库用户,但随后无法登录数据库。

有没有办法在 Terraform 中获取系统分配的托管标识的客户端 ID(即应用程序 ID),以便我可以将其提供给 mssql_user 资源?

最佳答案

您可以使用azure_service_principal data source 。 只需将object_id设置为principal_id,它就会返回application_id,即client_id。

data "azuread_service_principal" "sp" {
  object_id = some_resource.identity[0].principal_id
}

关于azure - 通过 Terraform 获取系统分配的托管标识的 Azure 客户端/应用程序 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76179989/

相关文章:

powershell - Login-AzureRmAccount 无法使用 PSCredential 登录 Azure

terraform - 是否可以在代码块中而不是在 Web 界面中更改 Terraform Cloud 工作区执行模式?

amazon-web-services - 使用新记录验证证书

sql-server - 超时已过。在 Azure sql 上完成操作之前超时时间已过

Azure数据工厂: How to rename blob csv or text file in blob storage during copy or import?

Azure 板 : retrieve all Work Items from a certain board of a given team

azure - 如何限制访问某些公共(public)网站

java - 将对象放入 Azure 存储时出现身份验证错误

azure - 无法使用 terraform 创建 Azure 认知服务

sql - 删除 Azure 移动服务中使用的数据库中的表时如何防止错误?