table of contents Table of contents

Snippets and environment variables

Checkly exposes handy resources to avoid code duplication and reduce the maintenance load on whoever is maintaining checks and scripts.

Code snippets

Code snippets are useful for reusing bits of code without duplication. They can be used for setup & teardown scripts in API checks, as well as in browser checks and come as standalone resources:

resource "checkly_snippet" "procedure-login" {
  name   = "Login"  // The snippet name
  script   = <<EOT  // The content of the snippet
    page.fill('#field-email')
    page.fill('#field-password')
    page.click('#btn-login')
EOT
}

For tidiness and ease of use, it is recommended to store scripts in separate files, instead of using the inline option:

resource "checkly_snippet" "procedure-login" {
  name   = "Login"
  script = file("${path.module}/snippets/login.js") // Our script is contained in this file
}

Environment variables

Environment variables help you store data (like credentials or other strings) on Checkly that you might need to use in one or more checks and groups. They can exist as arguments in check or group type resources:

// In a check:
resource "checkly_check" "e2e-login" {
  name                      = "Login Flow"
  type                      = "BROWSER"
  activated                 = true
  should_fail               = false
  frequency                 = 1
  double_check              = true
  ssl_check                 = false
  use_global_alert_settings = true
  
  environment_variables = {         // This sets check-level environment variables
    TEST_EMAIL = "user@email.com",
    TEST_USER_ID = "7d8d8288-43a6-44b0-ba08-4c5736c1b6ed"
  }
  
  locations = [
    "us-west-1",
    "eu-central-1"
  ]

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

// In a group:
resource "checkly_check_group" "key-shop-flows" {
  name      = "Key Shop Flows"
  activated = true
  muted     = false

  environment_variables = {         // This sets group-level environment variables
    TEST_EMAIL = "user@email.com",
    TEST_USER_ID = "7d8d8288-43a6-44b0-ba08-4c5736c1b6ed"
  }

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

  concurrency               = 3
  double_check              = true
  use_global_alert_settings = false
}
We are working on enabling Terraform users to set up environment variables at account level, too.

Last updated on August 15, 2024. You can contribute to this documentation by editing this page on Github