(Updated: )

It's code! Synthetic monitoring with Terraform Cloud & Checkly

Share on social

Table of contents

With active monitoring being a vital part of digital infrastructure, Checkly's developer-first approach made it an obvious partner for HashiCorp Terraform.

- Asvin Ramesh, Director, Partner Alliances at HashiCorp

How does one manage monitoring in the age of digital infrastructure as code? Also as code, of course! Combining HashiCorp Terraform Cloud and Checkly enables you to configure synthetic and API monitoring as part of your existing infrastructure codebase. It is flexible, programmable and will keep you out of maintenance hell, even at scale: it is monitoring for developers.

Extending your existing Terraform Cloud configuration takes only two minutes. Let's take a look together. (Already a pro? Skip to the complete example.)

Setting up deep API monitoring

Knowing the status of our API at any point in time is critical. This is especially true for public endpoints, which are being used directly by our customers. Checkly API checks will alert us if an endpoint returns wrong or incomplete data, or if it takes too long to respond, giving us the chance to intervene before our customers are impacted.

Let's create our first API check to ensure the users of our online bookstore can retrieve a list of available books at any time. Before we do anything else, we need to add the Checkly Terraform provider, which we will use to define every aspect of the check, to our Terraform file. To keep this tutorial simple, we will do all our work in the main.tf file.

variable "checkly_api_key" {}

terraform {
  required_providers {
    checkly = {
      source = "checkly/checkly"
      version = "0.7.1"
    }
  }
}

provider "checkly" {
  api_key = var.checkly_api_key
}

We also add a resource for the API check. Let's keep things simple for now and specify a few key parameters, including the name, schedule, locations and assertions:

resource "checkly_check" "webstore-list-books" {
  name                      = "list-books"
  type                      = "API"
  activated                 = true
  should_fail               = false
  frequency                 = 1
  double_check              = true
  ssl_check                 = true
  use_global_alert_settings = true
  degraded_response_time    = 5000
  max_response_time         = 10000

  locations = [
    "eu-central-1",
    "us-west-1"
  ]

  request {
    url              = "https://danube-webshop.herokuapp.com/api/books"
    follow_redirects = true
    assertion {
      source     = "STATUS_CODE"
      comparison = "EQUALS"
      target     = "200"
    }
    assertion {
      source     = "JSON_BODY"
      property   = "$.length"
      comparison = "EQUALS"
      target     = "30"
    }
  }
}

Notice how, aside from asserting against a successful status code, we added the $.length assertion on the response's JSON body, to ensure the right amount of items are being sent back.

Terraform Cloud has a tight integration with GitHub. This allows us to apply any changes to a Terraform configuration that is stored in a GitHub repository to a linked Terraform Cloud workspace, as soon as they are merged into a master branch. We want every successful push to master on our Git repository to be automatically applied to our Terraform Cloud workspace. For this reason, under Settings > General, our plan is set to Auto apply.

We also need to register a free account on Checkly. Once that is done, we can fetch our Checkly API key from our Checkly Account Settings...

...and add it as an environment variable on Terraform Cloud, under the Variables section, with the key TF_VAR_checkly_api_key:

We can now commit our changes. As soon as we have them merged into the master branch, the current run will appear on our Terraform Cloud dashboard.

Once that is done, we will see our new API check appear on our dashboard on Checkly:

The check will now run every minute, monitoring the status of our endpoint from the locations we have selected. Should it fail, it will immediately alert us on our channel(s) of choice:

Adding synthetic transaction monitoring

Having API endpoints monitored is important, but it rarely is enough: to ensure webapp functionality from the end user's perspective, we need to monitor key user journeys on our frontend, too.

Checkly leverages Puppeteer and Playwright to automatically go through the most important flows of your webapp, just like a user would. As soon as one breaks, it will alert you, just like with API checks.

Let's look at an example: we want to keep an eye on the login flow of our online bookstore, so we write or record the following script using Playwright.

const { chromium } = require("playwright");

const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();

await page.goto("https://danube-webshop.herokuapp.com/");

await page.click("#login");
await page.type("#n-email", "user@email.com");
await page.type("#n-password2", "supersecure1");
await page.click("#goto-signin-btn");

await page.waitForSelector("#login-message", { visible: true });

await browser.close();

Let's save the file in scripts/login.js, and then reference it in our main.tf file:

resource "checkly_check" "login" {

  name                      = "Login Flow"
  type                      = "BROWSER"
  activated                 = true
  should_fail               = false
  frequency                 = 10
  double_check              = true
  ssl_check                 = false
  use_global_alert_settings = true
  locations = [
    "us-west-1",
    "eu-central-1"
  ]

  script = file("${path.module}/scripts/login.js")

}

Let's also commit and merge these changes to have them reflected on Checkly.

Our check is fully configured, and will run every 10 minutes as specified, informing us if anything goes wrong with our login flow.

We can keep going and add as many checks as we need to: Checkly and Terraform Cloud scale seamlessly together, with many users managing checks in the thousands, stress-free.

The benefits of synthetic monitoring as code

Combining Terraform Cloud and Checkly to manage your monitoring as code enables us to:

  1. Avoid having to manage a large number of checks manually, saving precious time.
  2. Check in our active monitoring setup to source control, along with our existing infrastructure codebase, making it trivial to update it and to roll back changes.
  3. Use parameterised resources to generate hundreds or thousands of checks with small, tidy .tf files.

When managing complex real-world infrastructure setups, these benefits pay big dividends over time, saving precious resources while ensuring your key services are delighting your users.

Try it out on your own! You can sign up for a free Checkly and Terraform Cloud account. You can find the provider and a sample module here.

P.S.: Curious about scaling your active monitoring setup with Checkly and Terraform? Read more in our recent article.

Share on social