[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-18。"],[[["\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)"]]