Wednesday, 29 January 2025

Setup Azure File Share Capacity Alert to Slack with Terraform

 Setting up an Azure File Share capacity alert is useful to know when you reach at least 80% of allocated quota for the file share. This will give the teams ample time to increase the allocation to avoid out of space issues. If we are using standard tier for storage account then we need to use one storage account for each file share, to get the correct alert. Sending the alert to slack channel is a useful way to get properly alerted to take action on time. Let's use an example learn how to setup alerts for multiple Azure file shares uing terraform.

Expectation is to get the alerts to slack channel as shown below.


Assume there are two storage accounts and each contains a single Azure file share. Since standard tier storage accounts do not support metrics dimentions per file share we have to keep our file shares in seperate storage accounts, meaning we can only have one file share in each storage account as recomended in here. Below terraform code creates two storage accounts and a file share in each account.

terraform {
  backend "local" {
    path = "terraform.tfstate"
  }
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=4.16.0"
    }
    azuread = {
      source  = "hashicorp/azuread"
      version = "=3.1.0"
    }
  }
}

provider "azurerm" {
  features {}
  subscription_id = "subscriptionid"
}

resource "azurerm_resource_group" "instance_rg" {
  name     = "ch-alerttest-dev-eus-001-rg"
  location = "eastus"
}

resource "azurerm_storage_account" "filestorage_aks_win" {
  name                             = "chatdeveus001akswfsgreen"
  location                         = azurerm_resource_group.instance_rg.location
  resource_group_name              = azurerm_resource_group.instance_rg.name
  account_tier                     = "Standard"
  account_replication_type         = "LRS"
  account_kind                     = "StorageV2"
  access_tier                      = "Hot"
  allow_nested_items_to_be_public  = false
  min_tls_version                  = "TLS1_2"
  cross_tenant_replication_enabled = false

  network_rules {
    default_action = "Deny"
    bypass         = ["Metrics", "AzureServices", "Logging"]
    ip_rules       = ["46.15.119.116"]
  }
}

resource "azurerm_storage_account" "filestorage_aks_linux" {
  name                             = "chatdeveus001akslfsgreen"
  location                         = azurerm_resource_group.instance_rg.location
  resource_group_name              = azurerm_resource_group.instance_rg.name
  account_tier                     = "Standard"
  account_replication_type         = "LRS"
  account_kind                     = "StorageV2"
  access_tier                      = "Hot"
  allow_nested_items_to_be_public  = false
  min_tls_version                  = "TLS1_2"
  cross_tenant_replication_enabled = false

  network_rules {
    default_action = "Deny"
    bypass         = ["Metrics", "AzureServices", "Logging"]
    ip_rules       = ["46.15.119.116"]
  }
}

resource "azurerm_storage_share" "aks_windows" {
  name               = "akswindowsfileshare"
  storage_account_id = azurerm_storage_account.filestorage_aks_win.id
  access_tier        = "Hot"
  quota              = 200 # Size in GB
}

resource "azurerm_storage_share" "aks_linux" {
  name               = "akslinuxfileshare"
  storage_account_id = azurerm_storage_account.filestorage_aks_linux.id
  access_tier        = "Hot"
  quota              = 100 # Size in GB
}


The next step would be to cerate an action group to send an email. First we have to get the email address of the slack channel. We can get it from slack channel properties windows, integrations tab.



Then we can use copied email adress and create an action group in Azure  with terraform as shown below.

# Action group
resource "azurerm_monitor_action_group" "fileshare_capacity_action" {
  name                = "AKSFileShareCapacityAlert"
  resource_group_name = azurerm_resource_group.instance_rg.name
  short_name          = "fscapacity"

  email_receiver {
    name                    = "sendtodemoslack"
    email_address           = "chaminda-test-aaaapgfgbxylwwdrq@mycompany.slack.com"
    use_common_alert_schema = true
  }
}

Once we create the action group we whould recieve an email in the slack channel saying the consumer group is set.



Next step is to create two alerts for each storage file share to send an alert when reached a given capacity usage. Note that for testing purpose here the values are set as 4GiB and 2GiB which is just an example values to trigger alerts.

resource "azurerm_monitor_metric_alert" "aks_fs_win" {
  name                 = "${azurerm_storage_account.filestorage_aks_win.name}-${azurerm_storage_share.aks_windows.name}-capacity-alert"
  resource_group_name  = azurerm_resource_group.instance_rg.name
  scopes               = ["${azurerm_storage_account.filestorage_aks_win.id}/fileservices/default"]
  description          = "AKS file share ${azurerm_storage_account.filestorage_aks_win.name}/${azurerm_storage_share.aks_windows.name} reached 80% capacity"
  enabled              = true
  auto_mitigate        = true
  frequency            = "PT1M"
  window_size          = "PT1H"
  severity             = 2
  target_resource_type = "Microsoft.Storage/storageAccounts/fileservices"

  criteria {
    metric_namespace = "Microsoft.Storage/storageAccounts/fileservices"
    metric_name      = "FileCapacity"
    aggregation      = "Average"
    operator         = "GreaterThanOrEqual"
    threshold        = 4294967296 # 4GiB
  }

  action {
    action_group_id = azurerm_monitor_action_group.fileshare_capacity_action.id
  }
}

resource "azurerm_monitor_metric_alert" "aks_fs_linux" {
  name                 = "${azurerm_storage_account.filestorage_aks_linux.name}-${azurerm_storage_share.aks_linux.name}-capacity-alert"
  resource_group_name  = azurerm_resource_group.instance_rg.name
  scopes               = ["${azurerm_storage_account.filestorage_aks_linux.id}/fileservices/default"]
  description          = "AKS file share ${azurerm_storage_account.filestorage_aks_linux.name}/${azurerm_storage_share.aks_linux.name} reached 80% capacity"
  enabled              = true
  auto_mitigate        = true
  frequency            = "PT1M"
  window_size          = "PT1H"
  severity             = 2
  target_resource_type = "Microsoft.Storage/storageAccounts/fileservices"

  criteria {
    metric_namespace = "Microsoft.Storage/storageAccounts/fileservices"
    metric_name      = "FileCapacity"
    aggregation      = "Average"
    operator         = "GreaterThanOrEqual"
    threshold        = 2147483648 # 2GiB
  }

  action {
    action_group_id = azurerm_monitor_action_group.fileshare_capacity_action.id
  }
}


You can find the full terraform code exaple here in GitHub. After we deploy the terraform changes we should have below resources created in Azure.


Now we can ispect our file shares for the capacity usage.


We get an alert in Azure and in slack channel.









No comments:

Popular Posts