Skip to content

Commit 90397c4

Browse files
fix: azurerm erronously detecting drift for role assignments
compute a stable scoped role_definition_id for subscription assignments
1 parent bfe4d94 commit 90397c4

File tree

1 file changed

+14
-4
lines changed
  • modules/meshcloud-replicator-service-principal

1 file changed

+14
-4
lines changed

modules/meshcloud-replicator-service-principal/module.tf

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,20 @@ resource "azurerm_role_assignment" "meshcloud_replicator_subscription_canceler"
196196
}
197197

198198
resource "azurerm_role_assignment" "meshcloud_replicator_rg_deleter" {
199-
for_each = toset(var.can_delete_rgs_in_scopes)
200-
scope = each.key
201-
role_definition_id = azurerm_role_definition.meshcloud_replicator_rg_deleter.role_definition_resource_id
202-
principal_id = azuread_service_principal.meshcloud_replicator.id
199+
for_each = toset(var.can_delete_rgs_in_scopes)
200+
scope = each.key
201+
principal_id = azuread_service_principal.meshcloud_replicator.id
202+
203+
# The azurerm provider requires this must be a scoped id, so unfortuantely we need to construct the id of the role
204+
# definition at the assignment scope in order to make this stable for subsequent terraform apply's.
205+
# See https://github.com/hashicorp/terraform-provider-azurerm/issues/4847#issuecomment-2085122502
206+
# Apparently, this problem only comes up when the scope is a subscription, it seems management groups are not affected.
207+
# RG deletion is typically only selectively enabled for specific subscriptions.
208+
role_definition_id = join("", [
209+
each.key,
210+
"/providers/Microsoft.Authorization/roleDefinitions/",
211+
azurerm_role_definition.meshcloud_replicator_rg_deleter.role_definition_id
212+
])
203213
}
204214

205215
//---------------------------------------------------------------------------

0 commit comments

Comments
 (0)