Manage AWS Integrations as Code

Connect AWS accounts to Dash0 and choose the CloudWatch metrics to scrape with Terraform and the Dash0 CLI, so you can onboard many accounts without the Dash0 UI.

An AWS integration has two parts you can manage as code:

  • The integration itself: A CloudFormation stack in your AWS account. It creates the IAM roles Dash0 uses and registers the integration with Dash0. You deploy the same template the Connect AWS wizard launches, but with your own tooling instead of the Dash0 UI.
  • The CloudWatch metric scrape configuration: Which namespaces and metrics Dash0 scrapes for the integration, optionally filtered by resource tags and regions. This is the same configuration as the integration's Metrics tab.

Managing both as code lets you connect many AWS accounts in one change, keep the scrape configuration in version control, and review changes before they apply.

PartTerraformDash0 CLI
Integration (CloudFormation stack)aws_cloudformation_stack from the AWS providerNot supported
CloudWatch metric scrape configurationdash0_aws_metric_scrape_config, Dash0 Terraform provider v1.22.0 or laterdash0 apply and dash0 -X aws-metric-scrape-configs, Dash0 CLI v1.20.0 or later

Prerequisites

  • Auth token with all permissions: The CloudFormation stack registers the integration with an auth token that has the All permissions (querying, ingesting and configuring) permission. A token with fewer permissions fails the stack with insufficient permissions. See Auth Tokens. Dash0 then creates a separate ingest-only token for each integration, so one token can register any number of accounts.
  • AWS prerequisites: Each account needs AWS Resource Explorer, and a CloudTrail trail if you turn on the Lambda integration. See the prerequisites in Set Up AWS Integration.
  • AWS permissions: The identity that deploys the stack needs permissions to create CloudFormation stacks, StackSets, and IAM roles in each account.

Connect an AWS Account with Terraform

Deploy the Dash0 CloudFormation template with the AWS provider's aws_cloudformation_stack resource, and pass the stack's IntegrationId output to dash0_aws_metric_scrape_config:

hcl
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 5.0"
    }
    dash0 = {
      source  = "dash0hq/dash0"
      version = ">= 1.22.0"
    }
  }
}

resource "aws_cloudformation_stack" "dash0_integration" {
  name         = "dash0-integration"
  template_url = "https://s3.eu-west-1.amazonaws.com/public-integrations-production.eu-west-1.aws.dash0.com/dash0-customer-integration-cloudformation-v4.yaml"

  parameters = {
    ApiKey                = var.api_key
    Dataset               = var.dataset
    Regions               = join(",", var.regions)
    Dash0RegionalEndpoint = var.dash0_regional_endpoint
  }

  capabilities = ["CAPABILITY_NAMED_IAM", "CAPABILITY_AUTO_EXPAND"]
}

resource "dash0_aws_metric_scrape_config" "this" {
  integration_id                = aws_cloudformation_stack.dash0_integration.outputs["IntegrationId"]
  aws_metric_scrape_config_yaml = var.scrape_config_yaml
}

Referencing the IntegrationId output makes Terraform wait until the stack has registered the integration before it applies the scrape configuration.

For every template parameter, the stack outputs, and how to change regions or rotate the API key without Terraform sending a masked key, see the AWS integration via CloudFormation guide.

Connect Many AWS Accounts with Terraform

Each AWS account needs its own CloudFormation stack, and each stack becomes its own integration in Dash0. To onboard many accounts, put the two resources above in a module and call it once per account, each time with an AWS provider that deploys into that account.

Save the example above as modules/dash0-aws-account/main.tf, declare its variables, and output the integration ID:

hcl
variable "api_key" {
  type      = string
  sensitive = true
}

variable "dataset" {
  type    = string
  default = "default"
}

variable "regions" {
  type = list(string)
}

variable "dash0_regional_endpoint" {
  type = string
}

variable "scrape_config_yaml" {
  type = string
}

output "integration_id" {
  value = aws_cloudformation_stack.dash0_integration.outputs["IntegrationId"]
}

In the root configuration, define one AWS provider alias per account, for example by assuming a deployment role in it, and pass it to the module:

hcl
provider "dash0" {}

provider "aws" {
  alias  = "payments_prod"
  region = "eu-west-1"
  assume_role {
    role_arn = "arn:aws:iam::111111111111:role/terraform-deploy"
  }
}

provider "aws" {
  alias  = "search_prod"
  region = "eu-west-1"
  assume_role {
    role_arn = "arn:aws:iam::222222222222:role/terraform-deploy"
  }
}

module "dash0_payments_prod" {
  source    = "./modules/dash0-aws-account"
  providers = { aws = aws.payments_prod }

  api_key                 = var.dash0_api_key
  regions                 = ["eu-west-1", "us-east-1"]
  dash0_regional_endpoint = "https://ingress.eu-west-1.aws.dash0.com"
  scrape_config_yaml      = file("${path.module}/scrape-config.yaml")
}

module "dash0_search_prod" {
  source    = "./modules/dash0-aws-account"
  providers = { aws = aws.search_prod }

  api_key                 = var.dash0_api_key
  regions                 = ["eu-west-1"]
  dash0_regional_endpoint = "https://ingress.eu-west-1.aws.dash0.com"
  scrape_config_yaml      = file("${path.module}/scrape-config.yaml")
}

Terraform does not accept for_each over provider configurations, so each account needs its own provider block and module call. Generate these blocks from your account list if you have many accounts, or use a wrapper such as Terragrunt that runs the module once per account.

The dash0 provider reads its credentials from DASH0_API_URL and DASH0_AUTH_TOKEN. DASH0_API_URL is the Dash0 API endpoint, not the regional ingress endpoint passed to the stack. See Terraform provider configuration.

Define the Scrape Configuration

The scrape configuration is a Dash0AwsMetricScrapeConfig document. spec.namespaces lists every namespace to scrape and the metrics to collect from it:

yaml
apiVersion: dash0.com/v1alpha1
kind: Dash0AwsMetricScrapeConfig
spec:
  integrationFilter:
    - key: dash0.cloud.tags.environment
      operator: is
      value: production
  namespaces:
    - namespace: AWS/SQS
      metrics:
        - ApproximateAgeOfOldestMessage
        - NumberOfMessagesSent
    - namespace: AWS/Lambda
      metrics:
        - Errors
        - Invocations
      filter:
        - key: dash0.cloud.tags.team
          operator: is
          value: payments
  • spec.namespaces: The complete set of namespaces to scrape. A namespace missing from the list is not scraped, and namespaces: [] stops all scraping for the integration.
  • spec.integrationFilter: Optional conditions for every namespace. This is the Global filters bar on the Metrics tab.
  • filter: Optional conditions for one namespace. This is the namespace's Conditions bar.

Conditions use the keys dash0.cloud.tags.<tag-key> and cloud.region, and the operators is, is_one_of, matches, contains, starts_with, ends_with, and is_set. Dash0 combines all conditions with AND. See Filter AWS Metrics by Resource Tags for how conditions treat untagged resources and which namespaces support tag conditions.

Each integration has exactly one scrape configuration, and every write replaces it as a whole. Manage each integration's configuration from one place only. Two Terraform resources or configurations that manage the same integration overwrite each other on every apply.

Destroying dash0_aws_metric_scrape_config, or deleting the configuration with the CLI, stops scraping but keeps the integration. Destroying the CloudFormation stack removes the integration.

For ownership, import, and the full schema, see the dash0_aws_metric_scrape_config resource documentation.

Dash0 CLI

The Dash0 CLI manages the scrape configuration of an integration that already exists. It cannot create the integration.

The aws-metric-scrape-configs commands are experimental and require the --experimental (or -X) flag. They take the integration ID, which is in the IntegrationId output of the CloudFormation stack. The configuration belongs to the integration's dataset, so the commands take no --dataset flag.

Export a Configuration

bash
dash0 -X aws-metric-scrape-configs get <integration-id> -o yaml > scrape-config.yaml

The exported YAML carries the integration ID in the dash0.com/id label, and update and apply accept it unchanged. Configure an integration in the Dash0 UI first and export it to get a starting point.

Apply a Configuration

bash
# Preview the change as a diff
dash0 -X aws-metric-scrape-configs update -f scrape-config.yaml --dry-run

# Replace the configuration
dash0 -X aws-metric-scrape-configs update -f scrape-config.yaml

# Or apply it with other Dash0 assets, without the experimental flag
dash0 apply -f scrape-config.yaml

Without an integration ID argument, the CLI takes the ID from the document's dash0.com/id label. To apply one file to many integrations, pass the ID as an argument instead:

bash
dash0 -X aws-metric-scrape-configs update <integration-id> -f scrape-config.yaml

Stop Scraping

bash
dash0 -X aws-metric-scrape-configs delete <integration-id>

This clears the configuration and keeps the integration.

For every flag, see the Dash0 CLI command reference.

Further Reading

Last updated: