> ## Documentation Index
> Fetch the complete documentation index at: https://docs.costory.io/llms.txt
> Use this file to discover all available pages before exploring further.

# GCP Billing Setup

> Connect GCP BigQuery billing export with detailed usage costs to Costory via Terraform or manual setup.

> [tab] Terraform
>
> To install and authenticate the provider, see [Terraform setup](https://docs.costory.io/setup/terraform.md).
>
> ```hcl theme={null}
> 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]
> }
> ```

> [tab] Manual setup
>
> In Costory, go to **Billing > Import new billing datasource > GCP** and follow the stepper. First prepare your GCP account:
>
> 1. In the GCP Billing console, enable **Detailed Usage Cost** export to BigQuery. Use **EU** as the dataset location (or create an EU replica).
>
> 2. 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](https://docs.costory.io/use-cases/k8s_waste/visibility.md) for details.
>
> 3. 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.
>
> 4. 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**.
>
> 5. In Costory, go to **Billing > Import new billing datasource > GCP** and provide the table path.
>
> > [!NOTE]
> >
> > GCP backfills only the previous and the current month by default. The billing export shows BigQuery compute as one **Analysis** line, plus a lumped reservation line if you buy slots. To attribute both to the user who ran each query, connect [BigQuery jobs](https://docs.costory.io/setup/usage-metrics/bigquery-jobs.md) (beta).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.