Lorenzo Sfienti

Use AWS Cost Explorer with Terraform

In this article, I show you in which mode in my company we use AWS Cost Explorer with Terraform.

As mentioned in a previous article at Il Post we use Terraform to manage all AWS Resources.

Similarly, also for AWS Cost Explorer, we wrote the alerts and dashboards in Terraform language.

The first step was to tag all resources with a specific tag that represents the business domain. To do that inside the provider block for each terraform configuration we add a general tag like this:

provider "aws" {
  region  = "***"
  profile = "***"
  default_tags {
    tags = {
      "ilpost:created-by"  = "***"
      "ilpost:project-url" = "***"
    }
  }
}

Now we have all resources tagged with two specific tags. For AWS Cost Explorer we use the tag ilpost:project-url

Create an Allocation Tag for Cost Explorer.

resource "aws_ce_cost_allocation_tag" "example" {
  tag_key = "ilpost:project-url"
  status  = "Active"
}

This will allow you to group resources by tag inside the Cost Explorer and show the cost for each tag.