> ## 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.

<Tabs>
  <Tab title="Terraform">
    ```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>

  <Tab title="Manual setup">
    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.

    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](/use-cases/k8s_waste/visibility) 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.
    </Note>
  </Tab>
</Tabs>
