Back to Grafana

Set up Git Sync with Terraform

docs/sources/as-code/observability-as-code/git-sync/git-sync-setup/set-up-terraform.md

13.1.03.7 KB
Original Source

Set up Git Sync with Terraform

{{< admonition type="note" >}}

Git Sync is now GA for Grafana Cloud, OSS and Enterprise. Refer to Usage and performance limitations to understand usage limits for the different tiers.

Contact Grafana for support or to report any issues you encounter and help us improve this feature.

{{< /admonition >}}

You can also configure Git Sync via the Grafana provisioning app platform using Terraform.

Before you begin

Before you begin, make sure to have the following:

  • A Grafana Cloud account or an on-prem Grafana instance.
  • Administrator permissions in your Grafana stack/instance.
  • Terraform 1.11 or later installed on your machine. Refer to the Terraform install documentation to learn more.

{{< admonition type="note" >}} Save all of the following Terraform configuration files in the same directory. {{< /admonition >}}

Configure the Grafana provider

Use this Terraform configuration to configure the Grafana provider with the authentication required to set up Git Sync.

  1. Create a service account and token in Grafana. For more information refer to Service account tokens or Create and manage a Grafana Cloud stack using Terraform.

  2. Make sure that the token has the Admin or the Provisioning:Repositories writer permission.

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

    terraform
       terraform {
         required_providers {
           grafana = {
             source  = "grafana/grafana"
             version = ">= 4.28.1"
           }
         }
       }
    
       provider "grafana" {
         url = "https://<your-stack>.grafana.net/"
         auth = var.grafana_service_account_token
         stack_id = var.grafana_stack_id
       }
    

Replace the following field values:

  • <Stack-URL> with the URL of your Grafana stack (for example, https://my-stack.grafana.net/)
  • <Service-account-token> with the service account token that you created (or <USERNAME>:<PASSWORD> for basic auth)
  • <Stack-ID> with the Grafana stack ID, if you are using a Grafana Cloud stack

{{< admonition type="caution" >}}

The Git Sync Terraform resources are App Platform resources and talk directly to the Grafana stack's API.

Do not use Grafana Cloud provider settings in this configuration. Configure the provider with url and auth (not cloud_api_url and cloud_access_policy_token). If you're using Grafana Cloud, set stack_id to your stack ID.

{{< /admonition >}}

Create the resources to use Git Sync

You need two resources for configure and manage Git Sync:

  • The repository resource configures the Git repository to sync Grafana resources with. For examples, refer to Repository resource in the Terraform registry.
  • The connection resource configures your Git provider credentials. For examples, refer to Connection resource in the Terraform registry.

For better understanding of the required resources, refer to Git Sync key concepts.