### Install golangci-lint via Go Source: https://github.com/datadog/terraform-provider-datadog/blob/master/DEVELOPMENT.md Install the golangci-lint static analysis tool for any platform using the Go installation method. ```shell # Any platform via Go go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@latest ``` -------------------------------- ### Install golangci-lint on macOS Source: https://github.com/datadog/terraform-provider-datadog/blob/master/DEVELOPMENT.md Install the golangci-lint static analysis tool using Homebrew on macOS. ```shell # macOS brew install golangci-lint ``` -------------------------------- ### datadog_integration_slack_channel Resource Example Source: https://github.com/datadog/terraform-provider-datadog/blob/master/docs/resources/integration_slack_channel.md Example usage of the datadog_integration_slack_channel resource to configure a Slack channel integration. ```terraform resource "datadog_integration_slack_channel" "test_channel" { account_name = "foo" channel_name = "#test_channel" display { message = true notified = false snapshot = false tags = true } } ``` -------------------------------- ### Datadog Domain Allowlist Resource Example Source: https://github.com/datadog/terraform-provider-datadog/blob/master/docs/resources/domain_allowlist.md This example shows how to configure the datadog_domain_allowlist resource to enable the allowlist and specify allowed domains. ```terraform resource "datadog_domain_allowlist" "example" { enabled = true domains = ["@gmail.com"] } ``` -------------------------------- ### datadog_security_monitoring_rule Resource Example Source: https://github.com/datadog/terraform-provider-datadog/blob/master/docs/resources/security_monitoring_rule.md This example demonstrates how to create a security monitoring rule with multiple queries, cases, and options. ```terraform resource "datadog_security_monitoring_rule" "myrule" { name = "My rule" message = "The rule has triggered." enabled = true query { name = "errors" query = "status:error" aggregation = "count" data_source = "logs" group_by_fields = ["host"] } query { name = "warnings" query = "status:warning" aggregation = "count" data_source = "logs" group_by_fields = ["host"] } case { status = "high" condition = "errors > 3 && warnings > 10" notifications = ["@user"] } options { evaluation_window = 300 keep_alive = 600 max_signal_duration = 900 } tags = ["type:dos"] } ``` -------------------------------- ### datadog_monitor Data Source Example Source: https://github.com/datadog/terraform-provider-datadog/blob/master/docs/data-sources/monitor.md Example of how to use the datadog_monitor data source to retrieve a monitor by its name and tags. ```terraform data "datadog_monitor" "test" { name_filter = "My awesome monitor" monitor_tags_filter = ["foo:bar"] } ``` -------------------------------- ### datadog_action_connection Data Source Example Source: https://github.com/datadog/terraform-provider-datadog/blob/master/docs/data-sources/action_connection.md Example of how to use the datadog_action_connection data source to retrieve a connection by its ID. ```terraform data "datadog_action_connection" "my_connection" { id = "11111111-2222-3333-4444-555555555555" } ``` -------------------------------- ### Datadog Synthetics Locations Data Source Example Source: https://github.com/datadog/terraform-provider-datadog/blob/master/docs/data-sources/synthetics_locations.md This example shows how to use the datadog_synthetics_locations data source to fetch available locations and then use those locations to configure a datadog_synthetics_test resource. ```terraform data "datadog_synthetics_locations" "test" {} resource "datadog_synthetics_test" "test_api" { type = "api" locations = keys(data.datadog_synthetics_locations.test.locations) } ``` -------------------------------- ### Datadog Dashboard V2 with Free Layout Source: https://github.com/datadog/terraform-provider-datadog/blob/master/docs/resources/dashboard_v2.md Example of creating a Datadog dashboard using the 'free' layout type. This snippet includes a timeseries widget with specific layout dimensions and a template variable. ```terraform resource "datadog_dashboard_v2" "free_dashboard" { title = "Free Layout Dashboard" description = "Created using the Datadog provider in Terraform" layout_type = "free" widget { timeseries_definition { request { formula { formula_expression = "my_query_1" } query { metric_query { data_source = "metrics" query = "avg:system.cpu.user{*} by {env}" name = "my_query_1" aggregator = "sum" } } } } widget_layout { height = 16 width = 25 x = 0 y = 0 } } template_variable { name = "var_1" prefix = "host" default = "aws" } } ``` -------------------------------- ### Datadog Dashboard V2 with Ordered Layout Source: https://github.com/datadog/terraform-provider-datadog/blob/master/docs/resources/dashboard_v2.md Example of creating a Datadog dashboard using the 'ordered' layout type. This snippet demonstrates various widget types including timeseries, query value, and group widgets, along with template variables. ```terraform resource "datadog_dashboard_v2" "ordered_dashboard" { title = "Ordered Layout Dashboard" description = "Created using the Datadog provider in Terraform" layout_type = "ordered" widget { timeseries_definition { request { formula { formula_expression = "my_query_1" alias = "CPU Usage" } query { metric_query { data_source = "metrics" query = "avg:system.cpu.user{*} by {env}" name = "my_query_1" aggregator = "avg" } } } title = "CPU Usage by Environment" live_span = "1h" } } widget { query_value_definition { request { formula { formula_expression = "my_query_1" } query { metric_query { data_source = "metrics" query = "avg:system.load.1{*}" name = "my_query_1" aggregator = "avg" } } conditional_formats { comparator = "<" value = "2" palette = "white_on_green" } conditional_formats { comparator = ">" value = "2.2" palette = "white_on_red" } } autoscale = true precision = "4" text_align = "right" title = "System Load" live_span = "1h" } } widget { group_definition { layout_type = "ordered" title = "Group Widget" widget { note_definition { content = "cluster note widget" background_color = "pink" font_size = "14" text_align = "center" show_tick = true tick_edge = "left" tick_pos = "50%" } } } } template_variable { name = "var_1" prefix = "host" default = "aws" } } ``` -------------------------------- ### Get the API Key Manager role and list users assigned to it Source: https://github.com/datadog/terraform-provider-datadog/blob/master/docs/data-sources/role_users.md This example demonstrates how to first retrieve a role using its name filter and then use the role's ID to list all users assigned to that role. ```terraform # Get the API Key Manager role data "datadog_role" "api_key_manager" { filter = "API Key Manager" } # List users assigned to the API Key Manager role data "datadog_role_users" "api_key_managers" { role_id = data.datadog_role.api_key_manager.id } ``` -------------------------------- ### Create a recurring SLO correction Source: https://github.com/datadog/terraform-provider-datadog/blob/master/docs/resources/slo_correction.md This example shows how to create a recurring SLO correction using the `rrule` attribute. The correction is defined with a start time, recurrence rule (daily for 3 occurrences), duration, and timezone. The `slo_id` is linked to an existing SLO. ```terraform resource "datadog_slo_correction" "example_slo_correction_with_recurrence" { category = "Scheduled Maintenance" description = "correction example with recurrence" start = 1735707000 rrule = "FREQ=DAILY;INTERVAL=3;COUNT=3" duration = 3600 slo_id = datadog_service_level_objective.example_slo.id timezone = "UTC" } ``` -------------------------------- ### Create a datastore and add items to it Source: https://github.com/datadog/terraform-provider-datadog/blob/master/docs/resources/datastore_item.md This example demonstrates how to create a Datadog datastore and then add items to it using the `datadog_datastore_item` resource. The `item_key` is used to specify the primary key for each item. ```terraform resource "datadog_datastore" "example" { name = "users-datastore" description = "Datastore for user data" primary_column_name = "id" primary_key_generation_strategy = "none" } # Create a datastore item with the primary key specified in the value map resource "datadog_datastore_item" "user1" { datastore_id = datadog_datastore.example.id item_key = "user-123" value = { id = "user-123" username = "john_doe" email = "john@example.com" status = "active" } } # Create another datastore item resource "datadog_datastore_item" "user2" { datastore_id = datadog_datastore.example.id item_key = "user-456" value = { id = "user-456" username = "jane_doe" email = "jane@example.com" status = "active" } } ``` -------------------------------- ### Example Usage: Deployment and Logs Queries Source: https://github.com/datadog/terraform-provider-datadog/blob/master/docs/resources/software_catalog.md This snippet demonstrates how to define queries for deployment events and logs within the Datadog Terraform provider configuration. ```hcl ## Example Usage - name: "deployment events" query: "app:myapp AND type:github" - name: "event type B" query: "app:myapp AND type:github" logs: - name: "critical logs" query: "app:myapp AND type:github" - name: "ops logs" query: "app:myapp AND type:github" EOF } ``` -------------------------------- ### datadog_csm_threats_policy Resource Example Source: https://github.com/datadog/terraform-provider-datadog/blob/master/docs/resources/csm_threats_policy.md Example of how to define a datadog_csm_threats_policy resource with basic configuration. ```terraform resource "datadog_csm_threats_policy" "my_policy" { name = "my_policy" description = "My policy" enabled = true host_tags_lists = [["env:prod", "team:backend"], ["env:prod", "team:frontend"]] } ``` -------------------------------- ### Basic datadog_monitor Resource Source: https://github.com/datadog/terraform-provider-datadog/blob/master/docs/resources/monitor.md This example demonstrates the creation of a basic metric alert monitor. It includes essential configurations like name, type, message, query, monitor thresholds, and tags. Use this snippet to set up a standard metric alert. ```terraform resource "datadog_monitor" "foo" { name = "Name for monitor foo" type = "metric alert" message = "Monitor triggered. Notify: @hipchat-channel" escalation_message = "Escalation message @pagerduty" query = "avg(last_1h):avg:aws.ec2.cpu{environment:foo,host:foo} by {host} > 4" monitor_thresholds { warning = 2 critical = 4 } include_tags = true tags = ["foo:bar", "team:fooBar"] } ``` -------------------------------- ### datadog_team_permission_setting Example Source: https://github.com/datadog/terraform-provider-datadog/blob/master/docs/resources/team_permission_setting.md Example usage of the datadog_team_permission_setting resource to manage team membership permissions. ```terraform resource "datadog_team" "foo" { description = "Example team" handle = "example-team-updated" name = "Example Team-updated" } resource "datadog_team_permission_setting" "foo" { team_id = datadog_team.foo.id action = "manage_membership" value = "organization" } ``` -------------------------------- ### Run Import Script and Terraform Apply Source: https://github.com/datadog/terraform-provider-datadog/blob/master/scripts/cloud-cost-import-existing-resources/README.md Export Datadog API and App keys, run the import script, and then apply the generated Terraform configuration to import resources. ```bash $ export DD_API_KEY="***" $ export DD_APP_KEY="***" $ ./scripts/generate_import_config.sh ./my-terraform-project Checking prerequisites... Fetching existing resources from Datadog... Checking for AWS CUR configurations... Found 2 AWS CUR configuration(s) Checking for Azure UC configurations... Found 1 Azure UC configuration(s) Checking for GCP UC configurations... Found 1 GCP UC configuration(s) Checking for custom allocation rules... Found 3 custom allocation rule(s) Checking for tag pipeline rulesets... Found 2 tag pipeline ruleset(s) ======================================== Import blocks generated! ======================================== Total resources found: 8 Generated files: - ./my-terraform-project/provider.tf - ./my-terraform-project/imports.tf Initializing Terraform... Generating resource configuration from imports... Running: terraform plan -generate-config-out=generated_resources.tf ======================================== Configuration generated successfully! ======================================== Generated files: - ./my-terraform-project/provider.tf (provider configuration) - ./my-terraform-project/imports.tf (import blocks) - ./my-terraform-project/generated_resources.tf (resource configurations) Next steps: 1. Review ./my-terraform-project/generated_resources.tf: - Rename resources to meaningful names - Adjust configurations as needed - Remove any resources you don't want to manage 2. Import all resources into Terraform state: terraform apply 3. Verify the import succeeded: terraform plan You should see 'No changes' if everything matches correctly. 4. (Optional) Clean up the imports file: After successful import, you can delete ./my-terraform-project/imports.tf $ cd my-terraform-project $ terraform apply Terraform will perform the following actions: # datadog_aws_cur_config.aws_cur_123456789012 will be imported # datadog_azure_uc_config.azure_uc_987654321098 will be imported # ... (and so on) Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes datadog_aws_cur_config.aws_cur_123456789012: Importing... datadog_aws_cur_config.aws_cur_123456789012: Import complete! ... Apply complete! Resources: 8 imported, 0 added, 0 changed, 0 destroyed. $ terraform plan No changes. Your infrastructure matches the configuration. $ rm imports.tf # Clean up after successful import ``` -------------------------------- ### Import a datadog_app_key_registration resource Source: https://github.com/datadog/terraform-provider-datadog/blob/master/docs/resources/app_key_registration.md Import an existing application key registration using its ID. ```shell terraform import datadog_app_key_registration.my_app_key_registration 11111111-2222-3333-4444-555555555555 ``` -------------------------------- ### datadog_logs_metric Resource Example Source: https://github.com/datadog/terraform-provider-datadog/blob/master/docs/resources/logs_metric.md This example shows how to define a datadog_logs_metric resource with compute, filter, and group_by configurations. ```terraform resource "datadog_logs_metric" "testing_logs_metric" { name = "testing.logs.metric" compute { aggregation_type = "distribution" path = "@duration" } filter { query = "service:test" } group_by { path = "@status" tag_name = "status" } group_by { path = "@version" tag_name = "version" } } ``` -------------------------------- ### HTTP Connection with Token Authentication Example Source: https://github.com/datadog/terraform-provider-datadog/blob/master/docs/resources/action_connection.md Set up an HTTP connection with token authentication for Datadog Actions. This example demonstrates using secret tokens, custom headers, URL parameters, and a JSON body, with sensitive variables for tokens. ```terraform resource "datadog_action_connection" "http_connection" { name = "My HTTP connection with token auth" http { base_url = "https://catfact.ninja" token_auth { token { type = "SECRET" name = "token1" value = var.token1 } token { type = "SECRET" name = "token2" value = var.token2 } header { name = "header-one" value = "headerval" } header { name = "h2" value = "{{ token1 }} test" } url_parameter { name = "param1" value = "{{ token1 }}" } url_parameter { name = "param2" value = "paramVal2" } body { content_type = "application/json" content = jsonencode({ key = "mykey" value = "maybe with a secret: {{ token2 }}" }) } } } } variable "token1" { type = string sensitive = true } variable "token2" { type = string sensitive = true } ``` -------------------------------- ### Import a datadog_datastore_item resource Source: https://github.com/datadog/terraform-provider-datadog/blob/master/docs/resources/datastore_item.md This example shows how to import an existing `datadog_datastore_item` resource into your Terraform state. The import command requires the datastore ID and the item key, formatted as `datastore-id:item-key`. ```shell terraform import datadog_datastore_item.foo "datastore-id:item-key" ```