google-cloud-platformgoogle-cloud-rungoogle-cloud-billing

How to calculate the monthly costs of an individual Cloud Run service?


I have ~30 different microservices deployed to Google Cloud Run. Is there a way (API/dashboard/etc) to determine the 30 day costs of each individual service? Or, even better, a way to view the costs for each container over time (cost/day)?

Attempts: Manually downloading a .csv of the 30 day Billable container instance time graph from the https://console.cloud.google.com/run/detail/myregion/myservice/metrics for each service which is impractical. It is also unclear what sampling/averaging scheme is used to generate that data so the accuracy is ambiguous.


Solution

  • There are two ways for this approach :

    1st Approach uses third-party costing tools like: Harness CCM: https://developer.harness.io/docs/cloud-cost-management/use-cloud-cost-management/ccm-cost-categories/use-ccm-cost-categories/ or Apptio: https://www.apptio.com/products/cloudability/totalcost/

    2nd approach is using Google cloud-native services like Billing Sink to Bigquery, the way forward to achieve this will be as follows:

    Enable the BigQuery export feature > Go to the Billing page and select your billing account > Click on "Export Billing Data to BigQuery." > Follow the prompts to create a new dataset or select an existing one > Once BigQuery export is enabled, billing data will be automatically exported to the specified dataset.

    Here is a sample query that shows the total cost of each Cloud Run service:

    
    SELECT
      labels."run.googleapis.com/service_name" AS service_name,
      SUM(cost) AS total_cost
    FROM
      `billing_project.billing_dataset.gcp_billing_export_v1_XXXXXX_XXXXXX` -- replace with your dataset name and table suffix
    WHERE
      service.id = "Cloud Run"
    GROUP BY
      service.labels."run.googleapis.com/service_name"
    
    

    This query groups the billing data by the Cloud Run service name, then sums the cost for each service

    You can modify the query to get more detailed information, such as the cost per day or per hour, by adding time-based filters or aggregations. For example, to see the daily cost of each Cloud Run service for the last 7 days:

    SELECT
      labels."run.googleapis.com/service_name" AS service_name,
      DATE_TRUNC(date, DAY) AS date,
      SUM(cost) AS daily_cost
    FROM
      `billing_project.billing_dataset.gcp_billing_export_v1_XXXXXX_XXXXXX` -- replace with your dataset name and table suffix
    WHERE
      service.id = "Cloud Run"
      AND date >= DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY)
    GROUP BY
      service.labels."run.googleapis.com/service_name",
      DATE_TRUNC(date, DAY)
    
    

    This query groups the billing data by service name and day, then sums the cost for each service and day.

    There may be some delay between when the billing data is generated and when it is exported to BigQuery, so the results may not be real-time, for more context on this you can also follow docs: https://cloud.google.com/billing/docs/how-to/export-data-bigquery