Terraform basic example
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.
Prerequisites
-
Terraform CLI with the Dynatrace provider installed and available under
PATH
. -
Access token with at least the following permissions:
- Read configuration (
ReadConfig
) - Write configuration (
WriteConfig
) - Read settings (
settings.read
) - Write settings (
settings.write
)
To create a token that would work for all configuration, please include the following permissions in addition to the above.
- Create and read synthetic monitors, locations, and nodes (
ExternalSyntheticIntegration
) - Capture request data (
CaptureRequestData
) - Read credential vault entries (
credentialVault.read
) - Write credential vault entries (
credentialVault.write
) - Read network zones (
networkZones.read
) - Write network zones (
networkZones.write
)
- Read configuration (
Build a configuration
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.
1resource "dynatrace_management_zone_v2" "TerraformExample" {2name = "Terraform Example"3rules {4 rule {5 type = "ME"6 enabled = true7 attribute_rule {8 entity_type = "WEB_APPLICATION"9 attribute_conditions {10 condition {11 case_sensitive = true12 key = "WEB_APPLICATION_NAME"13 operator = "EQUALS"14 string_value = "easyTravel"15 }16 }17 }18 }19}20} -
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.
1set DYNATRACE_ENV_URL=https://########.live.dynatrace.com2set DYNATRACE_API_TOKEN=dt0c01.########.######## -
In your working directory, run
terraform plan
to generate an execution plan that provides a preview of the changes Terraform intends to make.1Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:2+ create34Terraform will perform the following actions:56# dynatrace_management_zone_v2.TerraformExample will be created7+ resource "dynatrace_management_zone_v2" "TerraformExample" {8 + id = (known after apply)9 + legacy_id = (known after apply)10 + name = "Terraform Example"1112 + rules {13 + rule {14 + enabled = true15 + type = "ME"1617 + attribute_rule {18 + entity_type = "WEB_APPLICATION"1920 + attribute_conditions {21 + condition {22 + case_sensitive = true23 + key = "WEB_APPLICATION_NAME"24 + operator = "EQUALS"25 + string_value = "easyTravel"26 }27 }28 }29 }30 }31 }3233Plan: 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).1dynatrace_management_zone_v2.TerraformExample: Creating...2dynatrace_management_zone_v2.TerraformExample: Creation complete after 1s [id=*************)]34Apply complete! Resources: 1 added, 0 changed, 0 destroyed.After the
apply
command, you'll notice aterraform.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.
Modify your configuration
-
Execute
terraform plan
, which should indicate that no changes are needed.1dynatrace_management_zone_v2.easyTravel: Refreshing state... [id=*************]23No changes. Your infrastructure matches the configuration.45Terraform 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 thestring_value
from"easyTravel"
to"Terraform"
.1resource "dynatrace_management_zone_v2" "TerraformExample" {2name = "Terraform Example"3rules {4 rule {5 type = "ME"6 enabled = true7 attribute_rule {8 entity_type = "WEB_APPLICATION"9 attribute_conditions {10 condition {11 case_sensitive = true12 key = "WEB_APPLICATION_NAME"13 operator = "EQUALS"14 string_value = "Terraform"15 }16 }17 }18 }19}20} -
After making your changes, execute
terraform apply
that will update the management zone configuration in Dynatrace and adjust the Terraform state file accordingly.1dynatrace_management_zone_v2.easyTravel: Modifying... [id=*************]2dynatrace_management_zone_v2.easyTravel: Modifications complete after 0s [id=*************]34Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
Delete the configuration
-
To remove a configuration, run
terraform plan
to confirm no changes are pending.1dynatrace_management_zone_v2.easyTravel: Refreshing state... [id=*************]23No changes. Your infrastructure matches the configuration.45Terraform 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
.1dynatrace_management_zone_v2.easyTravel: Destroying... [id=*************]2dynatrace_management_zone_v2.easyTravel: Destruction complete after 0s34Destroy 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