# Synthetic monitoring with Terraform Cloud & Checkly

> Combining HashiCorp Terraform Cloud and Checkly lets you configure API and synthetic monitoring as part of your existing infrastructure codebase.

Source: https://www.checklyhq.com/blog/synthetic-monitoring-with-terraform-cloud-checkly/

---

[Blog](https://www.checklyhq.com/blog/) / [Terraform](https://www.checklyhq.com/blog/tag/terraform/)

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

[Giovanni Rago](https://www.checklyhq.com/blog/author/giovanni/)

January 12, 2021 · Updated January 18, 2024

> _With active monitoring being a vital part of digital infrastructure, Checkly's developer-first approach made it an obvious partner for HashiCorp T_erraform. - 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](https://www.terraform.io/cloud) and Checkly enables you to configure synthetic and [API monitoring](https://www.checklyhq.com/blog/what-is-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](https://github.com/checkly/terraform-cloud-sample).)

## 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](https://www.checklyhq.com/product/api-monitoring/) 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](https://danube-webshop.herokuapp.com/) can retrieve a list of available books at any time. Before we do anything else, we need to add the [Checkly Terraform provider](https://registry.terraform.io/providers/checkly/checkly/latest), 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](https://app.checklyhq.com/signup). 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](https://pptr.dev/) and [Playwright](https://playwright.dev/) 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 o[nline bookstore,](https://danube-webshop.herokuapp.com/) so we write or r[ecord](https://github.com/checkly/headless-recorder)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:

- Avoid having to manage a large number of checks manually, saving precious time.
- 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.
- Use [parameterised resources](https://www.terraform.io/docs/configuration/functions/fileset.html) 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](https://app.checklyhq.com/signup) and [Terraform Cloud](https://app.terraform.io/signup/account) account. You can find the provider and a sample module [here](https://registry.terraform.io/providers/checkly/checkly/latest).

P.S.: Curious about scaling your active monitoring setup with Checkly and Terraform? Read more in our [recent article](https://blog.checklyhq.com/scaling-puppeteer-playwright-on-checkly-with-terraform/).

[Giovanni Rago](https://www.checklyhq.com/blog/author/giovanni/)

Head of Customer Solutions

In This Article

- Setting up deep API monitoring
- Adding synthetic transaction monitoring
- The benefits of synthetic monitoring as code

Monitor with AI

Let your agents create & deploy monitors with deep understanding of your application

[Start for free](https://app.checklyhq.com/signup)

Share on social

Try Checkly

## Catch it before your users do

Run Playwright and API checks against production every minute, from locations around the world. Your checks live in your repo and deploy with your code.

[Start for free](https://app.checklyhq.com/signup) [Book a demo](https://www.checklyhq.com/demo-request/)

Free plan. No credit card.

# scaffold a project with example checks

$ npm create checkly@latest

# run them from our global locations

$ npx checkly test

# turn them into production monitors

$ npx checkly deploy

## Related Articles

[Synthetic Monitoring: What is it, Challenges, and How to Get Started August 7, 2023](https://www.checklyhq.com/blog/what-is-synthetic-monitoring/)[Frontend Synthetic Monitoring: What is it, Types and Use Cases January 5, 2024](https://www.checklyhq.com/blog/frontend-synthetic-monitoring/)[The Advent of Monitoring, Day 11: Testing and Monitoring: Should You Separate or Unite Them? December 18, 2023](https://www.checklyhq.com/blog/testing-and-monitoring-should-you-separate-or-unite/)[10 Benefits of Synthetic Monitoring: Why It's a Necessity November 24, 2023](https://www.checklyhq.com/blog/advancing-cloud-monitoring-benefits-of-synthetic-monitoring/)[The Advent of Monitoring, Day 1: What Are Synthetics and Why They Are Needed December 8, 2023](https://www.checklyhq.com/blog/the-advent-of-monitoring-day-1-what-are-synthetics/)
