Documentation for automated readers
A curated documentation index is available at: https://grafana.com/llms.txt
A complete documentation index is available at: https://grafana.com/llms-full.txt
These indexes can help with page discovery before fetching individual documents.
This page is also available in Markdown, which may be easier for automated readers and AI tools to parse than HTML. The Markdown version is available at https://grafana.com/docs/grafana-cloud/learn-and-build/as-code/terraform/terraform-cloud-stack.md, or by sending Accept: text/markdown to https://grafana.com/docs/grafana-cloud/learn-and-build/as-code/terraform/terraform-cloud-stack/. For broader documentation discovery, the curated index is available at https://grafana.com/llms.txt and the complete index is available at https://grafana.com/llms-full.txt.
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:
- A Grafana Cloud account, as shown in Get started.
- Terraform installed on your machine.
Note
Save all the Terraform configuration files in the same directory.
Create a Cloud stack
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.tfand add the following:terraformterraform { 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" }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:readorgs:readstack-dashboards:readstacks:readdashboards:writeorgs:writestack-dashboards:writestacks:writestack-service-accounts:writedashboards:deletestack-dashboards:deletestacks:delete
<STACK_NAME>: The name of your stack.<REGION>: The region where you want to create the stack, for exampleusoreu.<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.
Create a file named
datasource.tfand add the following:terraformresource "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>" }) }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.
Create a file named
folder.tfand add the following:terraformresource "grafana_folder" "my_folder" { provider = grafana.my_stack title = "<FOLDER_TITLE>" }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.
Create a file named
dashboard.tfand add the following:terraformresource "grafana_dashboard" "my_dashboard" { provider = grafana.my_stack config_json = file("<DASHBOARD_FILE>.json") folder = grafana_folder.my_folder.uid }Replace
<DASHBOARD_FILE>with the name of the JSON file that contains the dashboard model.The
config_jsonargument 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.
Initialize the working directory and install the Grafana provider:
shterraform initPreview the changes that Terraform makes:
shterraform planApply the configuration:
shterraform 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_tokenresource 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_folderresource 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:
- Manage dashboards from a Git repository with CI/CD. Refer to Create and manage dashboards using Terraform and GitHub Actions.
- Configure other Grafana Cloud products. Refer to Manage other Grafana Cloud products with Terraform.
- Explore all available resources in the Grafana provider documentation.
Was this page helpful?
Related resources from Grafana Labs







