在查找/导入角色分配ID时出错
我收到了以下错误:
module.azure-prdsvc-terraform-roleassignment.azurerm_role_assignment.this: Creating... ╷ │ Error: unexpected status 409 (409 Conflict) with error: RoleAssignmentExists: The role assignment already exists. The ID of the existing role assignment is 158e5c63c7a2453af1707aeac5ed2c40. │ │ with module.azure-prdsvc-terraform-roleassignment.azurerm_role_assignment.this, │ on .terraform/modules/azure-prdsvc-terraform-roleassignment/main.tf line 16, in resource "azurerm_role_assignment" "this": │ 16: resource "azurerm_role_assignment" "this" {
于是我尝试将 158e5c63c7a2453af1707aeac5ed2c40 作为输入进行导入,结果得到以下错误:
Error: could not parse Role Assignment ID "158e5c63c7a2453af1707aeac5ed2c40"
我查阅了Terraform的文档,看到它要求的格式是:
terraform import azurerm_role_assignment.example /subscriptions/00000000-0000-0000-0000-000000000000/providers/Microsoft.Authorization/roleAssignments/00000000-0000-0000-0000-000000000000
于是我前往资源组的IAM > 角色分配并搜索 '158e5c63c7a2453af1707aeac5ed2c40',却没有任何结果。
这是否意味着该分配实际上并不存在?还是我漏掉了什么?
解决方案
角色分配的ID(名称)是GUID。这个ID很可能是158e5c63-c7a2-453a-f170-7aeac5ed2c40。这很可能是角色分配资源提供程序在抛出错误时把连字符 (-) 去掉造成的问题。
注:搜索角色分配最简单的方式是使用Azure Resource Graph,并按如下查询:
AuthorizationResources
| where type =~ 'microsoft.authorization/roleassignments'
| where name == '158e5c63-c7a2-453a-f170-7aeac5ed2c40'