Terraform best practices

  • Latest Dynatrace
  • Reference
  • 4-min read
  • Published Aug 18, 2025

This guide describes how to migrate Terraform from one tenant to another. This is useful for making backups or rolling out a standardized configuration to several tenants.

Ensure OneAgent communication is reconfigured via oneagentctl for migration. Direct OneAgent migration might generate new entity IDs, potentially causing configuration loss. Improper migration can cause significant environment issues.

For more info about oneagentctl, see OneAgent configuration via command-line interface.

Prerequisites

Before you use Terraform to migrate between Dynatrace environments, you must:

  • Install Terraform CLI with the Dynatrace Terraform provider. Make sure the Dynatrace Terraform provider is available in your PATH. For more information, see Install Terraform.
  • Have an OAuth client or platform token with the appropriate permissions. For more information, see Create API access token.

Steps

Define the environment variables to identify the Dynatrace tenant for configuration retrieval.

There are two ways to invoke the export utility and subsequent Terraform execution, the difference being how variables are used.

Setting the source and target environment variables separately is beneficial with the iterative method, where you might run multiple exports and Terraform executions within the same command line shell.

With this method, you use the same variables for both environments.

Optionally, the environment variable DYNATRACE_TARGET_FOLDER can be set to specify an output directory. If the variable is not set, the default folder ./configuration will be used.

There are two main approaches for migration:

  • Bulk: Transfer all configurations from a source to a fresh target environment.
  • Iterative: Transfer configurations by resource group. This is ideal for target environments with existing configurations.

For iterative migration, use the Terraform Migration Helper spreadsheet. It aids in tracking migration order and completion.

Bulk migration

  1. Ensure the target environment doesn't have any existing configurations.

  2. Use the -migrate flag. This will create all the necessary files with resource dependencies and hardcoded entity IDs.

    terraform-provider-dynatrace.exe -export -migrate

To view the resources that are excluded from the default export, run the provider with the -list-exclusions flag. To export any of the excluded resources, see Example usage.

Iterative migration

The iterative approach is useful for migrating specific resource groups or to environments with existing configurations.

  • Set environment variables to handle duplicates:

    • DYNATRACE_DUPLICATE_REJECT=ALL: Prevents overwriting existing configurations.
    • DYNATRACE_DUPLICATE_HIJACK=ALL: Allows overwriting existing configurations.
  • Use the -migrate and -datasources flags. This will create all the necessary files with dependencies via data sources and hardcoded entity IDs.

    terraform-provider-dynatrace.exe -export -migrate -datasources <resourcename>

optional

Common issues causing resources to fail during the migration:

  • Some API endpoints validate whether entity IDs in the configuration exist. If missing in the target environment, re-apply once the entities are present.
  • Some Classic API endpoints might reject outdated configurations. For instance, calculated service metrics now require a management zone or a condition marked with service property.

If you experience any other errors, please create an issue in the Dynatrace GitHub repo.

Related tags
Software Delivery