> ## 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 Autoscale Notifications

### More Info:

Ensure that email or webhook-based alert notifications are enabled for your Microsoft Azure virtual machine scale sets in order to get notified for successful or failed autoscale actions. Email notifications can be sent to any valid email address that you configure. Administrators and co-administrators of the Azure subscription where the virtual machine scale set is running will also be notified. Webhooks allow you to route the autoscale alert notifications to other systems for post-processing or custom notifications.

### Risk Level

Medium

### Address

Security

### Compliance Standards

* Cloudanix Best Practice

### Triage and Remediation

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

    <AccordionGroup>
      <Accordion title="Using Console" defaultOpen="true">
        To remediate the misconfiguration of enabling Autoscale Notifications in Azure, you can follow the below steps using the Azure console:

        1. Go to the Azure portal and navigate to the Autoscale settings of the resource group that you want to remediate.

        2. In the Autoscale settings, select the resource that you want to enable notifications for.

        3. Click on the "Notifications" tab, and then click "Add notification".

        4. Select the notification type that you want to use. You can choose from Email, SMS, Webhook, or Azure Function.

        5. Enter the details of the notification, such as the email address or phone number.

        6. Click "Save" to save the notification.

        7. Repeat steps 4-6 to add additional notifications if needed.

        8. Once you have added all the notifications that you need, click "Save" to save the Autoscale settings.

        By following these steps, you will be able to remediate the misconfiguration of enabling Autoscale Notifications in Azure.

        #
      </Accordion>

      <Accordion title="Using CLI">
        To remediate the misconfiguration of enabling Autoscale Notifications in AZURE using AZURE CLI, you can follow the below step by step instructions:

        1. Open the AZURE CLI and login to your AZURE account using the below command:

           `az login`

        2. Once you are logged in, set the subscription where you want to enable the Autoscale Notifications using the below command:

           `az account set --subscription <subscription_id>`

        3. Now, enable the Autoscale Notifications for your AZURE account using the below command:

           `az monitor autoscale update --resource-group <resource_group_name> --name <autoscale_setting_name> --email-administrator <email_address> --verbose`

           Here, replace the `<resource_group_name>` with the name of the resource group where your Autoscale setting is located, `<autoscale_setting_name>` with the name of the Autoscale setting that you want to update and `<email_address>` with the email address where you want to receive the notifications.

        4. After running the above command, you will receive a confirmation message that the Autoscale Notifications have been enabled for your account.

        By following the above steps, you can remediate the misconfiguration of enabling Autoscale Notifications in AZURE using AZURE CLI.
      </Accordion>

      <Accordion title="Using Python">
        To remediate the misconfiguration of enabling autoscale notifications 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.common.credentials import ServicePrincipalCredentials
        ```

        2. Set up the credentials for authentication:

        ```python theme={null}
        subscription_id = '<Subscription ID>'
        client_id = '<Client ID>'
        client_secret = '<Client Secret>'
        tenant_id = '<Tenant ID>'

        credentials = ServicePrincipalCredentials(
            client_id=client_id,
            secret=client_secret,
            tenant=tenant_id
        )
        ```

        3. Instantiate the `MonitorManagementClient`:

        ```python theme={null}
        monitor_client = MonitorManagementClient(credentials, subscription_id)
        ```

        4. Get the autoscale settings:

        ```python theme={null}
        autoscale_settings = monitor_client.autoscale_settings.list()
        ```

        5. For each autoscale setting, check if notifications are enabled. If not, enable them:

        ```python theme={null}
        for setting in autoscale_settings:
            if not setting.notification:
                setting.notification = {}
                setting.notification.email = {}
                setting.notification.email.send_to_subscription_administrator = True
                monitor_client.autoscale_settings.create_or_update(
                    resource_group_name=setting.id.split('/')[4],
                    autoscale_setting_name=setting.name,
                    parameters=setting
                )
        ```

        6. Verify that notifications have been enabled by checking the `notification` attribute of each autoscale setting.

        By following these steps, you can remediate the misconfiguration of enabling autoscale notifications in Azure using Python.
      </Accordion>

      <Accordion title="Using Terraform">
        ```hcl theme={null}
        # Existing VM Scale Set (example)
        resource "azurerm_linux_virtual_machine_scale_set" "VMSS_NAME" {
          name                = "VMSS_NAME"
          resource_group_name = azurerm_resource_group.RG_NAME.name
          location            = azurerm_resource_group.RG_NAME.location
          sku                 = "Standard_DS2_v2"
          instances           = 2
          # ...other required arguments...
        }

        # Autoscale configuration with notifications for this VM Scale Set
        resource "azurerm_monitor_autoscale_setting" "VMSS_NAME_autoscale" {
          name                = "VMSS_NAME-autoscale"
          resource_group_name = azurerm_resource_group.RG_NAME.name
          location            = azurerm_resource_group.RG_NAME.location
          target_resource_id  = azurerm_linux_virtual_machine_scale_set.VMSS_NAME.id

          profiles {
            name = "default-profile"

            capacity {
              default = 2         # SUBSTITUTE: DEFAULT_INSTANCE_COUNT
              minimum = 1         # SUBSTITUTE: MIN_INSTANCE_COUNT
              maximum = 5         # SUBSTITUTE: MAX_INSTANCE_COUNT
            }

            # Example scale-out rule (SUBSTITUTE threshold & metric as needed)
            rule {
              metric_trigger {
                metric_name        = "Percentage CPU"
                metric_resource_id = azurerm_linux_virtual_machine_scale_set.VMSS_NAME.id
                time_grain         = "PT1M"
                statistic          = "Average"
                time_window        = "PT5M"
                time_aggregation   = "Average"
                operator           = "GreaterThan"
                threshold          = 75  # SUBSTITUTE: SCALE_OUT_THRESHOLD
              }

              scale_action {
                direction = "Increase"
                type      = "ChangeCount"
                value     = "1"
                cooldown  = "PT5M"
              }
            }

            # Example scale-in rule
            rule {
              metric_trigger {
                metric_name        = "Percentage CPU"
                metric_resource_id = azurerm_linux_virtual_machine_scale_set.VMSS_NAME.id
                time_grain         = "PT1M"
                statistic          = "Average"
                time_window        = "PT5M"
                time_aggregation   = "Average"
                operator           = "LessThan"
                threshold          = 30  # SUBSTITUTE: SCALE_IN_THRESHOLD
              }

              scale_action {
                direction = "Decrease"
                type      = "ChangeCount"
                value     = "1"
                cooldown  = "PT5M"
              }
            }
          }

          notification {
            email {
              send_to_subscription_administrator    = true
              send_to_subscription_co_administrator = true
              custom_emails = [
                "AUTOSCALE_ALERTS_EMAIL@example.com", # SUBSTITUTE: ADDRESSES TO NOTIFY
              ]
            }

            webhook {
              service_uri = "https://WEBHOOK_ENDPOINT_URL" # SUBSTITUTE: WEBHOOK URL
              properties = {
                source = "vmss-autoscale"
                env    = "ENVIRONMENT_NAME"
              }
            }
          }

          tags = {
            environment = "ENVIRONMENT_NAME" # SUBSTITUTE
          }
        }
        ```

        This change adds a separate autoscale setting with email and webhook notifications and does not force replacement of the existing virtual machine scale set. After updating the configuration, `terraform plan` should show a new `azurerm_monitor_autoscale_setting` resource being created and no changes requiring replacement of the VM scale set.
      </Accordion>
    </AccordionGroup>
  </Tab>
</Tabs>
