Getting started with Terraform and StatusPal status pages
Learn how to provision your StatusPal status page, including monitoring and incident automation, with Terraform in this step-by-step guide.
Introduction
Terraform, created by HashiCorp, is a popular infrastructure-as-code software tool used to provision various types of infrastructure and services, including StatusPal status pages, monitoring checks, and incident automation.
This step-by-step guide teaches you how to set up StatusPal status pages with Terraform from the ground up, including the following resources:
StatuPal status page
Status page services
Enable monitoring checks for your services
Configure incident automation from external monitoring services
Before you begin
To use this guide, you should have some basic knowledge of both StatusPal and Terraform. If you don't have a StatusPal account, you can register here. Also, you will need to install the Terraform CLI.
1. Bootstrap Terraform and the StatusPal provider
Start by initializing a working directory and creating a Terraform configuration file:
Next, instruct Terraform to install and use the StatusPal provider, by setting the terraform and required_providers blocks in main.tf:
terraform {# Require Terraform version >= 1.1.0 required_version =">= 1.1.0"# Require the latest version of the StatusPal provider# You can find it here: https://registry.terraform.io/providers/statuspal/statuspal/latestrequired_providers { statuspal = { source ="statuspal/statuspal" version ="0.2.7" } }}
In this code block, you're setting the required version of Terraform to 1.9.0 and setting the StatusPal provider to the latest version. Using the right version constraints for your setup will provide better stability with your Terraform runs.
Now that you've set your Terraform and StatusPal provider versions, you need to configure the StatusPal provider.
2. Configure the StatusPal provider
With terraform all set, configure the StatusPal provider with the following items:
Your StatusPal API key, it can be a user API key or an organization API key.
Your StatusPal region. It can be US or EU.
In main.tf, set those values on the provider:
provider "statuspal" { api_key ="uk_aERPQU1kUzUrRmplaXJRMlc2TDEwZz09"# Your Statuspal user or organization API key region ="US"# US or EU}
By setting these values on the StatusPal provider, you're configuring that provider to make changes on behalf of your account through StatusPal APIs.
You can also configure the StatusPal provider usingenvironment variables. This is a useful way to set default values for your provider configuration.
For more information about configuring the StatusPal provider, please feel free to check out our official providerdocumentation.
With your StatusPal provider configured, initialize Terraform:
terraforminit
When Terraform finishes installing and registering the StatusPal provider, you'll receive a success message and some actionable next steps, such as running terraform plan. Before you can run terraform plan, however, you need to create your resources.
3. Configure a status page with services
With the StatusPal provider configured and initialized, you can define a status page.
Using the statuspal_status_page resource you can create an example status page:
resource "statuspal_status_page" "example" { organization_id ="1"# Your StatusPal organization ID. Find it in your organization's settings (https://docs.statuspal.io/faq#how-to-access-my-statuspal-organizations-settings), at the end of the URL (e.g. /admin/orgs/1) status_page = { name ="Example Terraform Status Page" url ="example.com" time_zone ="America/New_York" }}
Now you should be able to test your configuration with a dry run:
terraformplan
You should see output that displays Terraform's execution plan. The plan contains the actions Terraform performs when your run terraform apply:
In this case, the plan shows you that Terraform will create a new status page when you run terraform apply. After verifying the details, execute the plan to provision the status page resource in your StatusPal account:
terraformapply
Every time you apply changes, Terraform asks you to confirm the actions you've told it to run. Type "yes".
While it's running, Terraform sends logs to your console:
Log in to StatusPal and navigate to /admin/orgs/:organization_id to confirm that Terraform created your new status page in your organization.
At this point you can add services to your status page:
# Manage example service of the previously defined example status page's subdomain.resource "statuspal_service" "example" { status_page_subdomain = statuspal_status_page.example.status_page.subdomain service = { name ="Example Terraform Service" }}# Manage example child service of the previously defined example service.resource "statuspal_service" "child" { status_page_subdomain = statuspal_status_page.example.status_page.subdomain service = { name ="Example Terraform Child Service" parent_id = statuspal_service.example.service.id }}
Run terraform plan again to see what will happen if you apply the plan.
In this case, the plan shows you that Terraform will create 2 services when you run terraform apply. Maybe you already noticed, it's possible to create child services also. After verifying the details, execute the plan to provision the service resources in your example status page:
terraformapply
The same procedure is before, you need to write "yes" to approve the changes.
While it's running, Terraform sends logs to your console:
Again, you can navigate to /admin/status_pages/:status_page_subdomain/services to confirm that Terraform created your new services in your status page.
4. Configure StatusPal monitoring on a service
To configure StatusPal monitoring on a service you will need the change a few attributes.
We will use the previously defined service as an example:
resource "statuspal_service" "example" { status_page_subdomain = statuspal_status_page.example.status_page.subdomain service = { name ="Example Terraform Service" monitoring ="internal"# <- this means to use the StatusPal monitoring ping_url ="https://www.statuspal.io"# <- this is the URL to ping in every minute }}
5. Configure 3rd-party monitoring with incoming webhook URL
To configure 3rd-party monitoring with incoming webhook URL on a service you will need the change a few attributes.
We will use the previously defined service as an example:
resource "statuspal_service" "example" { status_page_subdomain = statuspal_status_page.example.status_page.subdomain service = { name ="Example Terraform Service" monitoring ="webhook"# <- this means to use the incoming webhook monitoring webhook_monitoring_service ="custom-jsonpath"# <- with this attribute you can the webhook monitoring service webhook_custom_jsonpath_settings = { # <- this is the settings for custom-jsonpath type of webhook monitoring service jsonpath ="$.status" expected_result ="\"up\"" } }}output "example_service_incoming_webhook_url" { value = statuspal_service.example.service.incoming_webhook_url}
After applying the plan with terraform apply you will be able to see the incoming webhook URL in the outputs: