How to provision Datadog resources with Terraform

Provision your Datadog resources right in Terraform

Adding the Datadog Provider

provider "datadog" {
api_key = var.datadog_api_key
app_key = var.datadog_app_key
}
data "aws_secretsmanager_secret_version" "datadog_secrets" {
secret_id = var.datadog_secrets_id
}
provider "datadog" {
api_key = jsondecode(data.aws_secretsmanager_secret_version.datadog_secrets.secret_string)["DATADOG_API_KEY"]
app_key = jsondecode(data.aws_secretsmanager_secret_version.datadog_secrets.secret_string)["DATADOG_APP_KEY"]
}
terraform {
required_providers {
datadog = {
source = "DataDog/datadog"
}
}
}

Adding Datadog Resources

main.tf
variables.tf
versions.tf
terraform {
required_providers {
datadog = {
source = "DataDog/datadog"
}
}
}
version = ">= 2.0.0, < 3"
resource "datadog_monitor" "database_storage_low" {
name = "${title(var.service)} database storage low"
type = "metric alert"
message = "Monitor triggered. Notify: ${var.notify}"

query = "avg(last_1h):avg:aws.rds.free_storage_space{environment:${var.env},service:${var.service}} < ${var.db_storage_critical}"

monitor_thresholds {
critical = var.db_storage_critical
warning = var.db_storage_warning
}

notify_no_data = false

tags = [
"env:${var.env}",
"service:${var.service}",
"team:${var.team}
]
}
variable "service" {
type = string
description = "Your service name. Should match the service tag on your db"
}
variable "env" {
type = string
description = "The environment we're in. Should match the environment tag on your db"
}
variable "team" {
type = string
description = "Your team's name"
}
variable "notify" {
type = string
description = "Where to route alerts to. Will look something like @pagerduty-<service> or @slack-<channel> etc."
}
variable "db_storage_critical" {
type = number
description = "Lowest acceptable free storage in bytes"
default = 10000000000 // ~10 gb
}
variable "db_storage_warning" {
type = number
description = "Low free storage warning threshold in bytes"
default = 20000000000 // ~20 gb
}

Backend software engineer at Stash. Pursuing an MDIV at Reformed Theological Seminary. Princeton 2018 - Electrical Engineering. Opinions are my own.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store