- Terraform
- Manual setup
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = ">= 5.0"
}
costory = {
source = "costory-io/costory"
version = ">= 0.1.0"
}
}
}
variable "costory_api_token" {
type = string
description = "Costory API token."
sensitive = true
}
provider "costory" {
token = var.costory_api_token
}
data "costory_service_account" "current" {}
locals {
bigquery_roles = toset([
"roles/bigquery.metadataViewer",
"roles/bigquery.dataViewer",
])
# Set the BigQuery project, dataset, and table IDs where your detailed billing data is exported.
bigquery_project_id = "my-project"
bigquery_dataset_id = "billing_export"
bigquery_table_id = "gcp_billing_export_v1_0123"
}
resource "google_bigquery_dataset_iam_member" "costory_access" {
for_each = local.bigquery_roles
project = local.bigquery_project_id
dataset_id = local.bigquery_dataset_id
role = each.key
member = "serviceAccount:${data.costory_service_account.current.service_account}"
}
resource "costory_billing_datasource_gcp" "main" {
name = "GCP Billing Export"
bq_uri = "${local.bigquery_project_id}.${local.bigquery_dataset_id}.${local.bigquery_table_id}"
is_detailed_billing = true
depends_on = [google_bigquery_dataset_iam_member.costory_access]
}
Open Costory and go to Billing > Import new billing datasource > GCP. The stepper guides you through each field. The steps below explain how to prepare your GCP account.
- In the GCP Billing console, enable Detailed Usage Cost export to BigQuery. Use EU as the dataset location (or create an EU replica).
- If you run GKE (autopilot or standard), enable cost allocation in the GKE cluster settings for pod-level Kubernetes cost visibility. See Kubernetes cost visibility for details.
- Note the full table path (e.g.,
gcp_billing_export_resource_v1_<billing_account_id>). GCP automatically includes all resource labels in the billing export, so no extra tag activation is needed. - Share the BigQuery dataset with the Costory service account using BigQuery Data Viewer and BigQuery Metadata Viewer roles. You can find the service account in Costory under Billing > Import new billing datasource > GCP.
- In Costory, go to Billing > Import new billing datasource > GCP and provide the table path.
GCP backfills only the previous and the current month by default.
