CostLensGCP Accounts

GCP Accounts

Connect a Google Cloud project to CostLens in one click — sign in with Google and CostLens provisions a dedicated service account and grants itself read access automatically.

Updated September 20269 min read

CostLens reads your Google Cloud cost data from your BigQuery billing export and your recommendations from the Recommender API, using a dedicated service account with read-only roles. The guided Add Account wizard connects with a Google sign-in — CostLens provisions the service account, binds the roles, and (where your org policy allows it) mints its own key automatically. No script to run, no key to paste.

Prerequisite: BigQuery billing export

CostLens reads cost data from the standard BigQuery billing export. If you haven't enabled it, go to Billing → Billing export → BigQuery export and configure Detailed or Standard usage cost export to a dataset. It can take a few hours for the first data to land.

How it works

StepWhat happens
1. Choose access levelRead-Only (monitoring, reporting, recommendations) or Read/Write (adds Auto-Fix for idle-VM stop/start).
2. Review exactly what's grantedCostLens shows the precise IAM roles it's about to request before anything happens.
3. AuthorizeOpens Google sign-in in the same tab.
4. CostLens provisions access automaticallyAfter you sign in and allow access, CostLens creates a dedicated costlens-cost-reader service account in your project, grants it the read-only roles, and — if your organization allows service-account keys — generates one itself. If key creation is blocked by org policy, CostLens automatically falls back to keyless: it grants its own service account access instead, with no key created at all.
5. First sync startsBilling sync from the BigQuery export, optimization checks (Recommender API), and AI explanations run automatically.

Connect a new GCP project

Step 1 — Add Account → GCP → choose access level

From the Accounts page, click Add Account, choose GCP, then choose Read-Only or Read/Write:

Access levelGrantsEnables
Read-Onlyroles/bigquery.dataViewer + roles/bigquery.jobUser (billing-export read), roles/billing.viewer, roles/recommender.viewer, roles/cloudasset.viewer, roles/compute.viewer + roles/monitoring.viewer, roles/logging.privateLogViewerBilling visibility, inventory, anomaly detection, recommendations & savings estimates
Read/WriteEverything in Read-Only, plus compute.instances.stop/start and the specific disk-snapshot actions CostLens's reversible remediations use — never a broad Editor/Owner roleEverything in Read-Only, plus Apply Fix and Auto-Fix

Step 2 — Authorize (Google sign-in)

Click Authorize. This redirects your browser to Google's own sign-in/consent page — sign in as a user with IAM Admin rights on the project(s) you want to connect (able to create service accounts and grant roles), and allow access.

Sign in with the right account

CostLens can only provision access in projects the signed-in user can already administer. If a project you expected isn't detected, you likely signed in as the wrong user or lack IAM Admin rights on it.

Step 3 — CostLens provisions access automatically

Google redirects you back into CostLens. From here everything is automatic — CostLens uses your one-time sign-in to:

  1. List every project and billing account visible to your signed-in identity, and auto-detect the BigQuery billing-export dataset if one exists.
  2. Create (or reuse, if reconnecting) a dedicated costlens-cost-reader service account in the project.
  3. Bind it the read-only roles for your chosen access level.
  4. Attempt to generate a service-account key for it automatically.
  5. If your organization's policy blocks service-account key creation, automatically fall back to keyless instead — granting CostLens's own dedicated service account the same roles, so no key exists anywhere.

As soon as provisioning succeeds, the project connects and its first sync starts. IAM changes can take a few seconds to propagate — the wizard keeps checking automatically during that window.

Multiple projects found

If your account has access to more than one project, CostLens connects the first one automatically and lists the rest as separate accounts you can add the same way, or via Connect an organization below.

Connect a whole org or folder

To grant CostLens access to every project under a GCP folder or organization in one step instead of one at a time, use the Connect an organization flow from the Accounts page: pick an already-connected GCP account whose service account is bound at the org/folder level, optionally run the generated grant script once, then Discover & connect — CostLens enumerates and connects every project it can now see.

Reconnecting a removed project

If a project was previously connected and later removed, reconnecting it (Authorize again, sign in the same way) reuses the existing costlens-cost-reader service account rather than creating a duplicate.

If you connect a project that turns out to already be active in your CostLens organization, you'll see "This account already exists — it's already connected. Nothing was added again" on the success screen.

Alternative: run the setup script yourself

If Authorize isn't available in your environment, or your organization prefers to review and run IAM changes itself, use the manual path instead. It offers the same two outcomes Authorize would have tried automatically — a dedicated service account with a key, or a keyless grant to CostLens's own service account — just run by hand.

If CostLens's own service account is configured for keyless access, the manual step offers a "Use keyless (recommended)" toggle. Choosing it generates a script that grants CostLens's own service-account email the required roles in your project — no key is ever created, downloaded, or pasted:

bash
#!/usr/bin/env bash
# CostLens — GCP keyless grant (no service-account key created)
set -euo pipefail
 
PROJECT_ID="<PROJECT_ID>"
COSTLENS_SA="<shown in the wizard>"
 
gcloud projects add-iam-policy-binding "$PROJECT_ID" \
  --member="serviceAccount:${COSTLENS_SA}" --role="roles/bigquery.dataViewer"
# ...plus the other read-only roles listed in Step 1 above, all bound to $COSTLENS_SA

The wizard generates the exact script (with your project/billing/dataset IDs filled in) and phones home automatically once it's run — no need to return to CostLens or paste anything back.

Key-based (dedicated service account)

If you'd rather create your own dedicated service account and key:

bash
#!/usr/bin/env bash
# CostLens — GCP connection setup (BigQuery billing-export viewer, least privilege)
set -euo pipefail
 
PROJECT_ID="<PROJECT_ID>"
BILLING_ACCOUNT_ID="<BILLING_ACCOUNT_ID>"
BQ_DATASET="<BQ_DATASET>"   # dataset holding the BigQuery billing export
SA_NAME="costlens-cost-reader"
 
gcloud config set project "$PROJECT_ID"
 
# 1. Create a dedicated service account
gcloud iam service-accounts create "$SA_NAME" --display-name "CostLens cost reader"
SA_EMAIL="${SA_NAME}@${PROJECT_ID}.iam.gserviceaccount.com"
 
# 2. Grant read-only roles
#    - BigQuery Data Viewer on the billing-export dataset
bq add-iam-policy-binding \
  --member="serviceAccount:${SA_EMAIL}" \
  --role="roles/bigquery.dataViewer" \
  "${PROJECT_ID}:${BQ_DATASET}"
#    - Run queries + view billing + view recommendations
gcloud projects add-iam-policy-binding "$PROJECT_ID" \
  --member="serviceAccount:${SA_EMAIL}" --role="roles/bigquery.jobUser"
gcloud billing accounts add-iam-policy-binding "$BILLING_ACCOUNT_ID" \
  --member="serviceAccount:${SA_EMAIL}" --role="roles/billing.viewer"
gcloud projects add-iam-policy-binding "$PROJECT_ID" \
  --member="serviceAccount:${SA_EMAIL}" --role="roles/recommender.viewer"
#    - View the resource inventory (Cloud Asset Inventory) — for the Cloud Inventory page
gcloud projects add-iam-policy-binding "$PROJECT_ID" \
  --member="serviceAccount:${SA_EMAIL}" --role="roles/cloudasset.viewer"
#    - Read compute resources + monitoring metrics — required for the compute & GKE
#      optimization checks (idle / right-sizing / Spot node pools, MIG autoscaling)
gcloud projects add-iam-policy-binding "$PROJECT_ID" \
  --member="serviceAccount:${SA_EMAIL}" --role="roles/compute.viewer"
gcloud projects add-iam-policy-binding "$PROJECT_ID" \
  --member="serviceAccount:${SA_EMAIL}" --role="roles/monitoring.viewer"
 
# 3. Create a service-account key — paste the JSON into CostLens
gcloud iam service-accounts keys create costlens-key.json --iam-account="$SA_EMAIL"

Paste the contents of costlens-key.json (plus Project ID, Billing account ID, and dataset) into the CostLens manual-entry step and click Connect account.

Avoid long-lived keys

Prefer keyless whenever it's offered — it's simpler (no key to secure or rotate) and is exactly what Authorize tries automatically first, before falling back to a key.

Recommendations

GCP recommendations come from the Recommender API (read-only), surfaced as individual checks:

  • Idle VM Instance, VM Right-sizing, Idle Cloud SQL, Unattached Persistent Disk, Idle Static IP, and Committed Use Discounts.
  • A CostLens custom check for Orphaned Disk Snapshots covers a Recommender gap.

The roles/recommender.viewer, roles/compute.viewer, and roles/monitoring.viewer scopes granted at Read-Only cover all of these.

Kubernetes (GKE) cost & optimization

The roles granted at Read-Only already give CostLens everything it needs to discover your GKE clusters and recommend node-pool savings (idle / right-sizing / Spot). One capability needs a one-time switch flipped on the cluster:

Node-pool optimization — nothing extra to enable

The compute.viewer + monitoring.viewer roles let CostLens read each node pool's Managed Instance Group and its Cloud Monitoring CPU. GKE node-pool recommendations appear on the Recommendations page automatically — no cluster change required. See Kubernetes Optimization.

Per-cluster, per-namespace & per-workload cost — enable GKE cost allocation

On each cluster, turn on GKE cost allocation (Kubernetes Engine → your cluster → Enable GKE cost allocation). Google then writes the goog-k8s-cluster-name, goog-k8s-namespace, and goog-k8s-controller-name labels into your BigQuery billing export, which CostLens already reads. Cost appears in the Kubernetes panel on the Billing page within ~24 hours. See Kubernetes Costs.

No agent, no cluster access

CostLens reads Kubernetes cost and utilisation entirely from Cloud Monitoring, the Compute API, and your BigQuery billing export — there is nothing to install in the cluster and no Kubernetes API access is required.

Applying fixes (optional)

Choosing Read/Write during onboarding already grants what Auto-Fix needs. Scanning itself is always read-only regardless of access level. Today's GCP remediations:

ActionReversible?
Stop an idle VM instanceYes — Auto-Fix can apply this automatically; a 24-hour manual rollback window follows
Delete an unattached disk / orphaned snapshot / idle static IPNo — always requires manual approval with typed confirmation, on any cloud

If you connected with Read-Only and want Auto-Fix later, reconnect with Read/Write — Authorize again and choose Read/Write; CostLens adds the missing roles to the existing service account rather than creating a new one.

Enabling automatic Auto-Fix with your own service account (least privilege)

If you used the manual/own-service-account path, create a custom role with only VM power control:

bash
gcloud iam roles create costlens_autofix --project="<PROJECT_ID>" \
  --title="CostLens Auto-Fix (VM power)" \
  --permissions=compute.instances.get,compute.instances.start,compute.instances.stop \
  --stage=GA
gcloud projects add-iam-policy-binding "<PROJECT_ID>" \
  --member="serviceAccount:costlens-cost-reader@<PROJECT_ID>.iam.gserviceaccount.com" \
  --role="projects/<PROJECT_ID>/roles/costlens_autofix"

Common errors and fixes

ErrorFix
Google sign-in didn't completeAuthorize again — you may have closed the Google window or clicked Cancel. Make sure you sign in as a user with IAM Admin rights on the project.
AUTH_FAILED (manual path)The service-account key JSON is malformed or the key was revoked. Generate a new key and update it via Edit Credentials.
INSUFFICIENT_PERMISSIONSA required role is missing. Confirm BigQuery Data Viewer on the dataset plus jobUser / billing.viewer / recommender.viewer / cloudasset.viewer / compute.viewer / monitoring.viewer — Authorize grants all of these automatically; the manual path needs each bound explicitly.
No GKE node-pool recommendationsConfirm compute.viewer + monitoring.viewer are granted — without them CostLens can't read node CPU.
No per-namespace GKE costEnable GKE cost allocation on the cluster; allow ~24h for the labels to appear in the billing export.
EXPORT_NOT_CONFIGURED (Not found: Table)The BigQuery billing export isn't set up, or the dataset/table name is wrong. Enable the export under Billing → Billing export and confirm the dataset.
No cost data after connectingThe billing export can lag a few hours after first enablement — CostLens picks it up on the next sync.
CostLens
Previous
Azure Accounts
CostLens
Next
Cost & Usage Reports (CUR)