Grafana Cloud

Configure the Grafana provider for App Platform resources in Grafana Cloud

Learn how to configure the Grafana Terraform provider to manage App Platform resources in a Grafana Cloud stack. App Platform resources include several features and products (such as Service Center services, alert rules, or dashboards) that use the Grafana Kubernetes-style APIs. For more information, refer to API structure in Grafana.

In this guide you’ll set the provider’s stack_id and create a Service Center service with the grafana_apps_servicemodel_component_v1alpha1 resource. You can use the same provider configuration for any other App Platform resource.

Why App Platform resources need a stack ID

The App Platform resources HTTP API requires that every resource belongs to a namespace. In Grafana Cloud, the namespace of a stack is stacks-<STACK_ID>. To manage these resources in Grafana Cloud, you must set the stack_id argument in the provider configuration, in addition to url and auth. If you omit stack_id, Terraform fails with an HTTP 403 - Forbidden error, even though url already points to your stack.

The provider builds the namespace from its own configuration, not from the stack URL:

  • If you set stack_id: The provider sends requests to the stacks-<STACK_ID> namespace of your stack.
  • If you don’t set stack_id: The provider falls back to org_id, which defaults to 1, and sends requests to the default namespace. Grafana Cloud rejects requests to this namespace with HTTP 403 - Forbidden.

Affected resources

In the Terraform registry, Grafana App Platform resources names start with grafana_apps_. The stack_id requirement applies to the following resources:

AreaResources
Alertinggrafana_apps_rules_*, grafana_apps_notifications_*, and grafana_apps_alertenrichment_alertenrichment_v1beta1
Dashboardsgrafana_apps_dashboard_dashboard_v1beta1, grafana_apps_dashboard_dashboard_v2beta1, and grafana_apps_dashboard_dashboard_v2
Git Syncgrafana_apps_provisioning_connection_v0alpha1 and grafana_apps_provisioning_repository_v0alpha1
Playlistsgrafana_apps_playlist_*
Product activationgrafana_apps_productactivation_*
Saved queriesgrafana_apps_queries_query_v1
Secrets Managementgrafana_apps_secret_*
Service Centergrafana_apps_servicemodel_component_v1alpha1

For the complete list, refer to the Grafana provider documentation.

Unlike typed grafana_apps_* resources, grafana_apps_generic_resource discovers the stack namespace automatically and uses stack_id only as a fallback or to verify that the configured stack matches the instance.

Other resources, such as grafana_folder, grafana_dashboard, and grafana_rule_group, use the legacy Grafana HTTP API and don’t need stack_id. You can set stack_id in a provider that manages both kinds of resources.

Before you begin

Before you begin, ensure you have the following:

  • Grafana Cloud stack: You need a Grafana Cloud stack to provision resources into. To create one, refer to Create and manage a Grafana Cloud stack using Terraform.
  • Terraform CLI: Install Terraform on your machine.
  • Grafana Terraform provider: Use grafana/grafana version 4.44.0 or later, which adds the grafana_apps_servicemodel_component_v1alpha1 resource that this guide uses.

Note

All of the following Terraform configuration files should be saved in the same directory.

Find your stack ID

The stack ID is the numeric ID of your Grafana Cloud stack, for example 123456.

To find your stack ID, follow these steps:

  1. Open the Grafana Cloud Portal.
  2. Select your stack.
  3. In the stack details, copy the numeric Instance ID. That value is your stack_id.

Create a service account token

Terraform authenticates against your Grafana Cloud stack with a service account token.

To create a service account and token, follow these steps:

  1. Create a service account in your Grafana Cloud stack and assign it the Admin role. To create a service account, refer to Create a service account.

    If you don’t want to grant the Admin role, assign the roles or role-based access control (RBAC) permissions that the resources you manage require.

  2. Add a token to the service account. To add a token, refer to Add a token to a service account.

  3. Copy the token and store it securely, because you can’t view it again after you leave the page.

Configure the Grafana provider

Create a file named main.tf and add the following:

terraform
terraform {
  required_providers {
    grafana = {
      source  = "grafana/grafana"
      version = ">= 4.44.0"
    }
  }
}

variable "grafana_auth" {
  type      = string
  sensitive = true
}

provider "grafana" {
  url  = "<GRAFANA_URL>"
  auth = var.grafana_auth

  # Required for App Platform resources in Grafana Cloud.
  stack_id = <STACK_ID>
}

Replace the following placeholders:

  • <GRAFANA_URL>: The URL of your Grafana Cloud stack, for example https://my-stack.grafana.net/.
  • <STACK_ID>: The stack ID that you found in Find your stack ID, for example 123456.

The grafana_auth variable holds the service account token. To keep the token out of your configuration files, set it through the TF_VAR_grafana_auth environment variable when you apply the configuration.

You can also set the stack ID through the GRAFANA_STACK_ID environment variable instead of the stack_id argument.

If you create the stack in the same Terraform configuration, reference the stack ID from the grafana_cloud_stack resource instead of hard-coding it:

terraform
provider "grafana" {
  alias = "my_stack"

  url      = grafana_cloud_stack.my_stack.url
  auth     = grafana_cloud_stack_service_account_token.cloud_sa.key
  stack_id = grafana_cloud_stack.my_stack.id
}

Define an App Platform resource

The following example creates a service in Service Center.

Create a file named service.tf and add the following:

terraform
resource "grafana_apps_servicemodel_component_v1alpha1" "<RESOURCE_NAME>" {
  metadata {
    uid = "<SERVICE_UID>"
  }

  spec {
    title = "<SERVICE_TITLE>"
  }
}

Replace the following placeholders:

  • <RESOURCE_NAME> is a name for the resource in Terraform, for example payments.
  • <SERVICE_UID> is the identifier of the service, for example payments-api. The identifier can only contain lowercase letters, numbers, and dashes, and must be 2 to 63 characters long.
  • <SERVICE_TITLE> is the display name of the service in Service Center, for example Payments API.

If your configuration uses more than one Grafana provider, add a provider argument to the resource that references the provider with stack_id, for example provider = grafana.my_stack.

For the complete schema, including owners, dependencies, and links, refer to the grafana_apps_servicemodel_component_v1alpha1 resource documentation.

Apply the Terraform configuration

In a terminal, run the following commands from the directory where all of the configuration files are located.

  1. Initialize a working directory containing Terraform configuration files.

    sh
    terraform init
  2. Set the service account token, then preview the changes that Terraform makes.

    sh
    export TF_VAR_grafana_auth='<TOKEN>'
    terraform plan

    Replace <TOKEN> with the service account token that you created in Create a service account token.

  3. Apply the configuration files.

    sh
    terraform apply

    After you confirm, Terraform creates the service:

    console
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

To verify the result, open Service Center in your Grafana Cloud stack and check that the service is listed.

Troubleshoot HTTP 403 errors

If terraform plan or terraform apply fails with HTTP 403 - Forbidden for an App Platform resource, the error message includes the API group of the resource, for example components.servicemodel.ext.grafana.com.

To fix the error, check the following:

  • The provider sets stack_id: Make sure that the provider "grafana" block that the resource uses includes stack_id, or that the GRAFANA_STACK_ID environment variable is set. If your configuration has more than one Grafana provider, check that the resource references the provider with stack_id in its provider argument.
  • The stack ID matches the stack URL: Make sure that stack_id is the ID of the stack in url. If the IDs don’t match, the provider sends requests to a namespace that the token can’t access.
  • The service account has the required permissions: If the error persists after you set stack_id, check that the service account token in auth belongs to a service account with the Admin role, or with the permissions that the resource requires.

Summary

In this guide, you configured the Grafana provider with stack_id and created a Service Center service, an App Platform resource, in a Grafana Cloud stack. You also learned how to fix HTTP 403 - Forbidden errors caused by a missing stack_id.

To learn more about managing Grafana Cloud using Terraform, refer to the Grafana provider documentation.

Next steps