@@ -155,6 +155,47 @@ resource "azurerm_resource_group" "avd_green" {
155155# principal_id = local.principal_id
156156# }
157157
158+
159+ data "azuread_service_principal" "avd_ms_sp" {
160+ application_id = local. principal_id # this is your "9cdead84-..." appId
161+ }
162+
163+
164+ resource "azurerm_role_definition" "avd_autoscale_operator" {
165+ name = " AVD Autoscale Operator"
166+ scope = data. azurerm_subscription . current . id
167+ description = " Allows Azure Virtual Desktop Autoscale to manage session host power state"
168+
169+ permissions {
170+ actions = [
171+ " Microsoft.DesktopVirtualization/hostPools/read" ,
172+ " Microsoft.DesktopVirtualization/hostPools/sessionHosts/read" ,
173+ " Microsoft.DesktopVirtualization/hostPools/sessionHosts/write" ,
174+ " Microsoft.Compute/virtualMachines/start/action" ,
175+ " Microsoft.Compute/virtualMachines/deallocate/action" ,
176+ " Microsoft.Compute/virtualMachines/read" ,
177+ " Microsoft.Insights/autoscalesettings/*"
178+ ]
179+ not_actions = []
180+ }
181+
182+ assignable_scopes = [
183+ data . azurerm_subscription . current . id
184+ ]
185+ }
186+
187+ resource "azurerm_role_assignment" "avd_autoscale_blue_sp" {
188+ for_each = local. deploy_blue_avd ? var. regions : {}
189+
190+ scope = azurerm_resource_group. avd_blue [each . key ]. id
191+ role_definition_id = azurerm_role_definition. avd_autoscale_operator . id
192+ principal_id = data. azuread_service_principal . avd_ms_sp . object_id
193+ principal_type = " ServicePrincipal"
194+
195+ # ensure role definition exists first
196+ depends_on = [azurerm_role_definition . avd_autoscale_operator ]
197+ }
198+
158199# Green AVD deployment
159200module "virtual-desktop-green" {
160201 for_each = (local. deploy_green_avd ? var. regions : {})
0 commit comments