Stay organized with collections
Save and categorize content based on your preferences.
This document explains how to send budget notifications to Slack.
Budgets
are typically configured to send email notifications. However, email isn't
always the best way to stay up to date on your cloud costs, particularly if
your budget is critical and time sensitive. With programmatic notifications,
you can forward your budget messages to other mediums, such as
Slack.
Before you begin
Before you begin, you must complete the following tasks:
The first step is to create your Slack workspace and the bot user tokens
that are used to call the Slack API. API tokens can be managed at
https://api.slack.com/apps.
For detailed instructions, see
Bot Users
on the Slack site.
Set up a Cloud Run function
Complete the steps in
Create a Cloud Run function.
Ensure that the Trigger type is set to the same Pub/Sub
topic that your budget will use.
Copy the following code into your Cloud Run function to post
budget notifications to a Slack chat channel using the Slack API:
Node.js
constslack=require('slack');// TODO(developer) replace these with your own valuesconstBOT_ACCESS_TOKEN=process.env.BOT_ACCESS_TOKEN||'xxxx-111111111111-abcdefghidklmnopq';constCHANNEL=process.env.SLACK_CHANNEL||'general';exports.notifySlack=asyncpubsubEvent=>{constpubsubAttrs=pubsubEvent.attributes;constpubsubData=Buffer.from(pubsubEvent.data,'base64').toString();constbudgetNotificationText=`${JSON.stringify(pubsubAttrs)}, ${pubsubData}`;awaitslack.chat.postMessage({token:BOT_ACCESS_TOKEN,channel:CHANNEL,text:budgetNotificationText,});return'Slack notification sent successfully';};
Python
importbase64importjsonimportosimportslackfromslack.errorsimportSlackApiError# See https://api.slack.com/docs/token-types#bot for more infoBOT_ACCESS_TOKEN="xxxx-111111111111-abcdefghidklmnopq"CHANNEL="C0XXXXXX"slack_client=slack.WebClient(token=BOT_ACCESS_TOKEN)defnotify_slack(data,context):pubsub_message=data# For more information, see# https://cloud.google.com/billing/docs/how-to/budgets-programmatic-notifications#notification_formattry:notification_attr=json.dumps(pubsub_message["attributes"])exceptKeyError:notification_attr="No attributes passed in"try:notification_data=base64.b64decode(data["data"]).decode("utf-8")exceptKeyError:notification_data="No data passed in"# This is just a quick dump of the budget data (or an empty string)# You can modify and format the message to meet your needsbudget_notification_text=f"{notification_attr}, {notification_data}"try:slack_client.api_call("chat.postMessage",json={"channel":CHANNEL,"text":budget_notification_text},)exceptSlackApiError:print("Error posting to Slack")
Ensure the following Slack API postMessage parameters are set correctly:
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-21 UTC."],[[["\u003cp\u003eThis guide outlines how to configure budget notifications to be sent to a Slack channel instead of just email.\u003c/p\u003e\n"],["\u003cp\u003eBefore setting up Slack notifications, you must enable the Cloud Billing API, create a budget, and set up programmatic budget notifications.\u003c/p\u003e\n"],["\u003cp\u003eSetting up Slack requires creating a workspace and managing bot user tokens through the Slack API interface.\u003c/p\u003e\n"],["\u003cp\u003eA Cloud Run function is created to receive Pub/Sub messages and then use the Slack API to post notifications to a designated Slack channel, with example code provided in Node.js and Python.\u003c/p\u003e\n"],["\u003cp\u003eAfter configuring, you can test the function to ensure messages are successfully delivered to Slack, with further options provided to control resource usage and billing through notifications.\u003c/p\u003e\n"]]],[],null,[]]