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

# Accelerated networking vm remediation

### Triage and Remediation

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

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        To remediate the misconfiguration "Enable Accelerated Networking for Virtual Machines" in Azure using the Azure console, follow these steps:

        1. Log in to the Azure portal.
        2. Navigate to the Virtual Machine that needs to be configured.
        3. Click on the "Networking" option in the left-hand menu.
        4. Click on the "Enable" button next to "Accelerated Networking."
        5. Choose the appropriate network interface and click "Save."

        Once you have completed these steps, the Accelerated Networking feature will be enabled for the virtual machine.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To enable Accelerated Networking for Virtual Machines in Azure using Azure CLI, follow the below steps:

        1. Open the Azure CLI in your terminal or Azure Cloud Shell.

        2. Run the following command to check if Accelerated Networking is already enabled for the VM:

           ```
           az vm show -g <resource-group-name> -n <vm-name> --query networkProfile.networkInterface.id
           ```

        3. Once you have verified that Accelerated Networking is not enabled, run the following command to enable it:

           ```
           az vm accerated-networking enable -g <resource-group-name> -n <vm-name>
           ```

        4. Wait for a few minutes for the changes to take effect.

        5. Run the following command to verify that Accelerated Networking is enabled for the VM:

           ```
           az vm show -g <resource-group-name> -n <vm-name> --query "networkProfile.networkInterfaces[0].enableAcceleratedNetworking"
           ```

           If the output is "true", then Accelerated Networking has been successfully enabled for the VM.

        That's it! You have successfully remediated the misconfiguration by enabling Accelerated Networking for Virtual Machines in Azure using Azure CLI.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the misconfiguration of enabling Accelerated Networking for Virtual Machines in Azure using Python, you can use the Azure Python SDK. Here are the steps to follow:

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

        ```
        pip install azure-mgmt-compute
        ```

        2. Authenticate with Azure by creating a Service Principal and assigning it the Contributor role for the subscription. You can do this using the Azure CLI or the Azure Portal.

        3. Import the necessary modules in your Python script:

        ```python theme={null}
        from azure.common.credentials import ServicePrincipalCredentials
        from azure.mgmt.compute import ComputeManagementClient
        ```

        4. Set the credentials for the Service Principal:

        ```python theme={null}
        TENANT_ID = '<your-tenant-id>'
        CLIENT_ID = '<your-client-id>'
        CLIENT_SECRET = '<your-client-secret>'
        SUBSCRIPTION_ID = '<your-subscription-id>'

        credentials = ServicePrincipalCredentials(
            client_id=CLIENT_ID,
            secret=CLIENT_SECRET,
            tenant=TENANT_ID
        )
        ```

        5. Create a ComputeManagementClient object using the credentials:

        ```python theme={null}
        compute_client = ComputeManagementClient(
            credentials=credentials,
            subscription_id=SUBSCRIPTION_ID
        )
        ```

        6. Get a list of all the virtual machines in the subscription:

        ```python theme={null}
        vm_list = compute_client.virtual_machines.list_all()
        ```

        7. For each virtual machine, check if Accelerated Networking is already enabled. If not, enable it:

        ```python theme={null}
        for vm in vm_list:
            if vm.network_profile.network_interfaces:
                for nic in vm.network_profile.network_interfaces:
                    nic_obj = compute_client.network_interfaces.get(
                        nic.id.split('/')[4],
                        nic.id.split('/')[8]
                    )
                    if not nic_obj.enable_accelerated_networking:
                        nic_obj.enable_accelerated_networking = True
                        compute_client.network_interfaces.create_or_update(
                            nic.id.split('/')[4],
                            nic.id.split('/')[8],
                            nic_obj
                        )
        ```

        8. Save and run the Python script. This will enable Accelerated Networking for all the virtual machines in the subscription that do not have it enabled.

        Note: Make sure to test this in a non-production environment before implementing it in a production environment.
      </Accordion>

      <Accordion title="Using Terraform">
        ```hcl theme={null}
        # Network Interface with Accelerated Networking enabled
        resource "azurerm_network_interface" "VM_NIC" {
          name                = "VM_NIC_NAME"              # replace with your NIC name
          location            = azurerm_resource_group.RG.location
          resource_group_name = azurerm_resource_group.RG.name

          enable_accelerated_networking = true

          ip_configuration {
            name                          = "PRIMARY_IPCONFIG"
            subnet_id                     = azurerm_subnet.SUBNET.id
            private_ip_address_allocation = "Dynamic"
            public_ip_address_id          = azurerm_public_ip.VM_PUBLIC_IP.id
          }
        }

        # Example Linux VM using the NIC above
        resource "azurerm_linux_virtual_machine" "VM" {
          name                = "VM_NAME"                  # replace with your VM name
          location            = azurerm_resource_group.RG.location
          resource_group_name = azurerm_resource_group.RG.name
          size                = "VM_SIZE"                  # must be a size that supports accelerated networking
          admin_username      = "ADMIN_USERNAME"           # set appropriately

          network_interface_ids = [
            azurerm_network_interface.VM_NIC.id,
          ]

          os_disk {
            caching              = "ReadWrite"
            storage_account_type = "Standard_LRS"
          }

          source_image_reference {
            publisher = "PUBLISHER"                        # e.g. "Canonical"
            offer     = "OFFER"                            # e.g. "UbuntuServer"
            sku       = "SKU"                              # e.g. "18.04-LTS"
            version   = "latest"
          }

          admin_ssh_key {
            username   = "ADMIN_USERNAME"                  # must match admin_username
            public_key = file("PATH_TO_PUBLIC_KEY")        # replace with path to your SSH public key
          }
        }
        ```

        Enabling `enable_accelerated_networking = true` on an existing NIC forces replacement of that NIC; if it is the VM’s primary NIC, the VM will also be recreated, causing downtime.

        For verification, `terraform plan` should show an update (or replacement) to `azurerm_network_interface.VM_NIC` with `enable_accelerated_networking` changing from `false` (or null) to `true`, and the VM still referencing this NIC.
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>
