1. Answers
  2. Setting Up GCP Monitoring Alert Policy with Terraform

How do I configure a GCP monitoring alert policy?

Setting Up GCP Monitoring Alert Policy with Pulumi

In this example, we will configure a monitoring alert policy on Google Cloud Platform. An alert policy allows you to define conditions under which you want to be notified about the state of your cloud infrastructure.

We will create:

  • A MetricThreshold condition based on CPU utilization.
  • A notification channel to send alerts.

The resources we will use:

  • google_monitoring_alert_policy: This resource creates the alert policy that includes the conditions and the notification channels.
  • google_monitoring_notification_channel: This resource sets up where the notifications will be sent, such as email.

Let’s dive into the code:

import * as pulumi from "@pulumi/pulumi";
import * as gcp from "@pulumi/gcp";

const emailChannel = new gcp.monitoring.NotificationChannel("email_channel", {
    displayName: "Email Notifications",
    type: "email",
    labels: {
        email_address: "your-notification-email@example.com",
    },
});
const cpuAlertPolicy = new gcp.monitoring.AlertPolicy("cpu_alert_policy", {
    displayName: "High CPU Alert Policy",
    conditions: [{
        displayName: "CPU Usage",
        conditionThreshold: {
            filter: "metric.type="compute.googleapis.com/instance/cpu/utilization"",
            duration: "60s",
            comparison: "COMPARISON_GT",
            thresholdValue: 0.8,
            aggregations: [{
                alignmentPeriod: "60s",
                perSeriesAligner: "ALIGN_MEAN",
            }],
        },
    }],
    notificationChannels: [emailChannel.id],
    combiner: "OR",
    documentation: {
        content: "This alert is triggered when the CPU usage exceeds 80%.",
        mimeType: "text/markdown",
    },
    userLabels: {
        environment: "production",
    },
});
export const alertPolicyId = cpuAlertPolicy.id;
export const notificationChannelId = emailChannel.id;

Key Points

  • The google_monitoring_notification_channel: Configures where alerts are sent (e.g., email, SMS).
  • The google_monitoring_alert_policy: Sets up the actual policy defining the alert conditions.
  • The filter in condition_threshold: Specifies the metric to monitor.
  • The notification_channels: Links the alert policy to the notification channel.
  • Outputs: Capture important IDs, useful for reference in other configurations.

Summary

We successfully created a GCP monitoring alert policy that checks CPU utilization and sends notifications via email when the CPU usage exceeds a specified threshold. By understanding these basic components, you can adapt and expand the configuration to match your operational needs.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up