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

# Associated load balancer remediation

### Triage and Remediation

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

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        To remediate the misconfiguration of VM Scale Sets not being integrated with Load Balancers in AZURE, follow the below steps:

        1. Login to the AZURE portal ([https://portal.azure.com/](https://portal.azure.com/)).
        2. Navigate to the "Virtual machine scale sets" option in the left-hand menu.
        3. Select the VM scale set that you want to integrate with a load balancer.
        4. Click on the "Networking" option under the "Settings" section.
        5. In the "Networking" section, click on the "Add inbound NAT rule" option.
        6. In the "Add inbound NAT rule" window, select the "Load balancer" option.
        7. Select the load balancer that you want to integrate with the VM scale set.
        8. Select the backend port and protocol for the VM instances.
        9. Select the frontend IP configuration for the load balancer.
        10. Click on the "Add" button to save the changes.

        After following these steps, the VM scale set will be integrated with the selected load balancer.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To remediate the misconfiguration of VM Scale Sets not being integrated with Load Balancers in AZURE, you can follow the below steps using AZURE CLI:

        1. First, you need to create a Load Balancer using the following command:

           ```
           az network lb create --name <load_balancer_name> --resource-group <resource_group_name> --location <location>
           ```

           Replace the `<load_balancer_name>`, `<resource_group_name>`, and `<location>` placeholders with the appropriate values.

        2. Next, you need to create a backend pool for the VM Scale Set using the following command:

           ```
           az network lb address-pool create --name <backend_pool_name> --lb-name <load_balancer_name> --resource-group <resource_group_name>
           ```

           Replace the `<backend_pool_name>`, `<load_balancer_name>`, and `<resource_group_name>` placeholders with the appropriate values.

        3. Now, you need to add the VM Scale Set instances to the backend pool using the following command:

           ```
           az network nic ip-config address-pool add --address-pool <backend_pool_name> --ip-config-name ipconfig1 --nic-name <nic_name> --resource-group <resource_group_name>
           ```

           Replace the `<backend_pool_name>`, `<nic_name>`, and `<resource_group_name>` placeholders with the appropriate values.

        4. Finally, you need to configure the VM Scale Set to use the Load Balancer by updating the Load Balancer ID in the VM Scale Set configuration using the following command:

           ```
           az vmss update --name <vmss_name> --resource-group <resource_group_name> --set virtualMachineProfile.networkProfile.networkInterfaceConfigurations[0].ipConfigurations[0].loadBalancerBackendAddressPools=[{'id':'/subscriptions/<subscription_id>/resourceGroups/<resource_group_name>/providers/Microsoft.Network/loadBalancers/<load_balancer_name>/backendAddressPools/<backend_pool_name>'}]
           ```

           Replace the `<vmss_name>`, `<resource_group_name>`, `<subscription_id>`, `<load_balancer_name>`, and `<backend_pool_name>` placeholders with the appropriate values.

        After following these steps, your VM Scale Set will be integrated with the Load Balancer in AZURE.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the misconfiguration "VM Scale Sets Should Be Integrated With Load Balancers" for Azure using Python, you can follow the below steps:

        Step 1: Import the required modules and authenticate to Azure using the Python SDK.

        ```python theme={null}
        from azure.identity import AzureCliCredential
        from azure.mgmt.compute import ComputeManagementClient
        from azure.mgmt.network import NetworkManagementClient

        credential = AzureCliCredential()
        compute_client = ComputeManagementClient(credential, subscription_id)
        network_client = NetworkManagementClient(credential, subscription_id)
        ```

        Step 2: Get the list of VM Scale Sets that are not integrated with Load Balancers.

        ```python theme={null}
        vmss_list = compute_client.virtual_machine_scale_sets.list()

        for vmss in vmss_list:
            if vmss.sku.capacity > 0:
                lb = network_client.load_balancers.list()
                if lb:
                    for lb in lb:
                        if lb.backend_address_pools:
                            for pool in lb.backend_address_pools:
                                if pool.virtual_machine_scale_set:
                                    if pool.virtual_machine_scale_set.id == vmss.id:
                                        break
                            else:
                                network_client.load_balancers.create_or_update(
                                    resource_group_name=resource_group_name,
                                    load_balancer_name=load_balancer_name,
                                    parameters={
                                        "location": location,
                                        "frontend_ip_configurations": [{
                                            "name": frontend_ip_configuration_name,
                                            "public_ip_address": {
                                                "id": public_ip_address_id
                                            }
                                        }],
                                        "backend_address_pools": [{
                                            "name": backend_address_pool_name,
                                            "virtual_machine_scale_set": {
                                                "id": vmss.id
                                            }
                                        }]
                                    }
                                )
        ```

        Step 3: If a VM Scale Set is not integrated with a Load Balancer, create a new Load Balancer and integrate it with the VM Scale Set.

        ```python theme={null}
        network_client.load_balancers.create_or_update(
            resource_group_name=resource_group_name,
            load_balancer_name=load_balancer_name,
            parameters={
                "location": location,
                "frontend_ip_configurations": [{
                    "name": frontend_ip_configuration_name,
                    "public_ip_address": {
                        "id": public_ip_address_id
                    }
                }],
                "backend_address_pools": [{
                    "name": backend_address_pool_name,
                    "virtual_machine_scale_set": {
                        "id": vmss.id
                    }
                }]
            }
        )
        ```

        By following these steps, you can remediate the misconfiguration "VM Scale Sets Should Be Integrated With Load Balancers" for Azure using Python.
      </Accordion>

      <Accordion title="Using Terraform">
        ```hcl theme={null}
        # Azure Load Balancer for the scale set
        resource "azurerm_lb" "VMSS_LB" {
          name                = "VMSS-LB"
          location            = azurerm_resource_group.RG.location
          resource_group_name = azurerm_resource_group.RG.name
          sku                 = "Standard"

          frontend_ip_configuration {
            name                 = "PublicFrontEnd"
            public_ip_address_id = azurerm_public_ip.VMSS_LB_PIP.id
          }
        }

        resource "azurerm_public_ip" "VMSS_LB_PIP" {
          name                = "VMSS-LB-PIP"
          location            = azurerm_resource_group.RG.location
          resource_group_name = azurerm_resource_group.RG.name
          allocation_method   = "Static"
          sku                 = "Standard"
        }

        resource "azurerm_lb_backend_address_pool" "VMSS_BEP" {
          name            = "VMSS-BackendPool"
          loadbalancer_id = azurerm_lb.VMSS_LB.id
        }

        # Optional: health probe and LB rule (typical HTTP example)
        resource "azurerm_lb_probe" "VMSS_PROBE" {
          name                = "http-probe"
          resource_group_name = azurerm_resource_group.RG.name
          loadbalancer_id     = azurerm_lb.VMSS_LB.id
          protocol            = "Tcp"
          port                = 80
        }

        resource "azurerm_lb_rule" "VMSS_LB_RULE" {
          name                           = "http-rule"
          resource_group_name            = azurerm_resource_group.RG.name
          loadbalancer_id                = azurerm_lb.VMSS_LB.id
          protocol                       = "Tcp"
          frontend_port                  = 80
          backend_port                   = 80
          disable_outbound_snat          = true
          frontend_ip_configuration_name = "PublicFrontEnd"
          backend_address_pool_ids       = [azurerm_lb_backend_address_pool.VMSS_BEP.id]
          probe_id                       = azurerm_lb_probe.VMSS_PROBE.id
        }

        # VM Scale Set integrated with the Load Balancer
        resource "azurerm_linux_virtual_machine_scale_set" "VMSS" {
          name                = "VMSS-NAME"
          location            = azurerm_resource_group.RG.location
          resource_group_name = azurerm_resource_group.RG.name
          sku                 = "Standard_DS1_v2"
          instances           = 2
          admin_username      = "ADMIN_USERNAME"
          admin_password      = "ADMIN_PASSWORD" # or use ssh_keys

          source_image_reference {
            publisher = "Canonical"
            offer     = "0001-com-ubuntu-server-focal"
            sku       = "20_04-lts"
            version   = "latest"
          }

          network_interface {
            name    = "vmss-nic"
            primary = true

            ip_configuration {
              name      = "internal"
              primary   = true
              subnet_id = azurerm_subnet.VMSS_SUBNET.id

              # This is the key setting that integrates the scale set with the LB
              load_balancer_backend_address_pool_ids = [
                azurerm_lb_backend_address_pool.VMSS_BEP.id
              ]
            }
          }

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

        # Supporting network resources (replace placeholders)
        resource "azurerm_resource_group" "RG" {
          name     = "RG_NAME"      # replace with your resource group name
          location = "REGION_NAME"  # replace with your Azure region
        }

        resource "azurerm_virtual_network" "VNET" {
          name                = "VNET-NAME"       # replace
          address_space       = ["10.0.0.0/16"]
          location            = azurerm_resource_group.RG.location
          resource_group_name = azurerm_resource_group.RG.name
        }

        resource "azurerm_subnet" "VMSS_SUBNET" {
          name                 = "VMSS-Subnet"
          resource_group_name  = azurerm_resource_group.RG.name
          virtual_network_name = azurerm_virtual_network.VNET.name
          address_prefixes     = ["10.0.1.0/24"]
        }
        ```

        Replace:

        * `RG_NAME` with your resource group name.
        * `REGION_NAME` with your Azure region.
        * `VNET-NAME` with your virtual network name.
        * `ADMIN_USERNAME` / `ADMIN_PASSWORD` with appropriate credentials or configure SSH keys instead.

        This change does not force replacement of the scale set resource itself, but updating `network_interface` / `ip_configuration` causes rollout operations on the scale set instances (VMs may be recreated or reimaged during apply).

        To verify, `terraform plan` should show:

        * creation of `azurerm_lb`, `azurerm_lb_backend_address_pool`, and related resources, if new.
        * an update on `azurerm_linux_virtual_machine_scale_set.VMSS` adding `load_balancer_backend_address_pool_ids` under the NIC `ip_configuration`.
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>
