To get you started with managing configurations, this section guides you through a simple example of creating a management zone with Dynatrace Configuration as Code via Terraform. You will learn how to create, update, and destroy a configuration.
Terraform CLI with the Dynatrace provider installed and available under PATH
.
Access token with at least the following permissions:
ReadConfig
)WriteConfig
)settings.read
)settings.write
)To create a token that works for all configurations, also include the following permissions.
ExternalSyntheticIntegration
)CaptureRequestData
)credentialVault.read
)credentialVault.write
)networkZones.read
)networkZones.write
)Certain resources require an OAuth client for authentication (for example, automation, document, and account management APIs). For details, see the resource-specific pages in the Terraform Registry.
Create a management zone for a web application using Terraform.
Inside your working directory, create a main.tf
file with the following content.
This file contains the Terraform configuration—a set of resource blocks that define the configuration. For more information on management zone resource, refer to the Terraform Registry documentation.
Consider using the export utility to export existing configurations from the environment.
resource "dynatrace_management_zone_v2" "TerraformExample" {name = "Terraform Example"rules {rule {type = "ME"enabled = trueattribute_rule {entity_type = "WEB_APPLICATION"attribute_conditions {condition {case_sensitive = truekey = "WEB_APPLICATION_NAME"operator = "EQUALS"string_value = "easyTravel"}}}}}}
Open a terminal and set the environment variables for your environment URL and API token. This identifies which tenant you'll be pushing configurations to.
In your working directory, run terraform plan
to generate an execution plan that provides a preview of the changes Terraform intends to make.
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:+ createTerraform will perform the following actions:# dynatrace_management_zone_v2.TerraformExample will be created+ resource "dynatrace_management_zone_v2" "TerraformExample" {+ id = (known after apply)+ legacy_id = (known after apply)+ name = "Terraform Example"+ rules {+ rule {+ enabled = true+ type = "ME"+ attribute_rule {+ entity_type = "WEB_APPLICATION"+ attribute_conditions {+ condition {+ case_sensitive = true+ key = "WEB_APPLICATION_NAME"+ operator = "EQUALS"+ string_value = "easyTravel"}}}}}}Plan: 1 to add, 0 to change, 0 to destroy.
After verifying the plan, execute terraform apply
to implement the proposed changes (in this case, pushing the management zone configuration to the environment).
dynatrace_management_zone_v2.TerraformExample: Creating...dynatrace_management_zone_v2.TerraformExample: Creation complete after 1s [id=*************)]Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
After the apply
command, you'll notice a terraform.tfstate
file. This is the Terraform state file that is automatically generated to keep track of the resources that Terraform is currently managing. It's crucial for subsequent Terraform operations.
Execute terraform plan
, which should indicate that no changes are needed.
dynatrace_management_zone_v2.easyTravel: Refreshing state... [id=*************]No changes. Your infrastructure matches the configuration.Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
To make a change, edit the main.tf
file. For instance, you can modify the string_value
from "easyTravel"
to "Terraform"
.
resource "dynatrace_management_zone_v2" "TerraformExample" {name = "Terraform Example"rules {rule {type = "ME"enabled = trueattribute_rule {entity_type = "WEB_APPLICATION"attribute_conditions {condition {case_sensitive = truekey = "WEB_APPLICATION_NAME"operator = "EQUALS"string_value = "Terraform"}}}}}}
After making your changes, execute terraform apply
that will update the management zone configuration in Dynatrace and adjust the Terraform state file accordingly.
dynatrace_management_zone_v2.easyTravel: Modifying... [id=*************]dynatrace_management_zone_v2.easyTravel: Modifications complete after 0s [id=*************]Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
To remove a configuration, run terraform plan
to confirm no changes are pending.
dynatrace_management_zone_v2.easyTravel: Refreshing state... [id=*************]No changes. Your infrastructure matches the configuration.Terraform has compared your real infrastructure against your configuration and found no differences, so no changes are needed.
To delete the management zone, execute terraform destroy
.
dynatrace_management_zone_v2.easyTravel: Destroying... [id=*************]dynatrace_management_zone_v2.easyTravel: Destruction complete after 0sDestroy complete! Resources: 1 destroyed.
The management zone configuration in the Dynatrace environment has been destroyed and the Terraform state file is now empty.
Next step: Terraform advanced example