Google cloud billing API, grab cost for current month

2/10/2020

I'm currently building an admin dashboard, using flask, for an online service that's running on GKE.

I want to show how large of a bill the service has racked up since the beginning of the month. I've looked into the Google Cloud billing API, but it's a bloody jungle and I can't seem to find any way to retrieve the data I want.

I assume it should be possible but I can't figure out how. Can anybody more experienced with Google Cloud APIs help me, preferably with python code-snippets?

I've looked at the answers in this post but couldn't really figure out how to use them.

-- limeeattack
google-api
google-cloud-platform
google-kubernetes-engine

1 Answer

2/10/2020

The official documentation:

Get started with the Cloud Billing Budget API

Cloud Billing Budget API Setup

Using the Cloud Billing Budget API

Cloud Billing Budget Client Libraries

Use the Cloud Billing Budget API to:

Create a separate budget for each of your Google Cloud projects so you know which areas of your Google Cloud environment are spending more than expected.

Bulk update all of your budgets after quarterly financial planning.

Integrate with your company's deployment manager to add the creation of budgets to your cloud provisioning workflow.

  1. Create a project
  2. Enable billing
  3. Enable the API (Cloud Budget API, Cloud Billing Budget API)
  4. Create a Budget, on the Budget & Alerts, Create Budget, Scope, Amount ( Last month’s spend), Actions
  5. Set up authentication (create a service account, grant the service account the role Billing Account Administrator, download the key.json, set the env variable export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
  6. Use the client library Client for Cloud Billing Budget API

    from google.cloud import billing_budgets_v1beta1
    client = billing_budgets_v1beta1.BudgetServiceClient()
    
    #get all the budgets
    parent = client.billing_account_path([BILLING_ACCOUNT])
    for element in client.list_budgets(parent):
       print(element)
       pass
    
    #for a specific budget
    name = client.budget_path('[BILLING_ACCOUNT]', '[BUDGET]')
    response = client.get_budget(name)
    
    #The output should be in the form 
    display_name: "billing-budget"
    budget_filter {
    projects: "projects/"
    credit_types_treatment: 
    }
    amount {
    last_period_amount {
    }
    }
    threshold_rules {
    threshold_percent: 0.5
    spend_basis: CURRENT_SPEND
    }
    threshold_rules {
    threshold_percent: 0.9
    spend_basis: CURRENT_SPEND
    }
    threshold_rules {
    threshold_percent: 1.0
    spend_basis: CURRENT_SPEND
    }
    all_updates_rule {
    }
    etag: "" ```

EDIT:

I checked the gcloud alpha billing command and I can see only the options :

    1. accounts(describe, get-iam-policy, list, projects(describe, link, list, unlink))

    2. budgets(create, delete, describe, list, update) 

    3. projects(describe, link, list, unlink). These are the API that you can call. 

There is no API for getting current spending to my understanding. You can export billing to BigQuery or to a file(this is deprecated) and do your analysis.

-- marian.vladoi
Source: StackOverflow