Azure Sample - Combine VM Availability Alert and Resource Graph based VM Health Status with Logic App Action
Use LogicApp as an Alert action and decide whether to send a notification (e.g. e-mail) or not based on additional information (Azure Resource Health information in this case) when VM Availability metrics threshold is reached.
This repository includes a GitHub Action workflow that automatically deploys the Logic App infrastructure using Terraform to your Azure subscription in tenant 539d8bb1-bbd5-4f9d-836d-223c3e6d1e43.
-
Azure App Registration with Federated Identity: Create an App Registration with federated identity credentials for GitHub Actions:
# Create app registration az ad app create --display-name "logic-app-deployment-oidc" --sign-in-audience AzureADMyOrg # Create service principal and assign roles APP_ID=$(az ad app list --display-name "logic-app-deployment-oidc" --query "[0].appId" -o tsv) az ad sp create --id $APP_ID # Configure federated identity for your repository az ad app federated-credential create --id $APP_ID --parameters '{ "name": "github-actions-main", "issuer": "https://token.actions.githubusercontent.com", "subject": "repo:YOUR_GITHUB_USERNAME/azuresamples-vm-availability-logicapp:ref:refs/heads/main", "audiences": ["api://AzureADTokenExchange"] }'
-
GitHub Secrets: Configure the following secrets in your GitHub repository:
AZURE_CLIENT_ID: App Registration application IDAZURE_SUBSCRIPTION_ID: Your Azure subscription IDAZURE_TENANT_ID: Azure tenant ID (539d8bb1-bbd5-4f9d-836d-223c3e6d1e43)AZURE_TARGET_SUBSCRIPTION_ID: Subscription ID to monitor for VM availability (can be the same as AZURE_SUBSCRIPTION_ID)NOTIFICATION_EMAIL: Email address for alert notificationsSTATE_STORAGE_ACCOUNT: Azure Storage account name for Terraform stateSTATE_CONTAINER: Azure Storage container name for Terraform stateSTATE_RESOURCE_GROUP: Azure Resource Group containing the storage accountSTORAGE-ACCESS-KEY: Storage account access key for Terraform state
Note: With federated identity, you no longer need
AZURE_CLIENT_SECRET, enhancing security.
- Push changes to the
mainbranch to trigger automatic deployment - The workflow will run
terraform planon pull requests andterraform applyon merge to main
Use the manual workflow dispatch in GitHub Actions:
- Go to Actions tab in your GitHub repository
- Select "Deploy Logic App Infrastructure"
- Click "Run workflow"
- Choose action:
plan,apply, ordestroy
The Terraform configuration deploys:
- Resource Group: Contains all Logic App resources
- User-Assigned Managed Identity: For secure access to Azure Resource Graph
- Role Assignment: Reader permissions for the managed identity on the target subscription
- App Service Plan: Dedicated hosting environment for Logic App Standard (S1 SKU)
- Storage Account: Required storage for Logic App Standard runtime and state
- Office 365 API Connection: For sending email notifications
- Logic App Standard: The main workflow application with enhanced features and performance
Copy terraform/terraform.tfvars.example to terraform/terraform.tfvars and customize the values:
subscription_id = "your-subscription-id"
target_subscription_id = "subscription-to-monitor"
notification_email = "your-email@domain.com"
resource_group_name = "rg-vm-availability-logicapp"
location = "Sweden Central"After successful deployment, the workflow outputs:
- Resource Group name
- Logic App name and ID
- Managed Identity Principal ID
- HTTP trigger callback URL (sensitive)
- Azure VM Availability (preview) Metric used to define Azure Alert
1.1) Alert Definition - Conditions
1.2) Azure Alerts showing VM Availability alert triggered
- Azure Resource Graph query used to retrieve Azure VM resource health information
- Logic App with the workflow to:
- Receive alert
- Query Azure Resource Graph API to retrieve Resource Health information
- Decide based on the status whether to send e-mail notification or ignore the alert
Note
Logic App Workflow code is available in the logicapp.json
- Logic App with the workflow - Condition
IF "VirtualMachineDeallocationInitiated" (means a user has requested VM to be stopped) then ignore the Alert







