我完全同意cosmos db默认提供的默认读写角色。它的 ID 是
00000000-0000-0000-0000-000000000002
。
我编写了简单的 TF 代码来将该角色分配给功能应用程序:
resource "azurerm_cosmosdb_sql_role_assignment" "example" {
resource_group_name = data.azurerm_cosmosdb_account.profiles_cdb.resource_group_name
account_name = data.azurerm_cosmosdb_account.profiles_cdb.name
role_definition_id = "00000000-0000-0000-0000-000000000002"
principal_id = azurerm_linux_function_app.syringe_function_app.identity[0].principal_id
scope = data.azurerm_cosmosdb_account.profiles_cdb.id
}
但是该代码抛出:
Error: parsing "00000000-0000-0000-0000-000000000002" as an SqlRoleDefinition ID: parsing Azure ID: parse "00000000-0000-0000-0000-000000000002": invalid URI for request │ │ with azurerm_cosmosdb_sql_role_assignment.syringe_integration_cdb_playerprofiles_msi_readwrite, │ on main.tf line 323, in resource "azurerm_cosmosdb_sql_role_assignment" "example": │ 323: role_definition_id = "00000000-0000-0000-0000-000000000002"
When i assing that role with `azure cli`:
``` az cosmosdb sql role assignment create --account-name profiles-cdb
--resource-group rg-shared-test --scope /subscriptions/<subscription_id>/resourceGroups/rg-shared-test/providers/Microsoft.DocumentDB/databaseAccounts/profiles-cdb
--principal-id <principal_id> --role-definition-id 00000000-0000-0000-0000-000000000002
它就像一个魅力
我尝试通过 terraform 分配默认读写 cosmos db 角色,如上所述,与 Azure CLI 中的角色分配相比,它需要更多信息。
您在 Terraform 中遇到的问题与 Azure CLI 处理的问题不同。 Azure CLI 允许您使用角色定义 ID 的缩写形式,例如
00000000-0000-0000-0000-000000000002
表示内置角色。但是,Terraform 需要完整的 Azure 资源管理器 (ARM) ID 来进行角色定义。
分配角色时,Azure CLI 可以将角色定义 ID 的缩写形式解析为其完整 ARM ID。但是,Terraform 需要在配置中指定完整的 ARM ID,因为它不执行此解析。
要更正您的 Terraform 代码,您需要为角色定义指定完整的 ARM ID。此 ARM ID 通常遵循以下模式:
/subscriptions/{subscription-id}/providers/Microsoft.DocumentDB/databaseRoleDefinitions/{role-definition-id}
。
我的演示地形配置:
provider "azurerm" {
features {}
}
data "azurerm_client_config" "current" {}
resource "azurerm_resource_group" "example" {
name = "demorg-vk"
location = "West Europe"
}
resource "azurerm_cosmosdb_account" "example" {
name = "vksb-cosmosdb"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
offer_type = "Standard"
kind = "GlobalDocumentDB"
consistency_policy {
consistency_level = "Strong"
}
geo_location {
location = azurerm_resource_group.example.location
failover_priority = 0
}
}
resource "azurerm_cosmosdb_sql_role_definition" "example" {
name = "vksbsqlroledef"
resource_group_name = azurerm_resource_group.example.name
account_name = azurerm_cosmosdb_account.example.name
type = "CustomRole"
assignable_scopes = [azurerm_cosmosdb_account.example.id]
permissions {
data_actions = ["Microsoft.DocumentDB/databaseAccounts/sqlDatabases/containers/items/read"]
}
}
resource "azurerm_cosmosdb_sql_role_assignment" "example" {
name = "736180af-7fbc-4c7f-9004-22735173c1c3"
resource_group_name = azurerm_resource_group.example.name
account_name = azurerm_cosmosdb_account.example.name
role_definition_id = azurerm_cosmosdb_sql_role_definition.example.id
principal_id = data.azurerm_client_config.current.object_id
scope = azurerm_cosmosdb_account.example.id
}
输出:
如果您仍想在 terraform 配置中使用短格式 ID,则可以在 Terraform 中使用
null_resource
来执行 Azure CLI 命令。可以将 null_resource
与 local-exec
配置器结合使用。
resource "null_resource" "example" {
provisioner "local-exec" {
command = "az cosmosdb sql role assignment create --account-name profiles-cdb --resource-group rg-shared-test --scope /subscriptions/${var.subscription_id}/resourceGroups/rg-shared-test/providers/Microsoft.DocumentDB/databaseAccounts/profiles-cdb --principal-id ${azurerm_linux_function_app.syringe_function_app.identity[0].principal_id} --role-definition-id 00000000-0000-0000-0000-000000000002"
}
depends_on = [azurerm_linux_function_app.syringe_function_app]
}