> ## 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 and Configure Health Monitoring

### More Info:

Ensure that Monitor Application Health feature is enabled for all the instances running within your Azure virtual machine scale set. Health monitoring via Application Health extension is required for OS upgrades and automatic instance repairs. The Azure Application Health extension reports on the application health from inside the virtual machine scale set instances. You can configure the health extension to probe on an application endpoint and update the status of the application on that instance.

### Risk Level

Medium

### Address

Security

### Compliance Standards

* APRA CPS 234 (Australia)
* BSI C5 (Germany)
* Brazil LGPD
* CCPA / CPRA (California)
* CIS Critical Security Controls v8
* CMMC 2.0
* CSA Cloud Controls Matrix v4
* DPDPA
* Digital Operational Resilience Act (EU)
* HIPAA
* ISO/IEC 27017
* ISO/IEC 27018
* ISO/IEC 27701
* KSA PDPL
* MAS Technology Risk Management (Singapore)
* MITRE ATT\&CK (Cloud)
* NIS2 Directive
* NIST SP 800-171
* NYDFS 23 NYCRR 500
* Reserve Bank of India (RBI) Master Direction – Information Technology Framework
* SWIFT Customer Security Controls Framework
* Sarbanes-Oxley IT General Controls
* Securities and Exchange Board of India (SEBI) - Cloud Security Adoption Framework
* UK NCSC Cyber Assessment Framework

### Triage and Remediation

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

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        To remediate the misconfiguration "Enable and Configure Health Monitoring" in Azure using the Azure console, please follow the below steps:

        Step 1: Login to the Azure portal ([https://portal.azure.com/](https://portal.azure.com/)).

        Step 2: Select the resource group in which your virtual machine or application is located.

        Step 3: Select the virtual machine or application for which you want to enable health monitoring.

        Step 4: Under the Monitoring section, select "Health check".

        Step 5: Click on "Add health check".

        Step 6: Select the protocol (HTTP or HTTPS) and enter the URL for which you want to enable health monitoring.

        Step 7: Configure the health check settings, including the frequency of the health check, the timeout value, and the number of retries.

        Step 8: Click on "Save" to save the health check configuration.

        Step 9: Verify that the health check is enabled and working properly by checking the status of the health check in the Azure portal.

        By following these steps, you can remediate the misconfiguration "Enable and Configure Health Monitoring" in Azure using the Azure console.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To enable and configure health monitoring in Azure using Azure CLI, follow these steps:

        1. Open the Azure CLI and log in to your Azure account.

        2. Run the command `az monitor metrics alert create` to create a new alert rule.

        3. Provide the required parameters for the alert rule, such as the resource group and resource name for the resource you want to monitor, the condition that triggers the alert, and the action to take when the alert is triggered.

        4. Use the `--enabled` parameter to enable the alert rule.

        5. Use the `--description` parameter to provide a description for the alert rule.

        6. Use the `--tags` parameter to add any tags you want to the alert rule.

        7. Run the command `az monitor metrics alert show` to verify that the alert rule was created successfully.

        8. Use the `az monitor metrics alert update` command to modify the alert rule if necessary.

        9. Use the `az monitor metrics alert delete` command to delete the alert rule if it is no longer needed.

        Note: This is just a basic outline of the steps required to enable and configure health monitoring in Azure using Azure CLI. The specific commands and parameters you need to use may vary depending on your specific requirements and environment. Please refer to the Azure documentation for more detailed guidance on how to remediate this misconfiguration.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the misconfiguration of enabling and configuring health monitoring in Azure using Python, you can follow the below steps:

        1. Import the necessary libraries:

        ```python theme={null}
        from azure.mgmt.monitor import MonitorManagementClient
        from azure.identity import ClientSecretCredential
        ```

        2. Authenticate with Azure using the `ClientSecretCredential` class:

        ```python theme={null}
        TENANT_ID = 'your_tenant_id'
        CLIENT_ID = 'your_client_id'
        CLIENT_SECRET = 'your_client_secret'
        credential = ClientSecretCredential(TENANT_ID, CLIENT_ID, CLIENT_SECRET)
        ```

        3. Initialize the `MonitorManagementClient` using the `credential` object:

        ```python theme={null}
        SUBSCRIPTION_ID = 'your_subscription_id'
        monitor_client = MonitorManagementClient(credential, SUBSCRIPTION_ID)
        ```

        4. Get the resource group and resource ID where you want to enable health monitoring:

        ```python theme={null}
        RESOURCE_GROUP_NAME = 'your_resource_group_name'
        RESOURCE_NAME = 'your_resource_name'
        ```

        5. Enable health monitoring for the resource using the `monitor_client` object:

        ```python theme={null}
        monitor_client.service_diagnostic_settings.create_or_update(
            resource_group_name=RESOURCE_GROUP_NAME,
            resource_uri=f"/subscriptions/{SUBSCRIPTION_ID}/resourceGroups/{RESOURCE_GROUP_NAME}/providers/Microsoft.Compute/virtualMachines/{RESOURCE_NAME}",
            parameters={
                "storage_account_id": "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{storageAccountName}",
                "metrics": {
                    "enabled": True,
                    "retentionPolicy": {
                        "enabled": True,
                        "days": 7
                    }
                },
                "logs": {
                    "enabled": True,
                    "retentionPolicy": {
                        "enabled": True,
                        "days": 7
                    }
                }
            }
        )
        ```

        6. Verify that health monitoring is enabled by checking the diagnostic settings:

        ```python theme={null}
        monitor_client.service_diagnostic_settings.get(
            resource_group_name=RESOURCE_GROUP_NAME,
            resource_uri=f"/subscriptions/{SUBSCRIPTION_ID}/resourceGroups/{RESOURCE_GROUP_NAME}/providers/Microsoft.Compute/virtualMachines/{RESOURCE_NAME}",
            name='default'
        )
        ```

        This should remediate the misconfiguration of enabling and configuring health monitoring in Azure using Python.
      </Accordion>

      <Accordion title="Using Terraform">
        ```hcl theme={null}
        # Existing VM scale set (substitute with your real VMSS resource)
        resource "azurerm_windows_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                 = "Standard_DS2_v2"
          instances           = 3

          # ...other required VMSS config...
        }

        # Enable Application Health monitoring via extension on the VM scale set
        resource "azurerm_virtual_machine_scale_set_extension" "VMSS_APP_HEALTH" {
          name                         = "ApplicationHealth"
          virtual_machine_scale_set_id = azurerm_windows_virtual_machine_scale_set.VMSS_NAME.id

          publisher            = "Microsoft.Azure.Monitoring"
          type                 = "ApplicationHealthWindows" # for Linux use "ApplicationHealthLinux"
          type_handler_version = "1.0"
          auto_upgrade_minor_version = true

          # Configure the health probe endpoint INSIDE the VM instances
          # Substitute values as appropriate for your app:
          settings = jsonencode({
            "protocol"     = "http"          # "http" or "https"
            "port"         = 80              # replace with your app port
            "requestPath"  = "/healthz"      # replace with your health endpoint path
            "intervalInSeconds" = 30         # how often to probe
            "numberOfProbes"    = 3          # how many failed probes before unhealthy
          })
        }
        ```

        This change does not force replacement of the VM scale set; Terraform will add/update the extension in place on the existing instances.

        To verify, `terraform plan` should show a new `azurerm_virtual_machine_scale_set_extension.VMSS_APP_HEALTH` resource being created (or its `settings` updated) attached to the target scale set, with no `destroy`/`create` cycle for the VMSS itself.
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>
