> ## Documentation Index
> Fetch the complete documentation index at: https://cloudanix.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Compute vmss automatic os upgrades remediation

### Triage and Remediation

<Tabs>
  <Tab title="Remediation">
    ### Remediation

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        To enable Automatic OS Upgrades in Azure, follow these steps:

        1. Log in to the Azure portal.

        2. Select the Virtual Machine that you want to configure.

        3. In the Virtual Machine pane, select the "Update Management" option.

        4. In the "Update Management" pane, select the "Schedule update deployments" option.

        5. In the "Schedule update deployments" pane, select the "Automatic" option for "OS upgrades".

        6. Choose the maintenance window time that suits your needs.

        7. Click on the "Save" button to save the changes.

        That's it! Now your Virtual Machine will automatically receive OS upgrades during the maintenance window you have selected, ensuring that your system is always up-to-date and secure.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To remediate the misconfiguration of not having automatic OS upgrades enabled for Azure using Azure CLI, you can follow these steps:

        1. Open the Azure CLI and log in to your Azure account using the command: `az login`
        2. Once you are logged in, set the subscription where the virtual machine is located using the command: `az account set --subscription <subscription_id>`
        3. Identify the virtual machine that needs to have automatic OS upgrades enabled using the command: `az vm list`
        4. Once you have identified the virtual machine, run the following command to enable automatic OS upgrades: `az vm auto-upgrade --resource-group <resource_group_name> --name <vm_name> --set upgradePolicy.mode="Automatic"`
        5. This command will enable automatic OS upgrades for the specified virtual machine.

        It is important to note that automatic OS upgrades may cause some issues with certain applications or services running on the virtual machine. Therefore, it is recommended to test the automatic OS upgrade feature in a non-production environment before enabling it in a production environment.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the misconfiguration "Enable Automatic OS Upgrades" for Azure using Python, you can follow these steps:

        1. Import the required Azure Python SDK modules:
           ```
           from azure.common.credentials import ServicePrincipalCredentials
           from azure.mgmt.compute import ComputeManagementClient
           from azure.mgmt.resource import ResourceManagementClient
           from azure.mgmt.subscription import SubscriptionClient
           ```

        2. Authenticate and create a client object for the Azure subscription:
           ```
           credentials = ServicePrincipalCredentials(client_id=<client_id>, secret=<secret>, tenant=<tenant_id>)
           subscription_client = SubscriptionClient(credentials)
           subscription = next(subscription_client.subscriptions.list())
           ```
           Replace `<client_id>`, `<secret>`, and `<tenant_id>` with the appropriate values for your Azure account.

        3. Create a client object for the Azure Resource Manager:
           ```
           resource_client = ResourceManagementClient(credentials, subscription.subscription_id)
           ```

        4. Get the list of virtual machines in the Azure subscription:
           ```
           compute_client = ComputeManagementClient(credentials, subscription.subscription_id)
           vms = compute_client.virtual_machines.list_all()
           ```

        5. For each virtual machine, enable automatic OS upgrades:

           ```
           for vm in vms:
               update_resource = compute_client.virtual_machines.begin_update(resource_group_name=vm.id.split('/')[4], vm_name=vm.name, parameters={'automatic_os_upgrade_policy': {'enable_automatic_os_upgrade': True}})
           ```

           This will enable automatic OS upgrades for all virtual machines in the subscription.

        Note: Make sure you have the necessary permissions to perform these actions in your Azure subscription before running the script.
      </Accordion>

      <Accordion title="Using Terraform">
        ```hcl theme={null}
        resource "azurerm_linux_virtual_machine_scale_set" "VMSS_NAME" {
          name                = "VMSS_NAME"                # replace with your VMSS name
          resource_group_name = azurerm_resource_group.RG_NAME.name
          location            = azurerm_resource_group.RG_NAME.location
          sku                 = "VM_SIZE"                  # e.g. "Standard_DS1_v2"
          instances           = 2

          # Automatic OS Upgrades require Rolling upgrade mode
          upgrade_mode = "Rolling"

          automatic_os_upgrade_policy {
            enable_automatic_os_upgrade = true
            # optional but recommended, set according to your rollback preference:
            disable_automatic_rollback = false
          }

          # ...rest of your existing configuration (network_profile, os_disk, source_image_reference, etc.) ...
        }
        ```

        Replace `VMSS_NAME`, `RG_NAME`, and `VM_SIZE` with your actual values.

        Changing `upgrade_mode` on an existing scale set may force replacement of the VM scale set resource and recreate instances, causing an outage during apply.

        Verification: `terraform plan` should show `enable_automatic_os_upgrade` changing from `false` (or null) to `true` (and possibly `upgrade_mode` changing to `"Rolling"`), with no other unintended changes.
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>
