How to use Google Cloud Billing APIs to calculate usage costs? - google-cloud-platform

How can I use the Google Cloud Billing APIs to retrieve my total expected bill / accumulated bill so far?
Is there an API call that simply returns the cost so far for a certain billing account?
Or alternatively, a combination of calls - one for retrieving the usage data and the other with the prices for the various Google Cloud services - to calculate the costs myself?
Thank you

Have you try Google Cloud Billing Report ?
https://cloudplatform.googleblog.com/2018/03/understand-your-spending-at-a-glance-with-Google-Cloud-Billing-reports-beta.html

Yes, there is an API to accomplish what you want.
You can control costs, track expenses and create alerts to notify billing administrators following this guide
Based on the comment below Alexandre answer, You can get programmatic budget notifications using Google Cloud Billing API setting up budget notifications as described in this tutorial.
I suggest you to start trying the quickstart, which performs simpler API calls.

Related

Identify connected sheets usage in GCP billing

We're using connected sheets to trigger request on BigQuery.
We're trying to identify the cost of this usage on the billing account but there is only a BQ analysis service. Nothing identified in SKU as "connected sheets" for example.
Is there anyone who found a way to easily identify usage of connected sheets request on GCP ?
Thanks in advance
There is an option to see queries run by a service account or a user. You can filter from Cloud Logging by email, and identify the totalBytesBilled and totalBytesProcessed to calculate the cost. In this case, Connected Sheets will be using either a service account or a user to run the queries, you can filter based on that.

Monitoring GCP spend without access to Billing API

I run a small research group at a large university that manages hundreds of GCP accounts. The university acts as the Billing Administrator, and my research group was assigned a GCP "project" for all of our work. However, for privacy reasons, they cannot give me access to the Billing API because this would allow me to see the billing details for other labs.
Because we have trainees in our lab who WILL make mistakes, I would like to setup an automated system that monitors our current GCP bill, and (1) sends notifications or (2) terminates all VMs, when that bill reaches certain predefined limits. For example, if our monthly budget is $10k, then I would like to receive a notification at $5k, another notification at $10k, and I would like to terminate all VMs at $15k.
My problem is that in order to implement a system like this, I need access to the Billing API. I have already contacted my system administrator and they have said that this is impossible. Instead, they proposed that I write a script that lists all VMs and uses the Cost Calculator to estimate my monthly GCP bill.
However, this seems a little circuitous. When I am using the Google Cloud Console, I can see the total and forecasted costs for my project, so it seems that I should be able to access this information programmatically. However, I cannot find any information on how to do this, since all solutions require me to activate the Billing API. Any ideas?
There is no API to fetch the data you see in the Google Cloud Console. You will need to export the billing data and then process each row of data to generate reports.
There are two options that I can think of:
Option 1) Ask the admin to set up billing data export to BigQuery. Grant you permission to query the billing tables. You can then query BiGQuery to generate your own cost reports.
Set up Cloud Billing data export to BigQuery
Option 2) Create a separate billing account for your project and grant you permission. A GCP ORG can have multiple Billing Accounts tied to the same Payments Account. This option supports creating budget alerts.

Alternative to Google service accounts

My company works with IOT devices, and we have a product where each device should have a service account.
This scenario it's impossible to us right now because, follow that doc (https://cloud.google.com/iam/docs/understanding-service-accounts) studying more about it, was discovered GCP had a limit quota of 100 service accounts. Makes us impossible to work with 1 service account by device.
At that moment, in GCP, have another option than service accounts?
Are there a way to increase the amount of service accounts?
I would suggest to check this article that describes the authentication strategies you can use to work in GCP, in particular Google Cloud APIs.
If you have decided that you would rather have a service account for each of your IOT devices, instead if using another option such as the OAuth 2.0 client then you can request a quota increase from the default limit of 100.
The quota increase request is subject to evaluation, so it's best to add a clear note on why you need more than 100 SAs.
Maybe authenticating as a en user could be a better option as whenever you need to increase the number of devices you won't need to wait for any type of approval. However it's not possible to know for sure if this option is best, as your application flow is not clear with the details you have added in the question so far. As mentioned before, you could take a look to the documentation and select the best option for you use case.

How to get billing information programmatically from Google Cloud Platform? Is there any API's there.?

I want write utility which fetches billing information for my project but I am not able to find any specific API from GCP to do the same. I tried a couple of APIs like getBillingInfo but these APIs just give information about billing account, not the pricing. They have the mechanism to export billing data to file but I want to do it programmatically. Is there any API to do the same?
One possible way that I am aware of is to export the Cloud billing to BigQuery. The process (document) to export Cloud Billing to BigQuery can be found here: https://cloud.google.com/billing/docs/how-to/export-data-bigquery
Once the export is done, the billing information including the price and services is available in almost realtime on BigQuery table. Once it's available on BigQuery there are numerous ways of extracting the information. A good solution would be to have Data Studio on Google Cloud to send you a periodic report on your billing information.

How to access GCP Reseller Billing

I would like to create a GCP cost management platform for resellers/MSP. The platform will pule cost data per each of the reseller's end customers.
Where Can I find information on:
How can I access the reseller's data?
What is the data structure? Does a reseller have one dataset for all of his customers, or one dataset per customer?
What are the needed credentials that the reseller needs to provide in order to fetch his customers data, and what permissions need to be provided?
I created a Video Lab on Google Cloud Billing. The first step is to understand how billing works in Google Cloud. Then understand how Identity and Access Management works so that you know what roles you need for credentials to access billing APIs.
Google Cloud Billing
Review the following documentation links for technical API details:
Get Started with the Cloud Billing API
Cloud Billing API
APIs & Reference
To access reseller information you will need to become a Google Partner first. Then request documentation.