Product5 min read

ByCostGraph Team

Announcing the CostGraph Terraform provider

Manage accounts, register Kubernetes clusters, and define cost attribution rules through Terraform.

Ask about this postClaudeChatGPTPerplexity

CostGraph connects your spend across multiple providers. With over 97 integrations, clickops was never going to scale, which is why, from day one, we have made the APIs to build upon available.

In September, we released the CostGraph CLI. This was the first step in bringing FinOps as code to CostGraph.

Today, we're excited to announce the CostGraph Terraform provider. You can now create tenants and connect provider accounts through Terraform, alongside the infrastructure those accounts contain.

For teams that rely heavily on infrastructure as code, this brings CostGraph setup into the same pull requests, reviews, and deployment pipelines you already use.

What does this version include?

Version 0.4.0 includes eight resources.

Create a tenant and connect AWS

Add the provider to your configuration. It reads your API key from COSTGRAPH_API_KEY, so you can supply it through your CI secret store.

terraform {
  required_providers {
    costgraph = {
      source  = "baselinehq/costgraph"
      version = "~> 0.4.0"
    }
  }
}

provider "costgraph" {}

For tenant and cloud-account management, use an organization owner's API key with the explicit organization:manage permission. Existing keys do not gain that permission automatically.

The following configuration creates a Production tenant and connects an existing AWS billing export to it.

resource "costgraph_tenant" "production" {
  name = "Production"
}

resource "costgraph_aws_integration" "production" {
  tenant_id                     = costgraph_tenant.production.id
  billing_account_id            = "123456789012"
  display_name                  = "Production AWS"
  region                        = "us-east-2"
  bucket                        = "example-focus-export"
  prefix                        = "billing/FOCUS/"
  role_arn                      = "arn:aws:iam::123456789012:role/CostGraphRead"
  activate_cost_allocation_tags = false
}

The export, bucket, and IAM role must already exist. Terraform can manage those through the AWS provider. The CostGraph provider connects them to the tenant and verifies access before saving the connection.

You can also import an existing CostGraph tenant or connection instead of recreating it.

Configure your AI gateway

With agentgateway and the CostGraph operator running in your registered cluster, add the connection. This example uses the inference namespace.

resource "costgraph_ai_gateway_integration" "inference" {
  tenant_id           = costgraph_tenant.production.id
  provider_name       = "agentgateway"
  provider_account_id = "production-inference"
  display_name        = "Production inference"
  cluster_id          = costgraph_kubernetes_cluster.production.id
  scrape_namespace    = "inference"

  tenant_map = {
    acme = "Acme"
  }
}

tenant_map labels usage from customer acme as Acme in CostGraph.

Example data.

For agentgateway, map a request header to the customer metric label, then send your authenticated customer's ID on each request:

x-costgraph-customer-id: acme

The operator collects the labelled metrics. You can derive the label from a JWT claim instead of a header.

Assign costs with virtual tags

You can also keep your cost attribution rules in version control. Virtual tags match your cost data without changing the tags on your cloud resources.

For this standalone example, use an API key belonging to the tenant you want to tag, with virtual-tags:write. The rule assigns AWS costs to the platform team.

variable "tenant_id" {
  type = string
}

resource "costgraph_virtual_tag" "team" {
  tenant_id = var.tenant_id
  key       = "team"

  rules = [
    {
      rule_key   = "aws"
      scope      = "billing"
      expression = "bill.provider == \"aws\""
      value      = "platform"
    }
  ]
}

Rules run in order, and the first active match sets the value. Terraform manages the whole key and its rule order. Import an existing key before managing it.

Register a Kubernetes cluster

Register an existing cluster with CostGraph, then reference its name from a Helm release to install the operator.

resource "costgraph_kubernetes_cluster" "production" {
  tenant_id = costgraph_tenant.production.id
  name      = "production-eks"
}

Use an organization owner's key with organization:manage for registration. The Helm example reads the collector credential from an existing Kubernetes Secret.

Use your self-hosted deployment

The provider also accepts your own CostGraph API endpoint.

provider "costgraph" {
  api_url = "https://api.costgraph.example.com"
}

You can set COSTGRAPH_API_URL instead. Supply an API key from that deployment and use its API base URL, without /api/v1.

Bring CostGraph into your Terraform workflow

This complements our Terraform GitHub Action and Infracost-compatible pricing API. Those help you estimate infrastructure costs before deploying a change. The provider lets you manage the CostGraph configuration that brings your accounts and usage into view.

Over time, we want more of what you configure in CostGraph to be manageable through Terraform. This release covers account setup, cluster registration, and virtual tags.

The provider is available on the Terraform Registry, with examples and import instructions for each resource. The source is on GitHub. If there's a part of your CostGraph setup you'd like to manage next, let us know.

Put your bill to work

Connect a provider, watch your spend attribute itself, ask GraphAI where the money’s going.