Skip to content

Instantly share code, notes, and snippets.

@alexwilcox9
Created April 15, 2024 12:20
Show Gist options
  • Save alexwilcox9/033de7fe2ad46236be0d6ee9062a8462 to your computer and use it in GitHub Desktop.
Save alexwilcox9/033de7fe2ad46236be0d6ee9062a8462 to your computer and use it in GitHub Desktop.
Example of conditionals on AVD Scaling Plans in Terraform
variable "holiday" {
type = bool
default = false
}
resource "azurerm_resource_group" "example" {
name = "example-rg"
location = "UK South"
}
data "azuread_service_principal" "avd" {
display_name = "Windows Virtual Desktop" // In newer tenants this will be Azure Virtual Desktop
}
resource "azurerm_role_assignment" "start-on-connect" {
scope = azurerm_resource_group.example.id
role_definition_name = "Desktop Virtualization Power On Off Contributor"
principal_id = data.azuread_service_principal.avd.object_id
}
resource "azurerm_virtual_desktop_host_pool" "example" {
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
name = "example-hostpool"
type = "Pooled"
maximum_sessions_allowed = 10
load_balancer_type = "BreadthFirst"
validate_environment = false
start_vm_on_connect = true
lifecycle {
ignore_changes = [
load_balancer_type,
]
}
}
resource "azurerm_virtual_desktop_scaling_plan" "example" {
name = "regular-scalingplan"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
description = "Production Scaling Plan"
time_zone = "GMT Standard Time"
schedule {
name = "Weekdays"
days_of_week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
ramp_up_start_time = "07:00"
ramp_up_load_balancing_algorithm = "BreadthFirst"
ramp_up_minimum_hosts_percent = 35
ramp_up_capacity_threshold_percent = 70
peak_start_time = "12:30"
peak_load_balancing_algorithm = "BreadthFirst"
ramp_down_start_time = "17:00"
ramp_down_load_balancing_algorithm = "DepthFirst"
ramp_down_minimum_hosts_percent = 0
ramp_down_force_logoff_users = false
ramp_down_wait_time_minutes = 60
ramp_down_notification_message = "To save resources this VM will be shutdown in 60 minutes. Please save your work and log off. If you wish to continue working you may log back in to use a VM that is not being scaled down"
ramp_down_capacity_threshold_percent = 90
ramp_down_stop_hosts_when = "ZeroActiveSessions"
off_peak_start_time = "23:59"
off_peak_load_balancing_algorithm = "DepthFirst"
}
schedule {
name = "Weekend"
days_of_week = ["Saturday", "Sunday"]
ramp_up_start_time = "07:00"
ramp_up_load_balancing_algorithm = "BreadthFirst"
ramp_up_minimum_hosts_percent = 0
ramp_up_capacity_threshold_percent = 90
peak_start_time = "12:30"
peak_load_balancing_algorithm = "BreadthFirst"
ramp_down_start_time = "17:00"
ramp_down_load_balancing_algorithm = "DepthFirst"
ramp_down_minimum_hosts_percent = 0
ramp_down_force_logoff_users = false
ramp_down_wait_time_minutes = 60
ramp_down_notification_message = "To save resources this VM will be shutdown in 60 minutes. Please save your work and log off. If you wish to continue working you may log back in to use a VM that is not being scaled down"
ramp_down_capacity_threshold_percent = 90
ramp_down_stop_hosts_when = "ZeroActiveSessions"
off_peak_start_time = "23:59"
off_peak_load_balancing_algorithm = "DepthFirst"
}
depends_on = [azurerm_role_assignment.start-on-connect]
dynamic "host_pool" {
for_each = var.holiday ? [] : ["enabled"]
content {
hostpool_id = azurerm_virtual_desktop_host_pool.example.id
scaling_plan_enabled = true
}
}
}
resource "azurerm_virtual_desktop_scaling_plan" "holiday" {
name = "holiday-scalingplan"
location = azurerm_resource_group.example.location
resource_group_name = azurerm_resource_group.example.name
description = "Production Scaling Plan for periods of low use"
time_zone = "GMT Standard Time"
schedule {
name = "All Week"
days_of_week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", ]
ramp_up_start_time = "00:10"
ramp_up_load_balancing_algorithm = "DepthFirst"
ramp_up_minimum_hosts_percent = 0
ramp_up_capacity_threshold_percent = 100
peak_start_time = "00:20"
peak_load_balancing_algorithm = "DepthFirst"
ramp_down_start_time = "00:30"
ramp_down_load_balancing_algorithm = "DepthFirst"
ramp_down_minimum_hosts_percent = 0
ramp_down_force_logoff_users = false
ramp_down_wait_time_minutes = 60
ramp_down_notification_message = "To save resources this VM will be shutdown in 60 minutes. Please save your work and log off. If you wish to continue working you may log back in to use a VM that is not being scaled down"
ramp_down_capacity_threshold_percent = 100
ramp_down_stop_hosts_when = "ZeroActiveSessions"
off_peak_start_time = "23:59"
off_peak_load_balancing_algorithm = "DepthFirst"
}
dynamic "host_pool" {
for_each = var.holiday ? ["enabled"] : []
content {
hostpool_id = azurerm_virtual_desktop_host_pool.example.id
scaling_plan_enabled = true
}
}
depends_on = [azurerm_role_assignment.start-on-connect]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment