Your infrastructure is already described in Terraform: networks, clusters, databases. Observability, however, is still configured by hand, screen by screen. With the Dynatrace Terraform provider, alerting profiles, management zones and dashboards join the same repository, the same plan and the same code review as the rest of the platform.

For the SRE team the gain is direct: a new service arrives with its alerting and its dashboard in the same merge request, drift between environments disappears and a rollback is a git revert. Less toil, fewer orphaned alerts, and an MTTR that no longer depends on someone’s memory.

The goal of this tutorial: configure the provider, create a management zone, an alerting profile and a dashboard, then import what already exists with the export tool. The provider targets Dynatrace SaaS and Dynatrace Managed; some resources, such as platform documents (new dashboards), are SaaS only. Each step says so.

Prerequisites

  • Terraform installed (a recent version) and a shared state backend (S3, GCS, Terraform Cloud…): state must never stay on a laptop.
  • An API token with settings.read and settings.write for Settings 2.0 resources (alerting, management zones), plus ReadConfig and WriteConfig for classic dashboards.
  • On SaaS, for documents: an OAuth client with the document:documents:read, document:documents:write, document:documents:delete and document:trash.documents:delete permissions.

If your SLOs are already managed in Terraform, the approach is the same; here we focus on alerting and the structure of the environment.

Configuring the Dynatrace Terraform provider

Step 1: declare the provider

# providers.tf terraform { required_providers { dynatrace = { source = "dynatrace-oss/dynatrace" version = "~> 1.0" } } } provider "dynatrace" {} 

The provider block stays empty: the URL and token are read from environment variables, never written in the code.

# SaaS export DYNATRACE_ENV_URL="https://<env-id>.live.dynatrace.com" # Managed # export DYNATRACE_ENV_URL="https://<your-domain>/e/<env-id>" export DYNATRACE_API_TOKEN="dt0c01.XXXX" # SaaS only, for dynatrace_document export DT_CLIENT_ID="dt0s02.XXXX" export DT_CLIENT_SECRET="dt0s02.XXXX.YYYY" export DT_ACCOUNT_ID="urn:dtaccount:<uuid>" terraform init 

Step 2: create a management zone

A management zone limits the view and the alerting to a scope. Here, everything that belongs to the prod-eu Kubernetes cluster, monitored by Dynatrace Operator (see Connecting Dynatrace Operator to a cluster):

# zones.tf resource "dynatrace_management_zone_v2" "prod_eu" { name = "k8s-prod-eu" rules { rule { type = "ME" enabled = true entity_selector = "" attribute_rule { entity_type = "CLOUD_APPLICATION_NAMESPACE" attribute_conditions { condition { case_sensitive = false key = "KUBERNETES_CLUSTER_NAME" operator = "EQUALS" string_value = "prod-eu" } } } } } } 

type = "ME" means a rule on monitored entities. Choosing the rules and tags that structure your zones is covered in Management zones and automatic tagging.

Step 3: create an alerting profile

# alerting.tf resource "dynatrace_alerting" "paiement" { name = "Paiement - production" management_zone = "" rules { rule { include_mode = "INCLUDE_ALL" tags = ["team:paiement", "env:production"] delay_in_minutes = 0 severity_level = "AVAILABILITY" } rule { include_mode = "INCLUDE_ALL" tags = ["team:paiement", "env:production"] delay_in_minutes = 10 severity_level = "PERFORMANCE" } } } 

Critical lines: severity_level picks the problem family, delay_in_minutes the delay before notification. An availability issue notifies immediately; a performance degradation waits ten minutes, which filters out short spikes and reduces on-call noise. The profile filters by tags rather than by zone, as the provider documentation recommends. Then link it to a notification integration (see Alerting profiles and notification integrations).

Step 4: create a dashboard

On SaaS, platform dashboards are documents:

# dashboards.tf (SaaS) resource "dynatrace_document" "paiement" { type = "dashboard" name = "Paiement - santé" content = file("${path.module}/dashboards/paiement.json") } 

The simplest way to get the JSON: build the dashboard in the UI, then download its content and place it in dashboards/. The repository then keeps a reviewable copy of every tile and every DQL query.

On Managed (and for classic dashboards on SaaS), use the dynatrace_json_dashboard resource:

resource "dynatrace_json_dashboard" "paiement" { contents = file("${path.module}/dashboards/paiement-classic.json") } 

Step 5: plan and apply

terraform fmt -check terraform validate terraform plan -out=tfplan terraform apply tfplan 

Read the plan like a code review: a -/+ line (destroy then recreate) on a production alerting profile deserves a question before the apply.

Step 6: import what exists

The provider binary ships an export tool that generates HCL from an existing environment. You call it directly, not through terraform:

./terraform-provider-dynatrace -export dynatrace_alerting dynatrace_management_zone_v2 

Add -import-state to also produce the state, so Terraform treats the objects as already managed. Review the generated code before merging it: it reflects what exists, inconsistencies included. Dashboards are excluded from the export by default and must be requested explicitly.

How to check that it works

  • terraform apply ends with Apply complete! and the expected number of resources.
  • A second terraform plan shows No changes: the environment matches the code.
  • In Dynatrace, the k8s-prod-eu management zone appears in the settings and does filter the cluster’s namespaces; the « Paiement – production » profile lists its two rules.
  • Through the API, the profile exists under the builtin:alerting.profile schema:
curl -s -H "Authorization: Api-Token $DYNATRACE_API_TOKEN" \ "$DYNATRACE_ENV_URL/api/v2/settings/objects?schemaIds=builtin:alerting.profile&fields=value" \ | jq '.items[].value.name' 

Good practices

  • Drift detection: run terraform plan every night in CI. A non-empty plan flags a manual change.
  • One tool per object: a profile managed by Terraform must not also be managed by Monaco or the UI.
  • Modules per team: a « service » module that creates a zone, profile and dashboard from a few variables ensures every new service starts with correct alerting.
  • Secrets out of the code: environment variables in CI, encrypted state, no token in a versioned .tfvars.
  • Sensitive data in state: state may contain configuration values; restrict access to it like a secret.

Frequently asked questions

Does the Dynatrace Terraform provider work with Managed?

Yes for most configuration and Settings 2.0 resources, with a URL of the form https://<domain>/e/<env-id>. SaaS platform resources, such as dynatrace_document, do not apply to Managed.

Why does my plan want to recreate a resource that has not changed?

Often because a server-side computed field differs from the written value (list order, default value). Align the code with the value returned by the export, then run the plan again.

Should I prefer Monaco?

If your teams already live in Terraform, stay with Terraform: one toolchain, one review process. Monaco remains relevant for Dynatrace-focused teams that want a lighter model.

Conclusion

With the Dynatrace Terraform provider, alerting and environment structure follow the same lifecycle as infrastructure: code, plan, review, apply. Start by exporting what exists, stabilise a first module for one service, then switch on drift detection. Observability becomes reproducible, and a new environment is ready in a single apply.

span.observer is an independent community blog, not affiliated with Dynatrace LLC. Dynatrace is a trademark of its owner. Provider resources and attributes change: check them in the provider documentation and the official documentation.

Originally published on span.observer. French version: https://span.observer/fr/terraform-dynatrace-alerting-dashboards/.