Integrating Cloud Monitoring Alarms into Massdriver
An instance alarm tracks a cloud monitoring alarm for an instance. An instance can have many alarms. Massdriver shows each alarm's state on the canvas and sends alarm events to event destinations.
Massdriver receives state changes from:
- AWS CloudWatch alarms
- Google Cloud Monitoring alert policies
- Azure Monitor alerts
- Prometheus Alertmanager
Alarm states​
| State | Meaning |
|---|---|
OK | The metric is within the threshold. |
ALARM | The metric crossed the threshold and the alarm is firing. |
An alarm with no reported state yet has a currentState of null.
Webhook URL​
Each environment has one alarm webhook URL. Deployments receive it as md_metadata.observability.alarm_webhook_url:
https://api.massdriver.cloud/alarms/<environment-uuid>/<alarm-token>
Point the cloud provider's alarm notifications at this URL. Massdriver matches each notification to a registered alarm by its cloud_resource_id. When no registered alarm matches, Massdriver registers one from the instance tag in the notification.
Alarms that a bundle registers with massdriver_instance_alarm are removed when OpenTofu or Terraform destroys that resource, for example when the instance is decommissioned.
Cloud Provider Integration Examples​
In each example, the cloud provider sends notifications to the webhook URL and a massdriver_instance_alarm resource registers the alarm with the instance.
AWS (CloudWatch Alarms) + Massdriver​
Official Modules:
# AWS SNS Topic for CloudWatch Alarm notifications
resource "aws_sns_topic" "main" {
# Name for the SNS topic (required)
name = "${var.md_metadata.name_prefix}-alarms"
}
# AWS SNS Topic Subscription for Massdriver Webhook
resource "aws_sns_topic_subscription" "main" {
# string: Webhook endpoint URL
endpoint = var.md_metadata.observability.alarm_webhook_url
# string: Protocol for the subscription
protocol = "https"
# string: SNS topic ARN to subscribe to
topic_arn = aws_sns_topic.main.arn
}
# AWS CloudWatch Metric Alarm
resource "aws_cloudwatch_metric_alarm" "alarm" {
count = var.alarm.alarm_enabled ? 1 : 0
# string: Name of the CloudWatch alarm (using Massdriver name prefix)
alarm_name = "${var.md_metadata.name_prefix}-test-alarm"
# string: Description of the alarm
alarm_description = "High CPU utilization alarm for Massdriver"
# Metric to monitor (name and namespace)
metric_name = var.alarm.metric_name
namespace = var.alarm.metric_namespace
# Send both ALARM and OK notifications to SNS
alarm_actions = [aws_sns_topic.main.arn]
ok_actions = [aws_sns_topic.main.arn]
treat_missing_data = var.alarm.treat_missing_data
datapoints_to_alarm = 1
# Map Massdriver comparison operator to AWS format and evaluate over one period
comparison_operator = local.massdriver_to_aws_comparison_operator[var.alarm.comparison_operator]
evaluation_periods = 1
period = var.alarm.period # in seconds
statistic = var.alarm.metric_statistic
threshold = var.alarm.threshold
# Include any metric dimensions (convert list of dimensions to map)
dimensions = try({ for dim in var.alarm.dimensions : dim.name => dim.value }, {})
}
# Massdriver Instance Alarm resource (linking the CloudWatch alarm to Massdriver)
resource "massdriver_instance_alarm" "aws_instance_alarm" {
count = var.alarm.alarm_enabled ? 1 : 0
display_name = var.alarm.massdriver_display_name
cloud_resource_id = aws_cloudwatch_metric_alarm.alarm[0].arn
comparison_operator = var.alarm.comparison_operator
threshold = var.alarm.threshold
period = var.alarm.period # seconds
metric {
namespace = var.alarm.metric_namespace
name = var.alarm.metric_name
statistic = var.alarm.metric_statistic
dimensions = aws_cloudwatch_metric_alarm.alarm[0].dimensions
}
}
GCP (Cloud Monitoring) + Massdriver​
Official Modules:
# GCP Webhook Notification Channel
resource "google_monitoring_notification_channel" "webhook" {
# string: Display name for the channel
display_name = "Massdriver Webhook"
# string: Channel type (webhook)
type = "webhook"
# map(string): Required labels including the webhook URL
labels = {
url = var.md_metadata.observability.alarm_webhook_url
}
}
# GCP Alert Policy (e.g., high CPU utilization)
resource "google_monitoring_alert_policy" "cpu_alert" {
count = var.alarm.alarm_enabled ? 1 : 0
# string: Display name for the alert policy
display_name = "${var.md_metadata.name_prefix} High CPU Alert"
combiner = "OR"
conditions {
display_name = "High CPU"
condition_threshold {
filter = "metric.type=\"compute.googleapis.com/instance/cpu/utilization\" AND resource.type=\"gce_instance\""
comparison = "COMPARISON_GT"
threshold_value = var.alarm.threshold
duration = "${var.alarm.period}s"
trigger {
count = 1
}
}
}
notification_channels = [google_monitoring_notification_channel.webhook.name]
}
# Massdriver Instance Alarm resource for GCP
resource "massdriver_instance_alarm" "gcp_instance_alarm" {
count = var.alarm.alarm_enabled ? 1 : 0
display_name = var.alarm.massdriver_display_name
cloud_resource_id = google_monitoring_alert_policy.cpu_alert[0].name
comparison_operator = var.alarm.comparison_operator
threshold = var.alarm.threshold
period = var.alarm.period # seconds
metric {
namespace = "compute.googleapis.com/instance"
name = "cpu/utilization"
statistic = "average"
dimensions = {}
}
}
Azure (Monitor Alerts) + Massdriver​
Official Modules:
# Azure Action Group with webhook receiver
resource "azurerm_monitor_action_group" "webhook" {
name = "massdriver-actiongroup"
resource_group_name = var.resource_group
short_name = "mdwebhook"
webhook_receiver {
name = "massdriver"
service_uri = var.md_metadata.observability.alarm_webhook_url
use_common_alert_schema = true
}
}
# Azure Metric Alert (e.g., high CPU on a VM)
resource "azurerm_monitor_metric_alert" "cpu_alert" {
count = var.alarm.alarm_enabled ? 1 : 0
name = "${var.md_metadata.name_prefix}-cpu-alert"
resource_group_name = var.resource_group
scopes = [var.target_resource_id] # target resource to monitor
description = "High CPU on Azure VM"
severity = 2
frequency = "PT1M" # evaluation frequency (1 minute)
window_size = "PT5M" # evaluation window (5 minutes)
criteria {
metric_namespace = "Microsoft.Compute/virtualMachines"
metric_name = "Percentage CPU"
aggregation = "Average"
operator = "GreaterThan"
threshold = var.alarm.threshold
}
action {
action_group_id = azurerm_monitor_action_group.webhook.id
webhook_properties = {}
}
}
# Massdriver Instance Alarm resource for Azure
resource "massdriver_instance_alarm" "azure_instance_alarm" {
count = var.alarm.alarm_enabled ? 1 : 0
display_name = var.alarm.massdriver_display_name
cloud_resource_id = azurerm_monitor_metric_alert.cpu_alert[0].id
comparison_operator = var.alarm.comparison_operator
threshold = var.alarm.threshold
period = 300 # matches the 5-minute window_size
metric {
namespace = "Microsoft.Compute/virtualMachines"
name = "Percentage CPU"
statistic = "Average"
dimensions = {}
}
}
Alertmanager (Kubernetes) + Massdriver​
Official Modules:
# Alertmanager ConfigMap with webhook receiver configuration
resource "kubernetes_config_map" "alertmanager" {
metadata {
name = "alertmanager-config"
namespace = "monitoring"
}
data = {
"alertmanager.yaml" = <<-EOT
global: {}
route:
receiver: "massdriver-webhook"
receivers:
- name: "massdriver-webhook"
webhook_configs:
- url: "${var.md_metadata.observability.alarm_webhook_url}"
EOT
}
}
# Massdriver Instance Alarm resource for Alertmanager
resource "massdriver_instance_alarm" "alertmanager_instance_alarm" {
count = var.alarm.alarm_enabled ? 1 : 0
display_name = var.alarm.massdriver_display_name
# <md_package label value>_<alertname>; see Instance Tags in Alarm Notifications
cloud_resource_id = "${var.md_metadata.name_prefix}_${var.alarm.prometheus_alert_name}"
}
Instance Tags in Alarm Notifications​
Massdriver matches a notification to a registered alarm by its cloud_resource_id. When no registered alarm matches, Massdriver finds the instance from an instance tag in the notification and registers the alarm for that instance. The instance must be in the environment of the webhook URL.
The instance tag is md-instance, which holds the instance identifier. Notifications that do not have md-instance are matched by the legacy md-package tag, which holds the instance's name prefix.
| Service | Where Massdriver reads the instance tag |
|---|---|
| AWS CloudWatch | md-instance or name_prefix key in the JSON alarm description, next to a message key |
| Google Cloud Monitoring | md-instance or md-package in the incident's resource user labels |
| Azure Monitor | md-instance or md-package in the action's webhook properties |
| Prometheus Alertmanager | md_instance or md_package in the alert's common labels |
Alertmanager uses underscores because Prometheus label names cannot contain hyphens. Massdriver sets the cloud_resource_id of an Alertmanager alarm to the label value and the alert name, joined by an underscore: <label value>_<alertname>. To register an Alertmanager alarm with massdriver_instance_alarm, use the same format. If an alert has both labels, Massdriver uses md_instance.
Switching to Custom Default Tags​
Organizations that use the legacy default tags send md-package on every resource. When an organization switches to a custom list of default tags, Massdriver stops sending md-package and sends md-instance instead. Each resource gets the new tags at the next deployment of its instance. Before you switch, check each alarm service:
- AWS CloudWatch: registered alarms match by alarm ARN, so no change is necessary. For alarms that are not registered, set
md-instanceto the instance identifier in the JSON alarm description. - Google Cloud Monitoring: alert policies that filter on
metadata.user_labels.md-packagestop matching after the next deployment, because the resources no longer have that label. Change the filter tometadata.user_labels.md-instancewith the instance identifier as the value. - Azure Monitor: actions that set their webhook properties from
md_metadata.default_tagssendmd-instanceafter the switch, so no change is necessary. Actions that setmd-packagedirectly continue to work. - Prometheus Alertmanager: alert rules that set
md_packagecontinue to work. If you addmd_instanceto a rule, change thecloud_resource_idof its registered alarm to<instance identifier>_<alertname>.
Registering alarms through the API​
Use the createInstanceAlarm mutation to register an alarm outside a bundle, for example from a script. It requires environment:update.
curl https://api.massdriver.cloud/api/v2 \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${MASSDRIVER_API_KEY}" \
-d @- <<'JSON'
{
"query": "mutation($input: CreateInstanceAlarmInput!) { createInstanceAlarm(organizationId: \"my-org\", instanceId: \"ecomm-prod-db\", input: $input) { successful result { id } messages { field message } } }",
"variables": {
"input": {
"displayName": "High CPU Utilization",
"cloudResourceId": "arn:aws:cloudwatch:us-west-2:123456789012:alarm:test-alarm",
"comparisonOperator": "GREATER_THAN",
"threshold": 80.0,
"period": 300,
"metric": {
"name": "CPUUtilization",
"namespace": "AWS/EC2",
"statistic": "Average",
"dimensions": [{ "name": "InstanceId", "value": "i-1234567890abcdef0" }]
}
}
}
}
JSON
| Field | Required | Description |
|---|---|---|
displayName | Yes | Name shown in the UI and in notifications. |
cloudResourceId | Yes | The provider's ID for the alarm: a CloudWatch alarm ARN, a GCP alert policy name (projects/<project-id>/alertPolicies/<policy-id>), or an Azure alert ID. Must be unique within the instance. |
comparisonOperator | No | GREATER_THAN, LESS_THAN, GREATER_THAN_OR_EQUAL_TO, or LESS_THAN_OR_EQUAL_TO. |
threshold | No | The value that triggers the alarm. |
period | No | Evaluation window in seconds. |
metric | No | name, namespace, statistic, region, and dimensions (a list of name/value pairs). |
Practices​
- Set thresholds from the application's normal operating range.
- Evaluate over several data points to avoid alarms on brief spikes.
- Write alarm descriptions that say what condition is monitored.
- Route critical alarms to an on-call path, and group or prioritize alarms to limit noise.
- Test webhook delivery after you change an alarm, and check that alarms move between
OKandALARMon the canvas.