> ## 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.

# Enable virtual machine boot diagnostics remediation

### Triage and Remediation

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

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        To enable Virtual Machine Boot Diagnostics in Azure using the Azure console, please follow these steps:

        1. Log in to the Azure portal using your credentials.

        2. Navigate to the virtual machine that you want to enable boot diagnostics for.

        3. In the virtual machine's blade, click on "Boot diagnostics" under the "Support + troubleshooting" section.

        4. In the "Boot diagnostics" blade, click on the "Enable" button.

        5. In the "Enable boot diagnostics" blade, select the storage account you want to use for storing the boot diagnostics logs.

        6. Click on "OK" to enable boot diagnostics for the virtual machine.

        7. Once boot diagnostics are enabled, you can view the boot logs by clicking on the "Serial log" tab in the "Boot diagnostics" blade.

        That's it! You have successfully enabled Virtual Machine Boot Diagnostics in Azure using the Azure console.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To remediate the misconfiguration "Enable Virtual Machine Boot Diagnostics" for AZURE using AZURE CLI, you can follow the below steps:

        1. Open the AZURE CLI on your system.

        2. Login to your AZURE account using the command: `az login`

        3. After logging in, select the subscription in which the virtual machine is present using the command: `az account set --subscription <subscription-id>`

        4. Next, enable boot diagnostics for the virtual machine using the command: `az vm boot-diagnostics enable --name <vm-name> --resource-group <resource-group-name> --storage <storage-account-name>`

           Here, replace `<vm-name>` with the name of the virtual machine for which you want to enable boot diagnostics, `<resource-group-name>` with the name of the resource group in which the virtual machine is present, and `<storage-account-name>` with the name of the storage account where the boot diagnostics data will be stored.

        5. Once the command is executed successfully, boot diagnostics will be enabled for the virtual machine.

           You can verify the same by logging in to the Azure portal, navigating to the virtual machine, and checking if boot diagnostics are enabled under the "Boot Diagnostics" section.

        By following the above steps, you can remediate the misconfiguration "Enable Virtual Machine Boot Diagnostics" for AZURE using AZURE CLI.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the misconfiguration of "Enable Virtual Machine Boot Diagnostics" in Azure using Python, you can use the Azure SDK for Python. Here are the step-by-step instructions:

        1. Install the Azure SDK for Python by running the following command in your terminal:

           `pip install azure-mgmt-compute`

        2. Authenticate with Azure by following the instructions in the Azure SDK for Python documentation.

        3. Get the resource group and virtual machine name that you want to remediate.

           ```
           resource_group_name = "my_resource_group"
           vm_name = "my_vm"
           ```

        4. Get the virtual machine object using the Azure SDK for Python.

           ```
           from azure.mgmt.compute import ComputeManagementClient

           compute_client = ComputeManagementClient(credentials, subscription_id)
           vm = compute_client.virtual_machines.get(resource_group_name, vm_name, expand='instanceView')
           ```

        5. Check if boot diagnostics are already enabled for the virtual machine.

           ```
           if vm.diagnostics_profile.boot_diagnostics is not None:
               print("Boot diagnostics are already enabled.")
           else:
               print("Boot diagnostics are not enabled.")
           ```

        6. If boot diagnostics are not enabled, enable them using the Azure SDK for Python.

           ```
           from azure.mgmt.compute.models import BootDiagnostics

           storage_account_uri = "/subscriptions/{subscription_id}/resourceGroups/{resource_group_name}/providers/Microsoft.Storage/storageAccounts/{storage_account_name}".format(
               subscription_id=subscription_id,
               resource_group_name=resource_group_name,
               storage_account_name=storage_account_name
           )

           boot_diagnostics = BootDiagnostics(enabled=True, storage_uri=storage_account_uri)
           vm.diagnostics_profile.boot_diagnostics = boot_diagnostics
           compute_client.virtual_machines.create_or_update(resource_group_name, vm_name, vm)
           ```

           Note: Replace `storage_account_name` with the name of the storage account that you want to use for boot diagnostics.

        7. Verify that boot diagnostics are enabled for the virtual machine.

           ```
           vm = compute_client.virtual_machines.get(resource_group_name, vm_name, expand='instanceView')
           if vm.diagnostics_profile.boot_diagnostics is not None:
               print("Boot diagnostics are enabled.")
           else:
               print("Boot diagnostics are not enabled.")
           ```

        That's it! You have successfully remediated the misconfiguration of "Enable Virtual Machine Boot Diagnostics" in Azure using Python.
      </Accordion>

      <Accordion title="Using Terraform">
        ```hcl theme={null}
        resource "azurerm_linux_virtual_machine" "EXISTING_VM" {
          name                = "REPLACE_WITH_VM_NAME"
          resource_group_name = azurerm_resource_group.EXISTING_RG.name
          location            = azurerm_resource_group.EXISTING_RG.location
          size                = "REPLACE_WITH_VM_SIZE"
          admin_username      = "REPLACE_WITH_ADMIN_USERNAME"

          network_interface_ids = [
            azurerm_network_interface.EXISTING_NIC.id,
          ]

          # ... other required arguments (os_disk, source_image_reference, etc.)

          # Enable Boot Diagnostics (recommended with a dedicated Storage Account)
          boot_diagnostics {
            storage_account_uri = azurerm_storage_account.BOOT_DIAG_STORAGE.primary_blob_endpoint
          }
        }

        resource "azurerm_storage_account" "BOOT_DIAG_STORAGE" {
          name                     = "REPLACE_WITH_UNIQUE_STORAGE_NAME"
          resource_group_name      = azurerm_resource_group.EXISTING_RG.name
          location                 = azurerm_resource_group.EXISTING_RG.location
          account_tier             = "Standard"
          account_replication_type = "LRS"
          kind                     = "StorageV2"
        }
        ```

        Changing only the `boot_diagnostics` block on an existing VM is an in-place update and does not force replacement.

        For verification, `terraform plan` should show an in-place `~ update` to `azurerm_linux_virtual_machine.EXISTING_VM` adding/enabling the `boot_diagnostics` block and any new `+ create` for `azurerm_storage_account.BOOT_DIAG_STORAGE` if it did not already exist.
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>
