This is documentation for the next version of Grafana documentation. For the latest stable release, go to the latest version.

Enterprise Open source

Manage Cloud Provider Observability in Grafana Cloud using Terraform

Manage Cloud Provider Observability, including Amazon CloudWatch and Microsoft Azure resources, in Grafana Cloud using Terraform. For more information on Cloud Provider Observability, refer to the Cloud Provider Observability documentation.

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
    • For more information on setting up a Grafana Cloud account, refer to Get started.
  • Terraform installed on your machine
  • Administrator permissions in your Grafana instance

Note

Save all the Terraform configuration files in the same directory.

Configure authentication for the Grafana Provider

The Grafana Provider is a logical abstraction of an upstream API that you can use to interact with Grafana Cloud resources. You must configure it with the following information:

  • A Grafana Cloud access policy token that includes the permissions the provider needs to access the Grafana Cloud Provider API.
  • A regional Cloud Provider API endpoint to establish which Grafana Cloud stack you are accessing.

To configure authentication for the Grafana Provider:

  1. Create a Grafana Cloud access policy and token.

    • To create an access policy for your organization, refer to the Create an access policy for a stack steps and use the following scopes listed for the supported Amazon CloudWatch or Microsoft Azure resources:
      • Amazon CloudWatch
        • Metrics scrape or resource metadata scrape
          • integration-management:read
          • integration-management:write
          • stacks:read
        • Metric streams
          • metrics:write
        • ALB access logs, logs with Lambda, or logs with Amazon Data Firehose
          • logs:write
    • Microsoft Azure
      • Serverless metrics
        • integration-management:read
        • integration-management:write
        • stacks:read
      • Logs with Azure functions
        • logs:write
  2. Obtain the regional Cloud Provider API endpoint.

    • To obtain the regional Cloud Provider API endpoint, use your access policy token and the following command to return a list of all of the Grafana stacks you own, along with their respective Cloud Provider API hostnames:
    Bash
    curl -sH "Authorization: Bearer <GRAFANA_CLOUD_ACCESS_POLICY_TOKEN>" "https://grafana.com/api/instances" | \
    jq '[.items[]|{stackName: .slug, clusterName:.clusterSlug, cloudProviderAPIURL: "https://cloud-provider-api-\(.clusterSlug).grafana.net"}]'

    Note

    If this endpoint does not work, your Grafana Cloud Provider API endpoint may be using the new URL format. For more information on Grafana Cloud URLs, refer to Determine Grafana Cloud URLs based on region.

  3. Create a file named cloud-provider.tf and add the following code block:

    terraform
    terraform {
      required_providers {
        grafana = {
          source = "grafana/grafana"
        }
      }
    }
    
    provider "grafana" {
      cloud_provider_url          = "<CLOUD_PROVIDER_URL>"
      cloud_provider_access_token = "<CLOUD_PROVIDER_ACCESS_TOKEN>"
    }
  4. Replace the following placeholders:

    • <CLOUD_PROVIDER_URL>: The regional Cloud Provider API endpoint from the previous step.
    • <CLOUD_PROVIDER_ACCESS_TOKEN>: The Grafana Cloud access policy token you created in step 1.

    Alternatively, set the GRAFANA_CLOUD_PROVIDER_URL and GRAFANA_CLOUD_PROVIDER_ACCESS_TOKEN environment variables instead of hardcoding them in cloud-provider.tf.

  5. Initialize and apply your Terraform configuration once you have added your resource definitions:

    Bash
    terraform init
    terraform apply

Configure your resources

To find instructions for configuring specific Amazon CloudWatch and Microsoft Azure resources in Cloud Provider Observability using Terraform, refer to the following documents:

  • Amazon CloudWatch
    • Metrics scrape: Pull CloudWatch metrics from multiple regions for your AWS account, without needing to install Grafana Alloy.
    • Metric streams: Push metrics with CloudWatch metric streams using Amazon Data Firehose, providing real-time insights and scalability while simplifying configuration and reducing cost and manual effort.
    • ALB access logs: Send application load balancer access logs from AWS to Grafana Cloud using a Lambda function.
    • Logs with Lambda: Send logs to Grafana Cloud from multiple AWS services using a lambda-promtail function.
    • Logs with Amazon Data Firehose: Send logs from AWS to Grafana Cloud with Amazon Data Firehose and minimal infrastructure.
  • Microsoft Azure
    • Serverless metrics: Monitor your Azure resources without the need to configure or deploy a collector by using Cloud Provider Observability.
    • Logs with Azure functions: Send Azure event logs to a Loki endpoint using an Azure function that subscribes to an Azure event hub.

Grafana cloud provider resources

You can define the following Cloud Provider Observability resources and data sources using Terraform:

Resource nameDescription
grafana_cloud_provider_aws_accountRepresents an AWS IAM role that authorizes Grafana Cloud to pull Amazon CloudWatch metrics for a set of regions. Usually, there’s one of these resources per configured AWS account. For a full reference of this resource, refer to the Terraform Grafana Provider reference documentation.
grafana_cloud_provider_aws_cloudwatch_scrape_jobRepresents a Grafana AWS scrape job. This configures Grafana to fetch a list of metrics and statistics for one or many AWS services, and for a given grafana_cloud_provider_aws_account. For a full reference of this resource, refer to the Terraform Grafana Provider reference documentation.
grafana_cloud_provider_aws_resource_metadata_scrape_jobRepresents a Grafana AWS Resource Metadata scrape job. This resource configures Grafana to fetch resource metadata for one or multiple AWS services, for a given grafana_cloud_provider_aws_account. For a full reference of this resource, refer to the Terraform Grafana Provider reference documentation.
grafana_cloud_provider_azure_credentialA resource representing an Azure Service Principal credential used by Grafana Cloud to pull Azure Monitor metrics from one or more subscriptions. For a full reference of this resource, refer to the Terraform Grafana Provider resource documentation.

You can also read existing Cloud Provider Observability configuration with the following data sources:

Data source nameDescription
grafana_cloud_provider_aws_accountReads an AWS account that’s connected to Grafana Cloud.
grafana_cloud_provider_aws_cloudwatch_scrape_jobReads a single Amazon CloudWatch scrape job.
grafana_cloud_provider_aws_cloudwatch_scrape_jobsLists all Amazon CloudWatch scrape jobs in a stack.
grafana_cloud_provider_azure_credentialReads a Microsoft Azure credential that’s connected to Grafana Cloud.