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

# Azure audit compute disk volumes without use remediation

### Triage and Remediation

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

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        Sure, here are the step-by-step instructions to remediate the misconfiguration "Remove Unattached Virtual Machine Disk Volumes" for Azure using Azure console:

        1. Log in to your Azure portal ([https://portal.azure.com/](https://portal.azure.com/)).
        2. In the left-hand menu, click on "Virtual machines".
        3. Select the virtual machine that has unattached disk volumes.
        4. In the virtual machine's overview page, scroll down to the "Disks" section and click on it.
        5. You will see a list of all the disks attached to the virtual machine. Identify the unattached disk volumes that need to be removed.
        6. Click on the unattached disk volume that you want to remove.
        7. In the disk's overview page, click on the "Delete" button at the top of the page.
        8. A confirmation message will appear. Click on "Yes" to confirm the deletion of the unattached disk volume.
        9. Repeat steps 6-8 for all the unattached disk volumes that need to be removed.

        That's it! You have successfully remediated the "Remove Unattached Virtual Machine Disk Volumes" misconfiguration for Azure using Azure console.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To remediate the misconfiguration "Remove Unattached Virtual Machine Disk Volumes" for Azure using Azure CLI, follow the below steps:

        1. Open the Azure CLI on your local machine or use the Azure Cloud Shell.

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

        3. Once you are logged in, set the subscription context where the virtual machines are located using the command:
           ```
           az account set --subscription <subscription_id>
           ```
           Replace `<subscription_id>` with the ID of the subscription where the virtual machines are located.

        4. Now, list all the virtual machines in the subscription using the command:
           ```
           az vm list --query "[].{Name:name, ResourceGroup:resourceGroup, Location:location}" --output table
           ```

        5. Identify the virtual machine for which you want to remove the unattached disk volumes.

        6. Run the following command to list all the disk volumes attached to the virtual machine:
           ```
           az vm show -g <resource_group_name> -n <vm_name> --query "storageProfile.dataDisks[].name" --output table
           ```
           Replace `<resource_group_name>` with the name of the resource group where the virtual machine is located and `<vm_name>` with the name of the virtual machine.

        7. Identify any unattached disk volumes in the output of the above command.

        8. To remove the unattached disk volumes, run the following command:
           ```
           az disk delete --ids <disk_id>
           ```
           Replace `<disk_id>` with the ID of the unattached disk volume that you want to remove.

        9. Repeat steps 6 to 8 for all the unattached disk volumes that you want to remove.

        10. Once you have removed all the unattached disk volumes, verify that they have been removed by running the command in step 6 again.

        11. Exit the Azure CLI using the command:

        ```
        exit
        ```

        By following the above steps, you can remediate the misconfiguration "Remove Unattached Virtual Machine Disk Volumes" for Azure using Azure CLI.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the misconfiguration of removing unattached virtual machine disk volumes in Azure using Python, you can follow the below steps:

        Step 1: Import the required libraries

        ```
        from azure.identity import DefaultAzureCredential
        from azure.mgmt.compute import ComputeManagementClient
        ```

        Step 2: Set the subscription ID and resource group name where the virtual machines are located.

        ```
        subscription_id = 'your-subscription-id'
        resource_group_name = 'your-resource-group-name'
        ```

        Step 3: Authenticate the client using the DefaultAzureCredential.

        ```
        credential = DefaultAzureCredential()
        compute_client = ComputeManagementClient(credential, subscription_id)
        ```

        Step 4: Get the list of virtual machines in the resource group.

        ```
        virtual_machines = compute_client.virtual_machines.list(resource_group_name)
        ```

        Step 5: Iterate through each virtual machine and get the list of disk volumes.

        ```
        for vm in virtual_machines:
            disk_volumes = compute_client.disks.list_by_vm(resource_group_name, vm.name)
        ```

        Step 6: Check if the disk volume is attached to the virtual machine or not. If it is not attached, delete the disk volume.

        ```
            for disk in disk_volumes:
                if not disk.managed_by and not disk.disk_state:
                    compute_client.disks.delete(resource_group_name, disk.name)
        ```

        Step 7: Run the script to remove the unattached virtual machine disk volumes.

        Note: Make sure you have the necessary permissions to delete the disk volumes.

        The above steps will help you remediate the misconfiguration of removing unattached virtual machine disk volumes in Azure using Python.
      </Accordion>

      <Accordion title="Using Terraform">
        ```hcl theme={null}
        # Example of an attached managed disk (kept)
        resource "azurerm_managed_disk" "APP_SERVER_DATA_DISK" {
          name                 = "APP-SERVER-DATA-DISK"
          location             = azurerm_resource_group.MY_RG.location
          resource_group_name  = azurerm_resource_group.MY_RG.name
          storage_account_type = "Premium_LRS"
          create_option        = "Empty"
          disk_size_gb         = 128
        }

        resource "azurerm_virtual_machine_data_disk_attachment" "APP_SERVER_DATA_DISK_ATTACH" {
          managed_disk_id    = azurerm_managed_disk.APP_SERVER_DATA_DISK.id
          virtual_machine_id = azurerm_windows_virtual_machine.APP_SERVER.id
          lun                = 0
          caching            = "ReadWrite"
        }

        # Example of an UNATTACHED managed disk to be removed:
        # Previously you might have had something like this:

        # resource "azurerm_managed_disk" "UNATTACHED_OLD_DISK" {
        #   name                 = "UNATTACHED-OLD-DISK"
        #   location             = azurerm_resource_group.MY_RG.location
        #   resource_group_name  = azurerm_resource_group.MY_RG.name
        #   storage_account_type = "Standard_LRS"
        #   create_option        = "Empty"
        #   disk_size_gb         = 64
        # }

        # To remediate, delete the entire azurerm_managed_disk.UNATTACHED_OLD_DISK
        # resource block from your Terraform configuration (do NOT reference it from
        # any azurerm_virtual_machine_data_disk_attachment or VM resource).

        # Placeholders to substitute:
        # - MY_RG: the name of your azurerm_resource_group resource
        # - APP_SERVER_DATA_DISK / APP_SERVER_DATA_DISK_ATTACH / APP_SERVER: names for your resources

        ```

        Deleting the `azurerm_managed_disk` block for an unattached disk from Terraform and applying will destroy that disk in Azure; this is irreversible and data on that disk will be permanently lost.

        Verification: `terraform plan` should show a `-` destroy action only for the unwanted unattached `azurerm_managed_disk` resources, with no changes to disks that are still attached to virtual machines.
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>
