Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save clowa/950e40e7676914e65f36d4b60d2d5736 to your computer and use it in GitHub Desktop.

Select an option

Save clowa/950e40e7676914e65f36d4b60d2d5736 to your computer and use it in GitHub Desktop.

Revisions

  1. clowa revised this gist Oct 9, 2023. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions TF AzAPI Maintenance Configuration Dynamic Scope.md
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    # Terraform Azure Update Manager Maintenance Configuration Dynamic Scope assignment to all subscriptions

    This gist used the [terraform azapi provider](https://registry.terraform.io/providers/Azure/azapi/latest/docs) 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.

    ```hcl
  2. clowa renamed this gist Oct 9, 2023. 1 changed file with 0 additions and 0 deletions.
  3. clowa created this gist Oct 9, 2023.
    68 changes: 68 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    # Terraform Azure Update Manager Maintenance Configuration Dynamic Scope assignment to all subscriptions

    This gist used the [terraform azapi provider](https://registry.terraform.io/providers/Azure/azapi/latest/docs) 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.

    ```hcl
    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:
    - [ARM API | Configuration Assignments For Subscriptions - Create Or Update](https://learn.microsoft.com/en-us/rest/api/maintenance/configuration-assignments-for-subscriptions/create-or-update?tabs=HTTP)
    - [Az API Resource Definition | Microsoft.Maintenance/configurationAssignments](https://learn.microsoft.com/en-us/azure/templates/microsoft.maintenance/configurationassignments?pivots=deployment-language-terraform)