[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-08-18 (世界標準時間)。"],[[["\u003cp\u003eThis guide teaches how to set up automated responses to Cloud Billing budget and cost anomaly notifications using Pub/Sub.\u003c/p\u003e\n"],["\u003cp\u003eYou can automate cost control responses by integrating with platforms like Slack, managing resource consumption, or even disabling billing when a budget is reached.\u003c/p\u003e\n"],["\u003cp\u003eTo set up these programmatic notifications, you'll need specific permissions for creating or modifying budgets/anomalies and Pub/Sub topics, varying based on your access level (Cloud Billing account or project).\u003c/p\u003e\n"],["\u003cp\u003eBudget notifications are sent multiple times daily via Pub/Sub with the budget's status, while anomaly notifications are sent only when a predefined cost impact threshold is met.\u003c/p\u003e\n"],["\u003cp\u003eThe tutorial covers creating a Pub/Sub topic, connecting it to budgets or anomalies, and understanding the notification format, which includes attributes and data describing the event and budget/anomaly details.\u003c/p\u003e\n"]]],[],null,["# Set up programmatic notifications\n\n\u003cbr /\u003e\n\n| #### Interactive tutorials: Set up automated responses to Cloud Billing\n| budget notifications (30 minutes)\n|\n| In this interactive tutorial, you'll learn how to do the following:\n|\n| - Set up a Pub/Sub topic\n| - Create a budget on a billing account that's connected to the Pub/Sub topic\n| - Create a Cloud Run function that listens for updates from the Pub/Sub topic\n| - Test your Cloud Run function\n|\n| [**Launch the tutorial**](https://console.cloud.google.com/home/dashboard?walkthrough_tutorial_id=billing--budget--cost_enforcement)\n\nThis document explains how to set up programmatic budget and cost anomaly\n([Preview](/products#product-launch-stages))\nnotifications using\n[Pub/Sub](/pubsub/docs/pubsub-basics).\n\n[Budgets](/billing/docs/how-to/budgets#manage-notifications)\nand [cost anomalies](/billing/docs/how-to/manage-anomalies#set-notifications)\nare typically configured to send email notifications. If you use budgets or\ncost anomaly detection as a cost control tool, email notifications might not be\nthe best method to use to ensure timely action to control your costs.\nYou can set up and use programmatic notifications to automate cost control\nresponses. Examples of automated cost control responses include the following:\n\n- [Integrate with Slack for faster response](/billing/docs/how-to/send-notifications-to-slack)\n- [Manage resource consumption to reduce costs](/billing/docs/how-to/control-usage)\n- [Limit spending by disabling billing when you reach your budget](/billing/docs/how-to/disable-billing-with-notifications)\n\nLimitations\n-----------\n\n- Budgets use estimated Cloud Billing data which is subject to\n change until your invoice is finalized.\n\n- If you set [organization policies](/resource-manager/docs/organization-policy/overview)\n to [limit resource sharing by domain](/resource-manager/docs/organization-policy/restricting-domains)\n (for example, enabling `enforceInTransit` on Pub/Sub topics),\n you might encounter an error when you try to set up or connect Pub/Sub\n topics. In this situation, you might need to\n [force account access](/resource-manager/docs/organization-policy/restricting-domains#forcing_access)\n to successfully connect a Pub/Sub topic to a budget. To force\n account access, complete the following steps:\n\n 1. Remove the organization policy containing the domain restriction constraint using one of the following methods:\n - Temporarily [remove the organization policy](/resource-manager/docs/organization-policy/using-constraints#delete_an_organization_policy).\n - [Override the organization policy for the project](/resource-manager/docs/organization-policy/using-constraints#override_the_organization_policy_for_a_project) that contains the [Pub/Sub topic](#pub-sub-topic).\n 2. Follow the steps to [connect a Pub/Sub topic](#connect-topic).\n 3. Reinstate the [organization policy](/resource-manager/docs/organization-policy/using-constraints#set_up_enforcement_on_the_organization_resource) for the organization or project (optional).\n\nPermissions required for this task\n----------------------------------\n\n### Budget notifications\n\nTo set up programmatic budget notifications, you need permissions to create or\nmodify budgets and Pub/Sub topics. You need a different set of\npermissions depending on your level of access.\n\n### Anomaly notifications\n\nTo set up programmatic anomaly notifications, you need permissions to create\nor modify anomalies and Pub/Sub topics. You must have\nCloud Billing account permissions.\n\nCreate a Pub/Sub topic\n----------------------\n\nTo set up programmatic budget or anomaly notifications, you must first\n[create a Pub/Sub topic](/pubsub/docs/create-topic).\nIf you already configured a Pub/Sub\ntopic for budgets or cost anomalies, you can skip this step.\n\n1. Go to the Google Cloud console **Pub/Sub** page.\n\n [Sign in to Google Cloud console Pub/Sub](https://console.cloud.google.com/cloudpubsub/)\n2. Select a project to contain the Pub/Sub topic.\n\n When configuring Pub/Sub topics for billing notifications, we\n recommend you use a\n [FinOps administration project](/billing/docs/how-to/create-billing-account#set-up-billing-project)\n to contain your billing-related Pub/Sub topics.\n3. Click add_box\n **Create topic**.\n\n4. In the **Topic ID** field, provide a topic name.\n\n5. Retain the default subscription and encryption settings, and click\n **Create**.\n\n| **Note:** You must have permission to grant the Pub/Sub Publisher role on the topic to allow Cloud Billing to publish messages to it.\n\nConnect your Pub/Sub topic\n--------------------------\n\nTo enable programmatic notifications to receive Pub/Sub\nmessages with the current status of your budget or anomaly, you must connect\nyour budget or anomaly to your Pub/Sub topic. \n\n### Budget notifications\n\nTo connect a Pub/Sub topic to a Cloud Billing budget,\ncomplete the following steps:\n\n### Anomaly notifications\n\nTo connect a Pub/Sub topic to a cost anomaly, complete the\nfollowing steps:\n\nNotification format\n-------------------\n\nNotifications sent to the Pub/Sub topic consist of two parts:\n\n- **Attributes**: A set of key-value pairs describing the event.\n- **Data**: A string containing a JSON object that describes the budget alert\n details.\n\n### Budget notifications\n\n### Attributes\n\nAttributes are key-value pairs contained in all notifications sent\nby Cloud Billing to your Pub/Sub topic. Notifications always\ncontain the following set of key-value pairs, regardless of the\nnotification payload.\n\n### Data\n\nBudget alert details are returned in a base64-encoded UTF-8 string containing\na JSON object with the following properties:\n\n| **Note:** To test an example budget notification, follow the steps in [Listen to your notifications](/billing/docs/how-to/listen-to-notifications).\n\n### Anomaly notifications\n\n### Attributes\n\nAttributes are key-value pairs contained in all notifications sent by\nCloud Billing to your Pub/Sub topic. Notifications always\ncontain the following set of key-value pairs, regardless of the notification\npayload.\n\n### Data\n\nAnomaly alert details are returned in a base64-encoded UTF-8 string containing\na JSON object with the following properties:\n\nDelivery guarantees for notifications to Pub/Sub\n------------------------------------------------\n\nAfter you connect your budget or anomaly to a Pub/Sub topic, you\ncan expect to receive programmatic notifications as follows: \n\n### Budget notifications\n\n- Budget notifications are sent to the Pub/Sub topic multiple times per day with the current status of your budget. This cadence is different than the cadence for budget alert emails, which are only sent when a [budget threshold](/billing/docs/how-to/budgets#alert-thresholds) is met.\n- Pub/Sub notifications are sent even if the Cloud Billing account has no usage.\n- It may take several hours before you receive the first Pub/Sub notification.\n- Pub/Sub only guarantees at-least-once delivery. You might receive a message multiple times, and messages might arrive out of order.\n- If delivery fails due to Pub/Sub topic misconfiguration, you won't be notified.\n\nDelivery might fail for one of the following reasons:\n\n- The Pub/Sub topic no longer exists.\n- The Cloud Billing no longer has permission to publish to the Pub/Sub topic.\n- You exceeded your notification quota.\n- You have [organization policies](/resource-manager/docs/organization-policy/overview) in place that [limit resource sharing by domain](/resource-manager/docs/organization-policy/restricting-domains).\n\n### Anomaly notifications\n\n- Anomaly notifications are sent to the Pub/Sub topic only when the [threshold](/billing/docs/how-to/manage-anomalies#set-anomaly-threshold) is reached.\n- Pub/Sub only guarantees an at-least-once delivery. You might receive a message multiple times, and messages might arrive out of order.\n- If delivery fails due to Pub/Sub topic misconfiguration, you won't be notified.\n\nDelivery might fail for one of the following reasons:\n\n- The Pub/Sub topic no longer exists.\n- The Cloud Billing account no longer has permission to publish to the Pub/Sub topic.\n- You exceeded your notification quota.\n- You have [organization policies](/resource-manager/docs/organization-policy/overview) in place that [limit resource sharing by domain](/resource-manager/docs/organization-policy/restricting-domains).\n\nFor more information, see\n[Publish and receive messages in Pub/Sub](/pubsub/docs/publish-receive-messages-console).\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- [Listen to your notifications](/billing/docs/how-to/listen-to-notifications)\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)"]]