skip to Main Content

I am trying to research if it is possible to create alerts for azure functions via terraform.

My goal is to create a general rule that can be setup for on going functions that would alert us if an automated/timed function fails for x amount of times

2

Answers


  1. Yes it’s possible. See the documentation here.

    Sample taken from the docs is creating a metric alert:

    resource "azurerm_resource_group" "example" {
      name     = "example-resources"
      location = "West Europe"
    }
    
    resource "azurerm_storage_account" "to_monitor" {
      name                     = "examplestorageaccount"
      resource_group_name      = azurerm_resource_group.example.name
      location                 = azurerm_resource_group.example.location
      account_tier             = "Standard"
      account_replication_type = "LRS"
    }
    
    resource "azurerm_monitor_action_group" "main" {
      name                = "example-actiongroup"
      resource_group_name = azurerm_resource_group.example.name
      short_name          = "exampleact"
    
      webhook_receiver {
        name        = "callmyapi"
        service_uri = "http://example.com/alert"
      }
    }
    
    resource "azurerm_monitor_metric_alert" "example" {
      name                = "example-metricalert"
      resource_group_name = azurerm_resource_group.example.name
      scopes              = [azurerm_storage_account.to_monitor.id]
      description         = "Action will be triggered when Transactions count is greater than 50."
    
      criteria {
        metric_namespace = "Microsoft.Storage/storageAccounts"
        metric_name      = "Transactions"
        aggregation      = "Total"
        operator         = "GreaterThan"
        threshold        = 50
    
        dimension {
          name     = "ApiName"
          operator = "Include"
          values   = ["*"]
        }
      }
    
      action {
        action_group_id = azurerm_monitor_action_group.main.id
      }
    }
    
    Login or Signup to reply.
  2. I tried to reproduce the same in my environment to create the Alerts in Function app using Terraform:

    Terraform code.

    provider "azurerm" {
      features {}
    }
    resource "azurerm_resource_group" "thejesh-rg" {
      name     = "Thejesh-RG-resources"
      location = "West Europe"
    }
    resource "azurerm_storage_account" "thejeshstorage" {
      name                     = "thejeshstorageaccount"
      resource_group_name      = azurerm_resource_group.thejesh-rg.name
      location                 = azurerm_resource_group.thejesh-rg.location
      account_tier             = "Standard"
      account_replication_type = "LRS"
    }
    
    resource "azurerm_service_plan" "thejeshsp" {
      name                = "thejeshsp-app-service-plan"
      resource_group_name = azurerm_resource_group.thejesh-rg.name
      location            = azurerm_resource_group.thejesh-rg.location
      os_type             = "Linux"
      sku_name            = "P1v2"
    }
    
    resource "azurerm_linux_function_app" "thejesh" {
      name                = "thejesh-linux-function-app"
      resource_group_name = azurerm_resource_group.thejesh-rg.name
      location            = azurerm_resource_group.thejesh-rg.location
    
      storage_account_name       = azurerm_storage_account.thejeshstorage.name
      storage_account_access_key = azurerm_storage_account.thejeshstorage.primary_access_key
      service_plan_id            = azurerm_service_plan.thejeshsp.id
    
      site_config {}
    }
    
    
    resource "azurerm_monitor_action_group" "actiongroup" {
      name                = "thejesh-actiongroup"
      resource_group_name = azurerm_resource_group.thejesh-rg.name
      short_name          = "exampleact"
      email_receiver{
      email_address = "Email-ID"
      name          = "sendtoadmin"
      }
    }
    resource "azurerm_monitor_metric_alert" "metrics" {
      name                = "theja-metricalert"
      resource_group_name = azurerm_resource_group.thejesh-rg.name
      scopes              = [azurerm_linux_function_app.thejesh.id]
      description         = "Action will be triggered when Transactions count is greater than 1."
      
      criteria {
        metric_namespace = "Microsoft.Web/sites"
        metric_name      = "Requests"
        aggregation      = "Total"
        operator         = "GreaterThan"
        threshold         = "1"
        }
        action {
        action_group_id = azurerm_monitor_action_group.actiongroup.id
      }
      }
    

    Terraform Plan:

    enter image description here

    Terraform Apply

    enter image description here

    Once ran the code resources are created.

    enter image description here

    Alert notification.

    enter image description here

    Successfully received email.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search