Stay organized with collections
Save and categorize content based on your preferences.
Example reference architecture
Figure 1: An example of using budget alerts
to automate cost control responses using Pub/Sub for programmatic
notifications and Cloud Run functions to automate a response.
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:
Enter a Service 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.
Click Add trigger and select Cloud Pub/Sub.
If prompted, enable any required APIs.
In the Eventarc trigger panel, complete the following steps:
Enter a Trigger name.
Set Trigger type to Google sources.
Set Event provider to Cloud Pub/Sub.
Select the Cloud Pub/Sub topic that 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, Security settings
as needed, then click Create.
Write code using the inline editor or upload a file that tells your function
how to handle notifications.
For details about the notifications your code will receive, see
Notification format.
Set Function 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:
[[["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-25 UTC."],[[["\u003cp\u003eThis guide details how to use Cloud Run functions to listen for and respond to programmatic budget notifications sent via Pub/Sub.\u003c/p\u003e\n"],["\u003cp\u003eTo begin, you need to create a budget and configure programmatic notifications to ensure the Pub/Sub topic is correctly set up.\u003c/p\u003e\n"],["\u003cp\u003eYou can create a Cloud Run function by selecting "Cloud Pub/Sub" as the trigger type and choosing the budget-configured topic.\u003c/p\u003e\n"],["\u003cp\u003eAfter deploying your Cloud Run function, you can check the function's logs for invocations and test it by publishing a sample message to the Pub/Sub topic.\u003c/p\u003e\n"],["\u003cp\u003eCloud Run functions let you create functions that respond to cloud events without needing to manage a server or runtime environment.\u003c/p\u003e\n"]]],[],null,["# Listen to your Pub/Sub notifications\n\n\u003cbr /\u003e\n\nExample reference architecture\n**Figure 1**: An example of using budget alerts to automate cost control responses using Pub/Sub for programmatic notifications and Cloud Run functions to automate a response.\n\nThis document explains how to listen to your programmatic budget\nnotifications by subscribing to your Pub/Sub topic with\n[Cloud Run function](/functions/docs/concepts/events-triggers)\ntriggers. Without a subscriber, Pub/Sub drops published messages\nand you can't retrieve them later. In this document, you'll learn how to\ncomplete the following tasks:\n\n1. Create a Cloud Run function that listens to notifications.\n2. View Cloud Run function events.\n3. Test a Cloud Run function.\n\n| **Note:** Although there are many ways you can [subscribe to your topic](/pubsub/docs/subscriber), the examples on this page use [Cloud Run functions](/functions/docs). Cloud Run functions lets you create functions that respond to cloud events without needing to manage a server or runtime environment.\n\nBefore you begin\n----------------\n\nBefore you begin, you must complete the following tasks:\n\n1. [Create a budget](/billing/docs/how-to/budgets)\n2. [Set up programmatic notifications](/billing/docs/how-to/budgets-programmatic-notifications)\n\nCreate a Cloud Run function\n---------------------------\n\nTo create a Cloud Run function, complete the following steps:\n\n1. In the Google Cloud console, go to the **Cloud Run functions** page.\n\n [Go to Cloud Run functions](https://console.cloud.google.com/functions)\n2. Click **CREATE SERVICE**.\n\n3. Select **Use an inline editor to create a function**.\n\n4. Enter a **Service name** that's meaningful to\n your budget notification.\n\n5. Select the [region](/functions/docs/locations)\n where your Cloud Run functions will run. You can't edit this setting\n after you deploy your function.\n\n6. Click **Add trigger** and select **Cloud Pub/Sub**.\n\n7. If prompted, enable any required APIs.\n\n8. In the **Eventarc trigger** panel, complete the following steps:\n\n 1. Enter a **Trigger name**.\n 2. Set **Trigger type** to **Google sources**.\n 3. Set **Event provider** to **Cloud Pub/Sub**.\n 4. Select the **Cloud Pub/Sub topic** that you configured on your budget.\n 5. Select a **Region**.\n 6. Click **Save trigger**.\n9. Edit the **Identity-Aware Proxy** , **Billing** , **Service scaling** ,\n **Ingress** , and **Container(s), Volume, Networking, Security** settings\n as needed, then click **Create**.\n\n10. Write code using the inline editor or upload a file that tells your function\n how to handle notifications.\n\n See the following examples for code samples:\n - [Send notifications to Slack](/billing/docs/how-to/send-notifications-to-slack)\n - [Control resource usage with notifications](/billing/docs/how-to/control-usage)\n - [Disable billing usage with notifications](/billing/docs/how-to/disable-billing-with-notifications)\n\n For details about the notifications your code will receive, see\n [Notification format](/billing/docs/how-to/budgets-programmatic-notifications#notification-format).\n11. Set **Function entry point** to the correct function to execute:\n\n12. Click **DEPLOY**.\n\nView Cloud Run function events\n------------------------------\n\nAfter you deploy the Cloud Run function, click **LOGS** to view\nthe logs from your function invocations.\n\nTest a Cloud Run function\n-------------------------\n\nAs notifications are sent to Pub/Sub, subscribers receive the\nmessages. To test a sample notification and ensure that your function is working\nas expected,\n[publish a message in Pub/Sub](/pubsub/docs/publisher)\nwith the following object as the message body: \n\n {\n \"budgetDisplayName\": \"name-of-budget\",\n \"alertThresholdExceeded\": 1.0,\n \"costAmount\": 100.01,\n \"costIntervalStart\": \"2019-01-01T00:00:00Z\",\n \"budgetAmount\": 100.00,\n \"budgetAmountType\": \"SPECIFIED_AMOUNT\",\n \"currencyCode\": \"USD\"\n }\n\nYou can also add message attributes such as the billing account ID. For more\ninformation, see\n[Notification format](/billing/docs/how-to/budgets-programmatic-notifications#notification-format).\n\nWhat's next\n-----------\n\nTo learn more about how you can use programmatic notifications, review the\nfollowing examples of programmatic cost control responses:\n\n- [Send notifications to Slack](/billing/docs/how-to/send-notifications-to-slack)\n- [Control resource usage with notifications](/billing/docs/how-to/control-usage)\n- [Disable billing usage with notifications](/billing/docs/how-to/disable-billing-with-notifications)"]]