Configure serverless Azure metrics
Complete the following steps to configure serverless Azure metrics.
Configure Azure authorization
To collect metrics from Azure Monitor, create a service principal with the proper authorization to allow Grafana Cloud to pull Azure metrics on your behalf.
Create a service principal with Azure CLI
Log in to your Azure account.
az login
List your available subscriptions.
az account list --output table
Create a service principal for each subscription you want to monitor, and give it the appropriate role. If a service principal already exists with this name, it will be updated with the role and scopes you provide. Make sure to replace
<subscription-id>
with the appropriate value.az ad sp create-for-rbac --name grafana-cloud-azure-metrics --role "Monitoring Reader" --scopes "/subscriptions/{subscriptionId}"
When the service principal is created, capture the output of the command. This output includes the credential information that you need for the Terraform configuration steps.
{ "appId": "54321a67-8fd9-123d-45d6-7891234567fd", "displayName": "grafana-cloud-azure-metrics", "password": "asdf1234~4321fdsa", "tenant": "12345a67-8fd9-123d-45d6-7891234567fd" }
Create a service principal with Terraform
The following snippet shows how to configure the Azure service principal using Terraform. You do not need to apply the Terraform to create the service principal before moving forward.
data "azurerm_client_config" "current" {}
resource "azuread_application" "grafana_cloud_azure_metrics" {
display_name = "grafana-cloud-azure-metrics"
}
resource "azuread_application_password" "grafana_cloud_azure_metrics" {
application_id = azuread_application.grafana_cloud_azure_metrics.client_id
end_date_relative = "8760h" # 1 year
}
resource "azurerm_role_assignment" "grafana_cloud_azure_metrics" {
scope = "/subscriptions/<subscription-id>"
role_definition_name = "Monitoring Reader"
principal_id = azuread_application.grafana_cloud_azure_metrics.client_id
}
Configure Grafana Cloud
After deciding how you want to configure the Azure service principal, you can configure Grafana Cloud to use it.
Create an Access Policy for the Grafana Terraform provider
Note: If you are already using the Grafana Terraform provider, you can skip this step after you ensure the access policy you are using has the following scopes:
- orgs: Read
- stacks: Read
- accesspolicies: Read, Write, Delete
Since this Access Policy is necessary to use the Terraform provider, you must create it through Grafana Cloud.
- Log in to Grafana Cloud.
- In the Cloud Portal, navigate to Security in the menu to the left and select Access Policies.
- Select Create Access Policy.
- Assign the required scopes.
If you don’t see the following scopes listed, use the Add scope text box to search for and add them:
- orgs: Read
- stacks: Read
- accesspolicies: Read, Write, Delete
- Click Create
- Find your created Access Policy and click Add token
- Enter a Token name and optionally configure an Expiration date
- Click Create to generate the token
- Click Copy to clipboard to copy the token to your clipboard and save it for use in the next steps
For more information on Access Policies and tokens, refer to the following: Create access policies and tokens.
Configure the Grafana Terraform provider with your Access Policy
Include the Grafana Terraform provider as a dependency in your Terraform configuration file. The version of the provider must be 3.18.0
or later.
terraform {
required_providers {
grafana = {
source = "grafana/grafana"
version = ">= 3.18.0"
}
}
}
Choose one of the following methods to configure the Grafana Terraform provider to use the provisioned Access Policy token.
- Embed the token in Terraform configuration,
provider "grafana" {
cloud_access_policy_token = "<cloud_access_policy_token_from_previous_step>"
}
- Use environment variable
GRAFANA_CLOUD_ACCESS_POLICY_TOKEN
set to the created token when running Terraform commands with the following provider block:
provider "grafana" {}
Configure the Terraform provider to use the Cloud Provider API
We suggest creating a dedicated Access Policy for interacting with the Cloud Provider API through the Terraform provider. The following snippet shows how to configure the Access Policy using Terraform. You do not need to apply the Terraform to create the Access Policy before moving forward.
The Azure Credential Terraform model
The Cloud Provider portion of the Grafana Terraform provider enables configuring Azure metric collection through the following resources and data sources.
The following is a minimal Azure Credential resource definition.
resource "grafana_cloud_provider_azure_credential" "myazurecred" {
stack_id = <stack_id>
name = "my-credential"
client_id = "<client_id>"
client_secret = "<client_secret>"
tenant_id = "<tenant_id>"
}
Full Terraform Example
The following snippet shows a full Terraform example that provisions an Azure service principal and uses it to collect Azure metrics.