Grafana Cloud

Create and manage a Grafana Cloud stack using Terraform

Learn how to create a Grafana Cloud stack using Terraform, and add a data source, a folder, and a dashboard to it.

Note

The Grafana Terraform provider is updated frequently. For the latest provider version and the complete reference for every resource and data source, refer to the Grafana provider in the Terraform Registry.

Before you begin

Before you begin, ensure you have the following:

Note

Save all the Terraform configuration files in the same directory.

Create a Cloud stack

  1. Create a Terraform configuration file.

    This Terraform configuration creates a Grafana Cloud stack, and a service account and token that Terraform uses to manage resources in the stack.

    Create a file named cloud-stack.tf and add the following:

    terraform
    terraform {
      required_providers {
        grafana = {
          source  = "grafana/grafana"
          version = ">= 2.13.0"
        }
      }
    }
    // Step 1: Create a stack
    provider "grafana" {
      alias = "cloud"
      cloud_access_policy_token = "<CLOUD_ACCESS_POLICY_TOKEN>"
    }
    
    
    resource "grafana_cloud_stack" "my_stack" {
      provider = grafana.cloud
    
      name               = "<STACK_NAME>"
      slug               = "<STACK_NAME>"
      region_slug        = "<REGION>"
      delete_protection  = true
    }
    
    // Step 2: Create a service account and key for the stack
    resource "grafana_cloud_stack_service_account" "cloud_sa" {
      provider   = grafana.cloud
      stack_slug = grafana_cloud_stack.my_stack.slug
    
      name        = "<SERVICE_ACCOUNT_NAME>"
      role        = "Admin"
      is_disabled = false
    }
    
    resource "grafana_cloud_stack_service_account_token" "cloud_sa" {
      provider   = grafana.cloud
      stack_slug = grafana_cloud_stack.my_stack.slug
    
      name               = "terraform serviceaccount key"
      service_account_id = grafana_cloud_stack_service_account.cloud_sa.id
    }
    
    // Step 3: Create resources within the stack
    provider "grafana" {
      alias = "my_stack"
    
      url  = grafana_cloud_stack.my_stack.url
      auth = grafana_cloud_stack_service_account_token.cloud_sa.key
    }
    resource "grafana_folder" "my_folder" {
      provider = grafana.my_stack
    
      title = "Test Folder"
    }
  2. Replace the following placeholders:

    • <CLOUD_ACCESS_POLICY_TOKEN>: Your Grafana Cloud access policy token. To create one, refer to Grafana Cloud access policies. Add all stacks to the realms list. The example needs the following scopes:
      • dashboards:read
      • orgs:read
      • stack-dashboards:read
      • stacks:read
      • dashboards:write
      • orgs:write
      • stack-dashboards:write
      • stacks:write
      • stack-service-accounts:write
      • dashboards:delete
      • stack-dashboards:delete
      • stacks:delete
    • <STACK_NAME>: The name of your stack.
    • <REGION>: The region where you want to create the stack, for example us or eu.
    • <SERVICE_ACCOUNT_NAME>: A name for the service account that Terraform uses to manage resources in the stack.

The first provider block, grafana.cloud, uses the access policy token to authenticate to the Grafana Cloud API. Terraform uses it to create the stack, the service account, and the token.

The second provider block, grafana.my_stack, uses the service account token to authenticate to the new stack. Terraform uses it to create resources inside the stack.

Add a data source

This guide uses the InfluxDB data source. The required arguments for grafana_data_source vary depending on the type of data source.

  1. Create a file named datasource.tf and add the following:

    terraform
    resource "grafana_data_source" "influxdb" {
      provider = grafana.my_stack
    
      type          = "influxdb"
      name          = "<DATA_SOURCE_NAME>"
      url           = "<DATA_SOURCE_URL>"
      username      = "<USERNAME>"
      database_name = "<DATABASE_NAME>"
    
      secure_json_data_encoded = jsonencode({
        password = "<PASSWORD>"
      })
    }
  2. Replace the following placeholders:

    • <DATA_SOURCE_NAME>: The name of the data source in Grafana.
    • <DATA_SOURCE_URL>: The URL of your InfluxDB instance.
    • <USERNAME>: The username to authenticate with your data source.
    • <PASSWORD>: The password to authenticate with your data source.
    • <DATABASE_NAME>: The name of your database.

Add a folder

This Terraform configuration creates a folder in your Grafana instance using grafana_folder.

  1. Create a file named folder.tf and add the following:

    terraform
    resource "grafana_folder" "my_folder" {
      provider = grafana.my_stack
    
      title = "<FOLDER_TITLE>"
    }
  2. Replace <FOLDER_TITLE> with a name for the folder.

Add a dashboard to the folder

This Terraform configuration creates a dashboard inside the folder from the previous section using grafana_dashboard.

  1. Create a file named dashboard.tf and add the following:

    terraform
    resource "grafana_dashboard" "my_dashboard" {
      provider = grafana.my_stack
    
      config_json = file("<DASHBOARD_FILE>.json")
      folder      = grafana_folder.my_folder.uid
    }
  2. Replace <DASHBOARD_FILE> with the name of the JSON file that contains the dashboard model.

    The config_json argument reads the dashboard from its JSON model.

Apply the Terraform configuration

In a terminal, run the following commands from the directory that contains the configuration files.

  1. Initialize the working directory and install the Grafana provider:

    sh
    terraform init
  2. Preview the changes that Terraform makes:

    sh
    terraform plan
  3. Apply the configuration:

    sh
    terraform apply

Verify the resources

After Terraform applies the configuration, verify the following:

  • The new Grafana Cloud stack appears in the Cloud Portal.

    The Cloud Portal showing the new Grafana Cloud stack

  • The service account token appears in your Grafana instance. In the following image, the grafana_cloud_stack_service_account_token resource added the token named “terraform serviceaccount key”.

    The service account token created by Terraform

  • The data source, InfluxDB in this example, appears in Connections > Data sources in your Grafana instance.

    The InfluxDB data source created by Terraform

  • The folder appears in Dashboards. In the following image, the grafana_folder resource added a folder named “Demos”.

    The Demos folder created by Terraform

  • The dashboard appears inside the folder. In the following image, Terraform created a dashboard named “InfluxDB Cloud Demos” inside the “Demos” folder.

    The InfluxDB Cloud Demos dashboard created by Terraform

Next steps

You created a Grafana Cloud stack, and added a data source, a folder, and a dashboard to it using Terraform. To continue: