Manage analytics dashboards with Terraform

TL;DR

Use the konnect_dashboard resource from the terraform provider to define and manage dashboards. You can import existing dashboards or create new ones with configurable chart layouts, titles, and filters.

Prerequisites

This page is part of the Create custom dashboards with Konnect analytics series.

Complete the previous page, Create a dashboard from a template before completing this page.

This how-to requires you to install Terraform.

This is a Konnect tutorial and requires a Konnect personal access token.

  1. Create a new personal access token by opening the Konnect PAT page and selecting Generate Token.

  2. Export your token to an environment variable:

     export KONNECT_TOKEN='YOUR_KONNECT_PAT'
    
  3. Create an auth.tf file that configures the kong/konnect Terraform provider. Change server_url if you are using a region other than us:

    echo '
    terraform {
      required_providers {
        konnect = {
          source  = "kong/konnect"
        }
        konnect-beta = {
          source  = "kong/konnect-beta"
        }
      }
    }
       
    provider "konnect" {
      server_url = "https://us.api.konghq.com"
    }
       
    provider "konnect-beta" {
      server_url            = "https://us.api.konghq.com"
    }
    ' > auth.tf
    
  4. Next, initialize your project and download the provider:

    terraform init
    

The provider automatically uses the KONNECT_TOKEN environment variable if it is available. If you would like to use a custom authentication token, set the personal_access_token field alongside server_url in the provider block.

This guide requires being an Organization Admin or belonging to the Analytics admin team.

This guide requires an existing dashboard in Konnect. You can create one using the Create a custom dashboard guide.

Get the dashboard ID

In this tutorial, we’ll export an existing dashboard in Konnect and modify it with Terraform, so you can integrate Konnect Analytics with your existing workflow.

Managing dashboards with Terraform requires the dashboard ID of the target dashboard:

  1. Get an existing dashboard ID from the Konnect URL of your dashboard. It appears at the end of the URL when viewing the dashboard in the UI:
    https://cloud.konghq.com/us/analytics/dashboards/$DASHBOARD_ID
    
  2. Export the dashboard ID into an environment variable to be used later:
  export DASHBOARD_ID='fe1b6b51-2e7e-44d6-bfa5-CC93489b3eed'

Import the dashboard

Now configure Terraform to import a dashboard from Konnect.

echo '
import {
  provider = "konnect-beta"
  to = konnect_dashboard.service_dashboard_template
  id = "8e66b804-a060-466a-aa6c-145e7f696228"
}
' >> import.tf

Be sure to replace the id with the id you exported for your dashboard.

Generate the Terraform configuration

  1. Initialize Terraform:
     terraform init
    
  2. Generate the Terraform configuration for the dashboard:
     terraform plan -generate-config-out=create_dashboard.tf
    

This creates a new file named create_dashboard.tf that contains the Terraform resource definition for the imported dashboard.

Configure the dashboard

The new create_dashboard.tf file contains information about your dashboard. You can use this file to make changes to the dashboard in Konnect with Terraform:


          }
          layout = {
            position = {
              col = 4
              row = 6
            }
            size = {
              cols = 2
              rows = 2
            }
          }
          type = "chart"
        }
      },
    ]
  }
  labels = null
  name   = "Quick summary dashboard"
}

You can add a label to the dashboard by modifying the file to replace labels = null with the following:

  labels = {
    test = "test"
  }

Validate

To validate that your changes are working, make an update in the generated Terraform file (for example, change a chart label).

  1. Run the following command to apply the update:
    terraform apply -auto-approve
    
  2. After the change is applied, return to the Konnect dashboard manager to confirm the “test” label has been added to the dashboard.
Something wrong?

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!