Photo by Lerone Pieters on Unsplash
Photo by Lerone Pieters on Unsplash

SaC - How to build status pages as code with Terraform

Marko Simon
Marko Simon

Status pages are a clever solution to bundle all your services, and see the status of them at one sight. We at iLert took this one step further: why not build your status page as code using Terraform? We want to show you how we make it possible, and how you can set it up for your own infrastructure - a real SaC solution.

Hashicorp’s Terraform is widely used for deploying and manging infrastructure as code. In our previous blogpost to Terraform we covered all the basics for setting up the iLert Terraform Provider and an alerting infrastructure. If you need more in depth information on the setup or if you are stuck you can find more information there. We will now step through the whole process: from creating your first alert source to deploying a status page with automation rules via code with Terraform.

Content

  1. Requirements
  2. Setup
  3. iLert infrastructure
  4. Create a status page
  5. Automate a status page
  6. Run the automation
  7. Observability can be easy

Requirements

You will need:

Setup

A quick overview of what you need to set up:

  • iLert - account - click
  • Terraform - local installation - click

Additionally, you will need to clone the example repository from GitHub.

git clone git@github.com:iLert/terraform-status-page-sample.git

You also need to provide your iLert credentials either as variables in terraform apply, or store it in a separate file named terraform.tfvars. Create a file in the repository root directory named terraform.tfvars, and Terraform will automatically detect and set those variables on every apply.

organization = "your organization e.g. tenant"
api_token    = "your api token"
// username     = "your username"
// password     = "your password"

To finish your setup you have to initialize Terraform to use it in your directory. Simply run terraform init and the initialization should be done after provider installation.

iLert infrastructure

Before applying any code here’s an overview of the resources and their relations you will be creating in iLert.

iLert resource connections

In short: we send an event to an alert source which triggers an alert. An automation rule connects the alert source and a service by changing the services status when the alert source fires an alert. Therefore when the status of the service changes the statuspage will monitor this and display the current status of the service.

Create a status page

Setting-up

First you have to configure an alert source and provide an escalation policy.

data "ilert_escalation_policy" "default" {
  name = "Default"
}

resource "ilert_alert_source" "example" {
  name              = "Example alert source from terraform"
  integration_type  = "API"
  escalation_policy = ilert_escalation_policy.example.id
}

Creating

To create a status page you also need a service which will be contained in the status page.

resource "ilert_service" "example" {
  name = "blogpost_example"
}

resource "ilert_status_page" "example" {
  name       = "My Statuspage"
  subdomain  = "example_statuspage.ilert.io"
  visibility = "PRIVATE"

  service {
    id = ilert_service.example.id
  }
}

Note: A subdomain must be unique, you may have to change it to another fitting name.

Automate a status page

Firing an alert or setting the status of the service manually is not desirable in 24/7 monitoring. An automation rule fits in perfect for that task: it connects the alert source to our service and helps us to automatically change the status of our service on an incoming alert.

resource "ilert_automation_rule" "example" {
  alert_type     = "CREATED"
  service_status = "DEGRADED"
  service {
    id = ilert_service.example.id
  }
  alert_source {
    id = ilert_alert_source.example.id
  }
}

If you now run terraform apply you should see cli output like this:

terraform apply output

You have successfully configured an alert source and an escalation policy and also built a statuspage containing a service while automating via automation rule.

status page operational

Fire up your browser and visit example_statuspage.ilert.io to check out the page you have just created :)

Run the automation

To put everything to test you have to trigger an alert, means sending an event to the iLert events endpoint. For that you have to put in your alert source integration key into apiKey in event.json.

event.json
{
    "apiKey": "your alert source integration key",
    "eventType": "ALERT",
    "summary": "Test alert"
}

This key can be found in the terraform state file terraform.tfstate or in the iLert UI under the alert source.

integration key

Now you send the event to trigger an alert:

curl -d "@event.json" -H 'Content-Type: application/json' -X POST https://api.ilert.com/api/events

An alert was created, the automation rule detected this and set the status of our service to ‘Degraded’, hence the status page shows the outage.

status page degraded

Destroying all created infrastructure

By using terraform destroy all of the created resources will be destroyed and removed from your iLert account.

Note: If you haven’t resolved the alert triggered from the alert source you won’t be able to delete it. Please resolve the alert first.

Observability can be easy

Building your status page as code with the iLert Terraform Provider simplifies the process of building consistent observability for your own services a lot. An iLert status page gives you the important information at first sight while providing details when needed.

If you are interested in knowing more about iLert status pages - take a look at our docs.

A fully executable sample of the shown code can be found on Github in our corresponding example repository.