Hi,
I am trying to assign Reservations Reader
role to a service principal created in azure (this requires "/providers/Microsoft.Capacity" as scope).
All resources like azuread_Application/service principal etc are generated via terraform code.
I show only the key bits.
This code fails to work:
resource "azurerm_role_assignment" "reservations_reader" {
scope = data.azurerm_subscription.primary.id
role_definition_id = "/providers/Microsoft.Authorization/roleDefinitions/582fc458-8989-419f-a480-75249bc5db7e"
principal_id = azuread_service_principal.main.object_id}
with this message (which I expected, since Reservations Reader has to be assigned on Microsoft.Capacity scope:
Error: authorization.RoleAssignmentsClient#Create: Failure responding to request: StatusCode=400 -- Original Error: autorest/azure: Service returned an error. Status=400 Code="RoleAssignmentScopeNotAssignableToRoleDefinition" Message="The role Reservations Reader is not available for assignment at the requested scope."
But if I change code to use /providers/Microsoft.Capacity as scope:
resource "azurerm_role_assignment" "reservations_reader" {
scope = "/providers/Microsoft.Capacity"
role_definition_id = "/providers/Microsoft.Authorization/roleDefinitions/582fc458-8989-419f-a480-75249bc5db7e"
principal_id = azuread_service_principal.main.object_id
}
I get the obvious error:
│ 41: scope = "/providers/Microsoft.Capacity"
│
╵
╷
│ Error: Can not parse "scope" as a resource id: No subscription ID found in: "providers/Microsoft.Capacity"
│
│ with azurerm_role_assignment.reservations_reader,
│ on main.tf line 41, in resource "azurerm_role_assignment" "reservations_reader":
│ 41: scope = "/providers/Microsoft.Capacity"
Which means scopes can only be in form of a resource based id sadly as below:
│ Expected a ResourceGroup ID that matched (containing 4 segments):
│
│ > /subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/example-resource-group
I had to go for a workaround using null_resource, but that is unable to assign this permission using a non-interactive login with a service principal dedicated to terraform pipelines in ci/cd.
It requires me, with my own azure user to run the commands from a terminal and using an interactive login via browser for it to work (I have the necessary permissions to assign other's permissions).
resource "null_resource" "reservations_reader_role_assignment" {
triggers = {
tenant_id = data.azurerm_subscription.primary.tenant_id
insight_application_object_id = azuread_service_principal.main.object_id
}
provisioner "local-exec" {
command = <<-EOF
Connect-AzAccount -Tenant ${self.triggers.tenant_id}
New-AzRoleAssignment -Scope '/providers/Microsoft.Capacity' -ObjectId ${self.triggers.insight_application_object_id} -RoleDefinitionName 'Reservations Reader'
EOF
interpreter = ["pwsh", "-Command"]
}
If there is no way to assign Reservations Reader permission using plain azurerm_role_assignment
, how can I make a non interactive login before New-AzRoleAssignment work for a service principal dedicated to running ci/cd pipelines?
I already tried with this code:
$SecurePassword = ConvertTo-SecureString -String