Monitoring as Code
Manage checks with Terraform
The OnlineOrNot Terraform provider manages your monitoring as code. Keep checks next to the services they watch, review changes in pull requests, and apply them from CI.
What you can manage
| Resource | What it manages |
|---|---|
onlineornot_uptime_check |
HTTP and HTTPS uptime checks, with assertions |
onlineornot_browser_check |
Browser checks, from a URL or a Playwright Test script |
onlineornot_dns_check |
DNS monitors |
onlineornot_tcp_check |
TCP monitors |
onlineornot_heartbeat |
Heartbeats for cron jobs and background workers |
onlineornot_status_page, onlineornot_status_page_component, and related resources |
Status pages, components, groups and their order |
onlineornot_status_page_incident, onlineornot_status_page_scheduled_maintenance |
Status page incidents and scheduled maintenance |
onlineornot_maintenance_window |
Maintenance windows that pause alerts |
onlineornot_webhook |
Webhooks for alerts |
onlineornot_environment_variable |
Environment variables referenced by uptime check headers |
onlineornot_token |
API tokens |
Data sources look up users, checks, heartbeats, status pages, webhooks and maintenance windows. The registry documentation lists every attribute.
The provider is generated from the same OpenAPI description as the OnlineOrNot API, so it follows the API as it changes.
Set up the provider
- In OnlineOrNot, go to API Tokens and create a token. Give it
EDITaccess to the resources Terraform will manage. The examples below look up alert recipients with theonlineornot_userdata source, which also needsREADaccess to People. - Set the token in the
ONLINEORNOT_API_KEYenvironment variable, in your shell or your CI secret store. Don't put the token in HCL. - Add the provider to your configuration:
terraform {
required_providers {
onlineornot = {
source = "onlineornot/onlineornot"
version = "~> 0.1.54"
}
}
}
provider "onlineornot" {}
- Run
terraform init.
Create an uptime check
data "onlineornot_user" "oncall" {
email = "oncall@example.com"
}
resource "onlineornot_uptime_check" "api" {
name = "API health check"
url = "https://api.example.com/health"
method = "GET"
test_interval = 60
test_regions = ["aws:us-east-1", "aws:eu-central-1"]
user_alerts = [data.onlineornot_user.oncall.id]
assertions = [
{
type = "TEXT_BODY"
property = ""
comparison = "CONTAINS"
expected = "ok"
}
]
}
Run terraform plan, review the changes, then run terraform apply.
Use for_each to monitor several endpoints with the same settings:
locals {
endpoints = {
api = "https://api.example.com/health"
web = "https://example.com"
}
}
resource "onlineornot_uptime_check" "endpoints" {
for_each = local.endpoints
name = "Production ${each.key}"
url = each.value
user_alerts = [data.onlineornot_user.oncall.id]
}
Deploy a Playwright check from a test file
Write the browser check as a normal Playwright Test file, run it on your computer, and let Terraform upload it.
Each browser check accepts one self-contained JavaScript file. The file must use Playwright Test. Terraform uploads the file contents only. It does not upload local imports, configuration files, or dependencies. Playwright projects are not supported yet.
Save this file as homepage.spec.js, next to your Terraform configuration:
import { test, expect } from '@playwright/test';
test('homepage loads', async ({ page }) => {
await page.goto('https://example.com');
await expect(
page.getByRole('heading', { name: 'Example Domain' }),
).toBeVisible();
});
Run it locally with the same Playwright version as the browser runtime:
npm init -y
npm install --save-dev @playwright/test@1.58.2
npx playwright install chromium
npx playwright test homepage.spec.js --browser=chromium --workers=1
Then read the file with file():
resource "onlineornot_browser_check" "homepage" {
name = "Homepage Playwright check"
script = file("${path.module}/homepage.spec.js")
test_interval = 300
test_regions = ["aws:us-east-1"]
}
Playwright owns the test steps, assertions and test timeouts. Terraform owns the monitor name, interval, regions and alert settings. Don't set url or timeout on a scripted check. A script can't raise the 120 second execution limit.
When you edit the file, terraform plan shows a change to the same resource and terraform apply updates the same monitor, so its history stays. If someone changes the script in the dashboard, the next plan shows the difference and apply restores your file.
A successful apply creates or updates the monitor. It doesn't prove the test passed: open the monitor in OnlineOrNot and check its next result.
Deploy every test file in a folder
resource "onlineornot_browser_check" "files" {
for_each = fileset("${path.module}/checks", "*.spec.js")
name = trimsuffix(each.key, ".spec.js")
script = file("${path.module}/checks/${each.key}")
test_interval = 300
test_regions = ["aws:us-east-1"]
}
Each matched file creates one monitor. Separate test() calls in a file don't create separate monitors. Removing a file plans the deletion of its monitor, and renaming a file changes its resource address, so the plan can delete the old monitor and create a new one. Use explicit resources or stable map keys when a monitor must survive a rename.
Keep secrets out of scripts
Terraform stores script contents in state and can show them in plans. Marking a value sensitive doesn't remove it from state. Don't put credentials in test files or interpolate secrets into them.
The provider's Playwright guide covers the full workflow, including a complete folder example.
Create a heartbeat
resource "onlineornot_heartbeat" "nightly_backup" {
name = "Nightly backup"
report_period_cron = "0 3 * * *"
timezone = "UTC"
grace_period = 900
user_alerts = [data.onlineornot_user.oncall.id]
}
See how heartbeats work for how to send pings.
Send alerts to integrations
Check and heartbeat resources take lists of integration IDs: slack_alerts, discord_alerts, microsoft_teams_alerts, telegram_alerts, pushover_alerts, incident_io_alerts, webhook_alerts, and oncall_alerts for PagerDuty, Opsgenie, Grafana OnCall and Spike.sh.
Terraform doesn't connect the integrations themselves. Connect them once in Settings > Integrations, then reference them from your checks. To find an integration's ID, select it on a check in the dashboard, then read that check with the CLI (onlineornot checks get <check-id>) or the API and copy the ID from its alert lists, such as oncall_alerts.
Webhooks are the exception: create them with the onlineornot_webhook resource, and attach checks with its check_ids or reference its id in webhook_alerts.