Example reference architecture

This document explains how to listen to your programmatic budget notifications by subscribing to your Pub/Sub topic with Cloud Run function triggers. Without a subscriber, Pub/Sub drops published messages and you can't retrieve them later. In this document, you'll learn how to complete the following tasks:
- Create a Cloud Run function that listens to notifications.
- View Cloud Run function events.
- Test a Cloud Run function.
Before you begin
Before you begin, you must complete the following tasks:
Create a Cloud Run function
To create a Cloud Run function, complete the following steps:
In the Google Cloud console, go to the Cloud Run functions page.
Click CREATE FUNCTION and enter a Function name that's meaningful to your budget notification.
Select the region where your Cloud Run functions will run. You can't edit this setting after you deploy your function.
For Trigger type, select Cloud Pub/Sub.
Select the Cloud Pub/Sub topic that you configured on your budget.
Edit the Runtime, build, connections, and security settings as needed, then click Next.
Write code using the inline editor or upload a file that tells your function how to handle notifications.
See the following examples for code samples:
- Send notifications to Slack
- Control resource usage with notifications
- Disable billing usage with notifications
For details about the notifications your code will receive, see Notification format.
Set Entry point to the correct function to execute:
Click DEPLOY.
View Cloud Run function events
After you deploy the Cloud Run function, click LOGS to view the logs from your function invocations.
Test a Cloud Run function
As notifications are sent to Pub/Sub, subscribers receive the messages. To test a sample notification and ensure that your function is working as expected, publish a message in Pub/Sub with the following object as the message body:
{
"budgetDisplayName": "name-of-budget",
"alertThresholdExceeded": 1.0,
"costAmount": 100.01,
"costIntervalStart": "2019-01-01T00:00:00Z",
"budgetAmount": 100.00,
"budgetAmountType": "SPECIFIED_AMOUNT",
"currencyCode": "USD"
}
You can also add message attributes such as the billing account ID. For more information, see Notification format.
What's next
To learn more about how you can use programmatic notifications, review the following examples of programmatic cost control responses:
- Send notifications to Slack
- Control resource usage with notifications
- Disable billing usage with notifications