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 thestacks-<STACK_ID>namespace of your stack. - If you don’t set
stack_id: The provider falls back toorg_id, which defaults to1, and sends requests to thedefaultnamespace. Grafana Cloud rejects requests to this namespace withHTTP 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:
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/grafanaversion 4.44.0 or later, which adds thegrafana_apps_servicemodel_component_v1alpha1resource 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:
- Open the Grafana Cloud Portal.
- Select your stack.
- 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:
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.
Add a token to the service account. To add a token, refer to Add a token to a service account.
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 {
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 examplehttps://my-stack.grafana.net/.<STACK_ID>: The stack ID that you found in Find your stack ID, for example123456.
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:
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:
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 examplepayments.<SERVICE_UID>is the identifier of the service, for examplepayments-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 examplePayments 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.
Initialize a working directory containing Terraform configuration files.
terraform initSet the service account token, then preview the changes that Terraform makes.
export TF_VAR_grafana_auth='<TOKEN>' terraform planReplace
<TOKEN>with the service account token that you created in Create a service account token.Apply the configuration files.
terraform applyAfter you confirm, Terraform creates the service:
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 theprovider "grafana"block that the resource uses includesstack_id, or that theGRAFANA_STACK_IDenvironment variable is set. If your configuration has more than one Grafana provider, check that the resource references the provider withstack_idin itsproviderargument. - The stack ID matches the stack URL: Make sure that
stack_idis the ID of the stack inurl. 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 inauthbelongs 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
- To manage alerting resources, refer to Use Terraform to provision alerting resources.
- To manage saved queries, refer to Manage saved queries using Terraform.
- To manage secure values, refer to Manage secrets in Grafana Cloud using Terraform.
- To synchronize a Git repository with Grafana, refer to Set up Git Sync with Terraform.


