The Dynatrace Terraform provider is a dedicated integration that enables management of a Dynatrace monitoring environment with HashiCorp Terraform, a widely adopted Infrastructure as Code tool. The provider functions as a library that allows the Terraform syntax and functionalities to create custom Dynatrace configurations, such as a Dynatrace dashboard, workflow, or custom alert. It also exposes data sources, for example the entity ID of a service detected by Dynatrace, and allows those data points to be pulled into Dynatrace configurations.
The Dynatrace Terraform Provider enables you to manage Dynatrace resources using Terraform, an open source infrastructure as code software tool. This allows for automated and consistent deployments of monitoring configurations in Dynatrace. The provider is officially supported by Dynatrace and provided by Dynatrace Incorporated. Support is provided by the Dynatrace Support team, as described on the support page. The provider requires Terraform 0.13.x+.
Why Terraform with Dynatrace
Managing configurations in Dynatrace across multiple environments can be challenging without automation. Terraform provides a powerful way to define, manage, and scale Dynatrace configurations as code, reducing manual effort and ensuring consistency.
Benefits of using Terraform for Dynatrace automation include:
- Consistency: Configuration as code eliminates manual setup discrepancies.
- Scalability: Easily manage configurations across environments or teams.
- Automation: Apply, update, and destroy configurations seamlessly.
By adopting Terraform for Dynatrace, you unlock the benefits of Infrastructure as Code. This approach ensures consistency across environments, easier scalability and management, and collaboration via version control.
Prerequisites and Installation
Before you start, ensure you have the following:
- Terraform installed on your machine. You can download it from Terraform Downloads.
- A Dynatrace account with API access.
- Basic understanding of Terraform and its configuration files.
To use the Dynatrace Terraform Provider, you need to install it. Here is how you can do it:
- Create a directory for your Terraform configuration.
- Inside this directory, create a file named main.tf.
- Add the following code to the main.tf file:
provider "dynatrace" {
api_url = "https://YOUR_DYNATRACE_TENANT_ID.live.dynatrace.com/api"
token = "YOUR_API_TOKEN"
}
- Replace YOURDYNATRACETENANTID and YOURAPI_TOKEN with your actual Dynatrace tenant ID and API token.
With the provider set up, you can now manage various Dynatrace resources.
Provider Configuration and Resources
The Dynatrace Terraform provider is a library that allows you to use the Terraform syntax and functionalities to create a custom configuration resource, such as a Dynatrace dashboard, workflow, or custom alert. Additionally, it provides data sources such as the entity ID of a service detected by Dynatrace. The provider allows you to pull information from these data sources into Dynatrace.
Please check the documentation within the Terraform Registry for how to use and configure this Provider as well as for currently supported resources and data sources. Additional information can be found under Dynatrace Configuration as Code via Terraform.
Environment variables for troubleshooting are described in the documentation.
The provider can be used to automate management of Custom Attributes, Synthetic Monitoring, and Maintenance Windows. Project structure is typically organized to support reuse.
A common pattern for integrating modules in main.tf is:
module "synthetic_monitoring" {
source = "./modules/synthetic_monitoring"
synthetic_tests = local.synthetic_tests
}
The same principles can be applied to Custom Attributes and Maintenance Windows, following a similar structure:
- Define reusable modules.
- Use JSON files for resource definitions.
- Pass configurations dynamically to modules.
Terraform CLI Commands with Dynatrace
Terraform CLI offers a wide variety of commands to manage your resources. You can use the following native commands with the Dynatrace Terraform provider:
| Command | Purpose |
|---|---|
| init | Prepares your working directory for other commands |
| validate | Checks whether the configuration is valid |
| plan | Shows changes required by the current configuration |
| apply | Creates or updates the infrastructure |
| destroy | Destroys the previously created infrastructure |
Applying the Configuration
Run the following commands to deploy the resources:
- Initialise Terraform:
terraform init
Plan Changes:
terraform plan
This shows the changes Terraform will make in Dynatrace.Apply Changes:
terraform apply
Confirm the changes to apply them.
Export Functionality
In addition to acting as a Terraform Provider Plugin, the executable terraform-provider-dynatrace, terraform-provider-dynatrace.exe on Windows, can also get directly invoked. The utility then reaches out to the Dynatrace Environment specified by the command line arguments and fetches all currently supported configuration items. These results will then automatically get transformed into HCL and places each configuration item into its own .tf file.
Dynatrace implements the export command specifically for the Dynatrace Terraform provider:
- export: Exports the existing Dynatrace resources from a given Dynatrace environment.
In addition to Terraform's out-of-the-box functionality, the Dynatrace Terraform Provider can be executed as a standalone executable to export an existing configuration from a Dynatrace environment. This functionality provides an alternative to manually creating a Terraform configuration and an easy way to create templates based on an existing configuration.
When exporting resources, consider appropriate permission scopes for fetching the desired configuration items.
Define the environment variables to identify the Dynatrace tenant for configuration retrieval, according to the API support and access permission handling.
Optionally, set the environment variable DYNATRACETARGETFOLDER to designate an output directory, export folder. If not set, the default directory .configuration is used.
Go to the Terraform Dynatrace Provider executable in the terminal. This executable isn't generic, such as terraform.exe or ./terraform.
Typically, you can find it in the .terraform/providers/registry.terraform.io/dynatrace-oss/dynatrace/{providerversion}/{osversion}/terraform-provider-dynatrace_x.y.z/ directory.
Directly invoke the executable with the options you need.
The export command is documented with examples of how to use export. Please check out the documentation for detailed information about using the export functionality.
Project Structure and Scaling
Before diving into the code, let's look at the structure of our project. A typical layout separates modules for different resource types, keeps provider configuration in main.tf, and stores variable definitions and JSON based resource definitions separately.
Scaling the Approach
The same principles can be applied to Custom Attributes and Maintenance Windows, following a similar structure:
- Define reusable modules.
- Use JSON files for resource definitions.
- Pass configurations dynamically to modules.
Start small with a single module and gradually scale your configuration to include all Dynatrace resources. Happy automating.
Conclusion
The Dynatrace Terraform provider provides an officially supported path to treat monitoring configuration as code. It combines the declarative model of Terraform with Dynatrace's monitoring capabilities, allowing dashboards, workflows, custom alerts, synthetic monitoring, maintenance windows and custom attributes to be defined, versioned, and applied consistently.
Installation begins with provider configuration using api_url and token, followed by resource definitions in HCL. The native Terraform workflow of init, validate, plan, apply and destroy applies directly to Dynatrace resources. The provider specific export command and direct invocation of terraform-provider-dynatrace enables configuration discovery from an existing environment, transforming live configuration into HCL files and reducing manual authoring.
The combination of reusable modules, JSON driven resource definitions, and environment variable handling for tenant identification and output folders supports scalable governance across teams and environments. Consistency is achieved through configuration as code, scalability through module reuse, and automation through the standard Terraform lifecycle.