Uptime/

Monitoring a Web App

This guide walks through setting up complete monitoring for a web application with a frontend and API backend, including uptime checks, security scans, and alerting.

Step 1: Configure the Provider

Start by configuring the Uptime provider with your API credentials.

main.tfhcl
terraform {
  required_providers {
    uptime = {
      source = "registry.terraform.io/skunkworq/uptime"
    }
  }
}

provider "uptime" {
  host      = "https://uptime-prod-api-5bbjdhyfxa-ts.a.run.app"
  api_token = var.uptime_api_token
  org_id    = var.uptime_org_id
}

variable "uptime_api_token" {
  type      = string
  sensitive = true
}

variable "uptime_org_id" {
  type = string
}

Step 2: Define Your Services

Create services for each component you want to monitor. Setting is_public = true makes them visible on your public status page.

main.tfhcl
resource "uptime_service" "api" {
  name        = "API Server"
  url         = "https://api.example.com"
  description = "Main API gateway"
  is_public   = true
}

resource "uptime_service" "frontend" {
  name        = "Frontend"
  url         = "https://app.example.com"
  description = "Next.js frontend"
  is_public   = true
}

Step 3: Add Monitoring Checks

Attach checks to your services. Use uptime checks for health monitoring and security checks for SSL and header validation.

main.tfhcl
resource "uptime_check" "api_uptime" {
  service_id       = uptime_service.api.id
  name             = "HTTP Health Check"
  type             = "uptime"
  interval_seconds = 60
  timeout_seconds  = 30

  uptime_config {
    method                = "GET"
    expected_status_codes = [200]
    verify_ssl            = true
    follow_redirects      = true
  }
}

resource "uptime_check" "api_security" {
  service_id       = uptime_service.api.id
  name             = "Security Scan"
  type             = "security"
  interval_seconds = 3600
  timeout_seconds  = 60

  security_config {
    check_ssl               = true
    check_headers           = true
    check_tls_version       = true
    ssl_expiry_warning_days = 30
    required_headers        = [
      "Strict-Transport-Security",
      "X-Content-Type-Options",
    ]
  }
}

resource "uptime_check" "frontend_uptime" {
  service_id       = uptime_service.frontend.id
  name             = "Frontend Health"
  type             = "uptime"
  interval_seconds = 60
  timeout_seconds  = 30

  uptime_config {
    method                = "GET"
    expected_status_codes = [200]
    verify_ssl            = true
  }
}

Check Intervals

Use shorter intervals (60s) for uptime checks and longer intervals (3600s) for security scans. Security checks are less time-sensitive and more resource-intensive.

Step 4: Configure Alerts

Set up alerts to notify your team when issues are detected. You can combine multiple condition types and notification channels.

main.tfhcl
resource "uptime_alert" "api_down" {
  check_id       = uptime_check.api_uptime.id
  name           = "API Down"
  condition_type = "consecutive_failures"

  consecutive_failures_config {
    count = 3
  }

  channel {
    type   = "email"
    target = "ops@example.com"
  }

  channel {
    type   = "slack"
    target = "https://hooks.slack.com/services/T00/B00/xxx"
  }
}

resource "uptime_alert" "api_slow" {
  check_id       = uptime_check.api_uptime.id
  name           = "API Response Time High"
  condition_type = "threshold"

  threshold_config {
    metric   = "response_time_ms"
    operator = "gt"
    value    = 2000
  }

  channel {
    type   = "email"
    target = "ops@example.com"
  }
}

resource "uptime_alert" "ssl_expiry" {
  check_id       = uptime_check.api_security.id
  name           = "SSL Certificate Expiring"
  condition_type = "ssl_expiry"

  ssl_expiry_config {
    days_before = 14
  }

  channel {
    type   = "email"
    target = "security@example.com"
  }
}

Step 5: Apply

bash
# Initialize the provider
terraform init

# Preview what will be created
terraform plan

# Apply the configuration
terraform apply

What's Next?