Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clowa/950e40e7676914e65f36d4b60d2d5736 to your computer and use it in GitHub Desktop.
Save clowa/950e40e7676914e65f36d4b60d2d5736 to your computer and use it in GitHub Desktop.
Terraform Azure Update Manager Maintenance Configuration Dynamic Scope

Terraform Azure Update Manager Maintenance Configuration Dynamic Scope assignment to all subscriptions

This gist used the terraform azapi provider to directly interact with the ARM API, because the azurerm provider currently doesn't support this resource nativly.

You have to prepare the Azure VM first for Customer Managed Schedules Patch orchestration.

resource "azurerm_maintenance_configuration" "example" {
  name                = "example-mc"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location

  scope = "InGuestPatch"

  window {
    start_date_time = "2023-10-01 00:00"
    duration        = "02:00"
    time_zone       = "GTB Standard Time"
    recur_every     = "1Week Saturday"
  }

  install_patches {
    reboot = "IfRequired"
    linux {
      classifications_to_include = ["Critical", "Security"]
    }
    windows {
      classifications_to_include = ["Critical", "Security"]
    }
  }

  in_guest_user_patch_mode = "User"
}

data "azurerm_subscriptions" "available" {} # Get all subscriptions

resource "azapi_resource" "dynamic_scope" {
  for_each = { for sub in data.azurerm_subscriptions.available.subscriptions : sub.subscription_id => sub if sub.subscription_id == "cf288725-a1b6-48ce-9b2c-0506b1c5fa6d" }

  type      = "Microsoft.Maintenance/configurationAssignments@2023-04-01"
  name      = each.value.subscription_id # Is a unique identifier per resource
  location  = ""                         # Resource doesn't support locations
  parent_id = each.value.id              # Resource ID of the scope
  body = jsonencode({
    properties = {
      filter = {
        locations = []
        osTypes = [
          "Windows", "Linux"
        ]
        resourceGroups = []
        resourceTypes = [
          "microsoft.Compute/VirtualMachines", # Azure VM
          "microsoft.HybridCompute/machines",  # Azure Arc
        ]
        tagSettings = {
          filterOperator = "All"
          tags           = {}
        }
      }
      maintenanceConfigurationId = azurerm_maintenance_configuration.example.id
    }
  })
}

References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment