product

Observability as Code with Terraform and Baselime

Observability as Code with Terraform and Baselime

3 mins read

04 March, 2024
Maksym Makuch
Senior Software Engineer @Baselime
Discuss this post on Slack
Join our Slack community to discuss this topic with 100s of CTOs, AWS heroes, community builders, and cloud computing experts.
Linkedin icon
Reddit icon

We're excited to launch support for Observability as Code with Terraform! Our Terraform provider enables you to create and manage your observability configurations as code, as part of your infrastructure.

Dashboard detecting changes

It is now possible to reuse your existing Terraform workflows in CI/CD pipelines to manage both your infrastructure and your observability configurations.

Getting started

To get started, add the Baselime Terraform provider and your Baselime API key to your Terraform configuration.

terraform {
  required_providers {
    baselime = {
      version = "~> 0.1.6"
      source  = "baselime/baselime"
    }
  }
}

provider "baselime" {
  api_key = "baselime_api_key"
}

Once this is completed, create queries, alerts and dashboards as code, directly in Terraform.

Queries

Build and reuse complex queries to understand how your application behaves. For example, to compute the latency of all requests with a specific git hash:

resource "baselime_query" "my_terraformed_query" {
  name        = "latency"
  calculations = [
    {
      operator = "P90"
      key      = "traceDuration"
    },
    {
      operator = "MEDIAN"
      key      = "traceDuration"
    }
  ]
  filters     = [
    {
      key       = "hash"
      operation = "="
      value     = var.git_hash
      type      = "string"
    }
  ]
}

Building queries with Terraform enables you to keep common queries across your app in sync with your development. You can use them set alerts or to build dashboards directly within Terraform.

Query created with Terraform

Alerts

Create and manage alerts to get notified when something interesting occurs in your application. It could be an error, high latencies, or anything specific to your application that is of interest.

Alerts are based on queries. An alert will run a query on a schedule, and notify you if a threshold you define is met.

resource "baselime_alert" "my_terraformed_alert" {
  name        = "high-latencies"
  channels = [
    {
      type    = "email"
      targets = ["foo@baselime.io"]
    }
  ]
  query = baselime_query.my_terraformed_query.id
  threshold = {
    operator = "GREATER_THAN"
    value    = 10000
  }
  frequency = "10m"
  window    = "5m"
}

With alerts, you don't need to manually dig into your data to know when something needs your attention. Baselime surfaces things for you. And now, you can automate everything with Terraform.

Alert created with Terraform

Dashboards

Dashboards are a collection of queries. They enable you to quickly visualise the state of metrics you care about. It could be latencies or number of errors, but also reuqests by status code or number of users who signed up to your app: you define your own queries.

resource "baselime_dashboard" "my_terraformed_dashboard" {
  name        = "my-terraformed-dashboard"
  widgets = [
    {
      query_id    = baselime_query.my_terraformed_query.id
      type        = "timeseries"
      name        = "Latencies"
      description = "Request latencies for latest deployment"
    }
  ]
}

With the Baselime Terraform provider, you can now create and manage dashboards as code, as part of your CI/CD pipelines.

Dashboard created with Terraform

Import existing resources

If you already have existing resources created in the Baselime console, you can import them into your Terraform configurations.

terraform import baselime_query.name_in_your_terraform_file name-in-baselime-console

Once imported, you can manage those resources within Terraform and keep them in sync with the Baselime console.

For more information on how to use the provider, check out the documentation.

What's Next?

We're currently supporting a limited number of resources with our Terraform provider. Within the next few months, we will expand it to support creating and managing:

  • Markers
  • Datasets (with all the sampling and obfuscating settings)
  • Public API keys, etc.

Check out our new Terraform Provider. Connect with us on Twitter or join our community to share your experience and feedback!

Discuss this post on Slack

Join our Slack community to discuss this topic with 100s of CTOs, AWS heroes, community builders, and cloud computing experts.

Related posts
Start resolving issues today.
Without the hassle.