Terraform

Manage your Better Stack resources automatically with Terraform. Using Terraform allows you to define your monitoring and logging configurations as code, making it easy to version, reuse, and automate your setup.

Uptime & On-call

The Better Stack Uptime provider for Terraform allows you to manage your Uptime resources, including monitors, heartbeats, integrations, on-call configuration, and status pages.

For detailed information on available resources and their configurations, please see the Uptime provider documentation.

The provider is built on top of the Uptime API and is open-source.

Logs and Metrics

The Better Stack Telemetry provider for Terraform allows you to manage your Telemetry resources, including telemetry sources, metrics, dashboards, and alerts.

For more information, please see the Telemetry provider documentation.

The provider is built on top of the Telemetry API and is open-source.

Importing existing resources into Terraform

If you have resources that were created through the Better Stack UI or API, you can bring them under Terraform management using the terraform import command. This allows you to manage your existing infrastructure as code without having to recreate it.

The process involves two main steps: importing the resource into your Terraform state and then writing the corresponding configuration code.

1. Import the resource

First, define an empty resource block in your Terraform configuration file (e.g., main.tf). This tells Terraform where to import the state.

main.tf
resource "betteruptime_monitor" "imported_monitor" {
  # This block is intentionally left empty for the import.
}

Next, run the terraform import command, providing the resource address and the resource ID from Better Stack.

Most resources use a simple numeric ID, which you can find in the URL in the Better Stack UI. Some nested resources use a composite ID, typically in the format parent_id/resource_id.

Simple ID (Uptime monitor) Composite ID (Telemetry exploration alert)
# The ID is the number from the monitor's URL.
terraform import betteruptime_monitor.imported_monitor 12345
# The ID is 'exploration_id/alert_id'.
terraform import logtail_exploration_alert.my_alert 12345/789

Not sure which ID to use?

If you use an incorrect ID format, the provider will return an error message guiding you to the correct format. For example: Error: invalid alert ID format "12345", expected 'exploration_id/alert_id'.

2. Generate the configuration

After the import is successful, the resource exists in your Terraform state, but your configuration file is still empty. You need to write the configuration to match the imported state.

You can inspect the imported resource's state to see all its attributes:

View imported state
terraform state show 'betteruptime_monitor.imported_monitor'

The output will show the full configuration for the resource. Copy this into your .tf file to complete the process.

Output
# betteruptime_monitor.imported_monitor:
resource "betteruptime_monitor" "imported_monitor" {
    id                  = "12345"
    monitor_type        = "http"
    name                = "My Website Monitor"
    url                 = "https://my-website.com"
    # ... all other attributes from the state show output ...
}

After adding the configuration, run terraform plan to verify that your code matches the state. There should be no changes to apply.

That's it. Your existing resource is now fully managed by Terraform. 🎉