Have you ever had that moment when you realize your cloud costs have unexpectedly increased? Keeping track of your Google Cloud Platform (GCP) expenses usually requires manual effort, as you need to visit the console regularly. But what if you could receive daily updates on your costs automatically? Enter Slack notifications!
Imagine receiving a daily report about your GCP billing directly in your favorite messaging app. It’s like having a personal assistant to help you manage your cloud expenses. This automation uses BigQuery, Cloud Run Functions, and Cloud Scheduler to keep you informed about your costs. Let’s break it down step by step.
Tools and Technologies Used
Before we start, let’s overview the tools you’ll need:
-
Google Cloud Platform (GCP): Our main platform.
-
BigQuery: The database that stores our billing data.
-
Cloud Functions: To process the billing data and prepare it for notifications.
-
Cloud Scheduler: To trigger the Cloud Function daily.
Setting Up BigQuery Export
First, we need to export our billing data to BigQuery. Here’s how:
-
Enable Billing Export to BigQuery: Go to your GCP console, navigate to the billing section, and enable the export option under Billing export. Choose the BigQuery dataset where you want the billing data to be stored. You can just use the Standard usage cost for the daily cost.
-
Configure the Export: Set up the permissions and configurations to ensure data flows smoothly from your billing account to BigQuery.
Pricing Export
- Once you activate the pricing export feature to BigQuery, it might take up to 48 hours for your Google Cloud pricing data to start appearing.
- After activation, the pricing data for your Cloud Billing account is transferred to BigQuery once a day.
Creating a Cloud Run Function
Next, let’s create a Cloud Run Function that will process the billing data:
-
Cloud Run Functions: Think of Cloud Run Functions as small pieces of code that run in the cloud, triggered by specific events.
-
Create the Function: In the GCP console, go to Cloud Run Functions and create a new function. Choose the runtime environment that suits you (Node.js, Python, etc.).
-
Write the Code: Your function will query BigQuery for the latest billing data, format the results, and send them to Slack. Here’s a simple code example to get you started:
This is primarily designed to track the total cost of your Google Cloud Platform (GCP) usage for the current month and send updates to a Slack channel. It uses Google BigQuery to query cost data and sends notifications through a Slack webhook. The script checks the current cost, updates it if there’s new data, and posts a message to Slack, ensuring you stay informed about your cloud expenses.
Scheduling the Cloud Function with Cloud Scheduler
To make sure our Cloud Function runs daily, we’ll use Cloud Scheduler:
-
Cloud Scheduler: Cloud Scheduler is a fully managed cron service provided by Google Cloud Platform (GCP). It allows you to schedule and automate tasks, known as cron jobs, to run at specified times or regular intervals. These tasks can include batch jobs, big data jobs, cloud infrastructure operations, and more.
-
Set Up the Scheduler: In the GCP console, navigate to Cloud Scheduler and create a new job. Set the frequency and point it to the Cloud Function you created. See the example configuration below:
-
Configuration: Ensure all the permissions and settings are correctly configured to allow the scheduler to trigger the function.
Testing and Troubleshooting
Before you relax, make sure everything works:
-
Test the Setup: Open the URL of your Cloud Run Function and check Slack for the notification.
-
Troubleshoot: If something goes wrong, check the logs in Cloud Functions and Cloud Scheduler for any errors.
Benefits and Use Cases
By setting up this automation, you’ll enjoy several benefits:
-
Stay Informed: Daily updates mean no more surprises at the end of the month.
-
Cost Management: Helps in better budget management and cost forecasting.
-
Ease of Access: Get all your billing updates in Slack, where you and your team already communicate.
Conclusion
With this automation, you’re well on your way to managing your GCP expenses. Daily Slack notifications will keep you informed and in control, all while simplifying your workflow. So, set it up, and let automation handle the rest.
Reference:
https://cloud.google.com/billing/docs/how-to/export-data-bigquery-tables