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 functionspage.
-
Click CREATE SERVICE.
-
Select Use an inline editor to create a function.
-
Enter a Service namethat'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.
-
Click Add triggerand select Cloud Pub/Sub.
-
If prompted, enable any required APIs.
-
In the Eventarc triggerpanel, complete the following steps:
- Enter a Trigger name.
- Set Trigger typeto Google sources.
- Set Event providerto Cloud Pub/Sub.
- Select the Cloud Pub/Sub topicthat you configured on your budget.
- Select a Region.
- Click Save trigger.
-
Edit the Identity-Aware Proxy, Billing, Service scaling, Ingress, and Container(s), Volume, Networking, Securitysettings as needed, then click Create.
-
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 Function entry pointto the correct function to execute:
-
Click DEPLOY.
View Cloud Run function events
After you deploy the Cloud Run function, click LOGSto 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:

