בדרך כלל, התראות לגבי תקציבים וחריגות בעלויות מוגדרים לשליחה באימייל. אם אתם משתמשים בהתראות לגבי תקציבים או חריגות בעלויות בתור כלי לבקרה על עלויות, יכול להיות שקבלת התראות באימייל לא תאפשר לכם להגיב בזמן ולבצע פעולות בקרה על העלויות.
אתם יכולים להגדיר התראות פרוגרמטיות ולהשתמש בהן כדי לשלוח תגובות אוטומטיות לבקרת עלויות. דוגמאות לתגובות אוטומטיות לבקרת עלויות:
התקציבים מבוססים על נתוני חיוב משוערים ב-Cloud. יכול להיות שיהיו בהם שינויים לפני סגירת החשבונית.
אם במדיניות הארגון שלכם מוגדרת הגבלה על שיתוף המשאבים לפי דומיין, (לדוגמה, הפעלת enforceInTransit בנושאים ב-Pub/Sub), יכול להיות שתקבלו הודעת שגיאה כשתנסו להגדיר או לקשר נושאי Pub/Sub. במצב כזה, יכול להיות שתצטרכו לאלץ את הגישה לחשבון כדי לקשר נושא Pub/Sub לתקציב. כדי לאלץ גישה לחשבון, צריך לבצע את הפעולות האלה:
מסירים את מדיניות הארגון שכוללת את אילוץ ההגבלה לפי הדומיין באחת מהשיטות האלה:
הפעלה מחדש של מדיניות הארגון ברמת הארגון או הפרויקט (לא חובה).
ההרשאות שנדרשות לביצוע המשימה הזו
התראות לגבי תקציבים
כדי להגדיר התראות פרוגרמטיות לגבי תקציבים, אתם צריכים הרשאות ליצירה או לשינוי של תקציבים ונושאי Pub/Sub. אתם צריכים הגדרה שונה של הרשאות בהתאם לרמת הגישה שלכם.
pubsub.topics.list בפרויקט היעד שכולל את הנושאים ב-Pub/Sub שרוצים להשתמש בהם
כדי לקבל את ההרשאות הנדרשות באמצעות תפקידים מוגדרים מראש, אתם צריכים לבקש מהאדמין שבחשבון לחיוב ב-Cloud יוקצה לכם אחד מתפקידי ה-IAM בחיוב ב-Cloud האלה:
כדי לקבל את ההרשאות הנדרשות באמצעות תפקידים מוגדרים מראש, אתם צריכים לבקש מהאדמין שבחשבון לחיוב ב-Cloud יוקצה לכם אחד מתפקידי ה-IAM בחיוב ב-Cloud האלה:
כדי להגדיר התראות פרוגרמטיות לגבי תקציבים או חריגות, קודם צריך ליצור נושא Pub/Sub.
אם כבר הגדרתם נושא Pub/Sub של תקציבים או חריגות בעלויות, אתם יכולים לדלג על השלב הזה.
בהודעה שמופיעה, בוחרים את החשבון לחיוב ב-Cloud שרוצים לנהל את החריגות שבו.
ייפתח הדף Anomalies של החשבון לחיוב שבחרתם.
לוחצים על settingsManage anomalies.
מזינים ערך Anomaly threshold של Cost impact. הסכום הזה הוא ערך הסף שגורם להפעלה של ההתראות. אם לא מזינים ערך, ערך הסף שמוגדר הוא אפס ונשלחות התראות על כל החריגות.
בקטע Pub/Sub notifications, בוחרים באפשרות Connect a Pub/Sub topic to receive programmatic updates about anomalies.
בתפריט הנפתח שמופיע, בוחרים את נושא ה-Pub/Sub שרוצים לקשר אליו בשביל לקבל התראות.
הנושאים רשומים לפי פרויקט. יכול להיות שצריך לעבור לפרויקט אחר כדי למצוא את הנושא שרוצים לבחור.
אם רוצים ליצור נושא חדש, עוברים לפרויקט המועדף לפני שלוחצים על Create a topic.
לוחצים על Save.
פורמט ההתראות
ההתראות שנשלחות לנושא Pub/Sub כוללות שני חלקים:
מאפיינים: קבוצה של צמדי מפתח/ערך שמתארים את האירוע.
נתונים: מחרוזת שמכילה אובייקט JSON לתיאור הפרטים של ההתראה בקשר לתקציב.
התראות לגבי תקציבים
מאפיינים
המאפיינים הם צמדי מפתח/ערך שכלולים בכל ההתראות שנשלחות מהחיוב ב-Cloud לנושא Pub/Sub. ההתראות תמיד כוללות את הקבוצה הבאה של צמדי מפתח/ערך, ללא קשר למטען הייעודי (payload) שלהן.
שם המאפיין
דוגמה
תיאור
billingAccountId
01D4EE-079462-DFD6EC
המזהה של החשבון לחיוב ב-Cloud שהתקציב בבעלותו.
budgetId
de72f49d-779b-4945-a127-4d6ce8def0bb
המזהה של ההתראה לגבי תקציבים בחשבון לחיוב ב-Cloud.
schemaVersion
1.0
גרסת הסכימה שבשימוש בהתראות.
נתונים
הפרטים של ההתראות לגבי תקציבים מוחזרים במחרוזת מסוג UTF-8 בקידוד base64 עם אובייקט JSON שכולל את המאפיינים האלה:
נתונים
דוגמה
תיאור
budgetDisplayName
"My Personal Budget"
השם שהוקצה לתקציב, בפורמט שקריא לבני אדם.
costAmount
140.321
סכום העלויות שהצטברו. סוג העלויות שבמעקב תלוי במסננים ובהגדרות של התקציב.
costIntervalStart
"2021-02-01T08:00:00Z"
מועד ההתחלה של פרק הזמן להתראות בקשר לתקציב. הערך ב-'costAmount' כולל את עלויות השימוש שמתחילות בפרק הזמן הזה. זהו היום הראשון בפרק הזמן של התקציב (חודש, רבעון, שנה או טווח תאריכים מותאם אישית) שבמהלכו התבצע השימוש בתקציב.
budgetAmount
152.557
הסכום שהוקצה בתקציב.
budgetAmountType
"SPECIFIED_AMOUNT"
הסוג של סכום התקציב. יכול להיות אחד מהערכים האלה:
"SPECIFIED_AMOUNT": סכום קבוע.
"LAST_MONTH_COST": העלות הכוללת שהצטברה במהלך החודש הקלנדרי האחרון.
"LAST_PERIODS_COST": העלויות שהצטברו במהלך התקופה הקלנדרית האחרונה, שהיא לא חודשית, כמו רבעון או שנה.
alertThresholdExceeded
0.9
הסף הגבוה ביותר שהוגדר להתראות שהעלות בפועל חרגה ממנו. הערך הוא אחוז שמוצג כשבר עשרוני (לדוגמה, 0.9 הוא 90%). המפתח הזה לא נמצא אם העלות בפועל לא חורגת מסף כלשהו.
forecastThresholdExceeded
0.2
הסף הגבוה ביותר שהוגדר להתראות שהעלות החזויה חרגה ממנו. הערך הוא אחוז שמוצג כשבר עשרוני (לדוגמה, 0.2 הוא 20%). המפתח הזה לא נמצא אם העלות החזויה לא חורגת מסף כלשהו.
currencyCode
"USD"
המטבע שבו משתמשים בהתראות בקשר לתקציב. כל העלויות והסכומים של ההתראות לגבי תקציבים הם במטבע הזה.
התראות לגבי חריגות
מאפיינים
המאפיינים הם צמדי מפתח/ערך שכלולים בכל ההתראות שנשלחות מהחיוב ב-Cloud לנושא Pub/Sub. לא משנה מהו המטען הייעודי (payload) של ההתראה, ההתראות תמיד כוללות את צמדי המפתח/ערך האלה:
שם המאפיין
דוגמה
תיאור
scope
scope_project
היקף החריגה.
billingAccountName
billingAccounts/01D4EE-079462-DFD6EC
המזהה של החשבון לחיוב ב-Cloud שהייתה בו חריגה.
resourceDisplayName
My project
השם המוצג של המשאב שהייתה בו חריגה.
resourceName
projects/123456
המזהה של המשאב שהייתה בו חריגה.
SchemaVersion
1.0
הגרסה של סכימת ה-Pub/Sub.
נתונים
הפרטים של ההתראות לגבי חריגה מוחזרים במחרוזת מסוג UTF-8 בקידוד base64 עם אובייקט JSON שכולל את המאפיינים האלה:
אחרי שמקשרים את התקציב או החריגה לנושא Pub/Sub, כך אתם אמורים לקבל את ההתראות הפרוגרמטיות:
התראות לגבי תקציבים
ההתראות בקשר לתקציב נשלחות לנושא Pub/Sub כמה פעמים ביום, עם הסטטוס הנוכחי של התקציב. קצב השליחה שונה מקצב השליחה של אימיילים של התראות רגילות לגבי התקציב, שנשלחים רק כשמגיעים לסף התקציב.
ההתראות נשלחות ל-Pub/Sub גם אם אין שימוש בחשבון לחיוב ב-Cloud.
יכול להיות שתקבלו את ההודעה הראשונה ב-Pub/Sub רק תוך כמה שעות.
בזכות השימוש ב-Pub/Sub תישלח הודעה אחת לפחות לכל התראה, אבל יכול להיות שתקבלו את אותה הודעה כמה פעמים ויכול להיות שההודעות יגיעו בסדר הלא נכון.
אם המסירה נכשלת בגלל הגדרה שגויה של נושא Pub/Sub, לא תקבלו התראה.
מסירת ההתראות עשויה להיכשל בגלל אחת מהסיבות הבאות:
הנושא לא קיים יותר ב-Pub/Sub.
לחיוב ב-Cloud אין יותר את ההרשאה לפרסם בנושא Pub/Sub המסוים הזה.
[[["התוכן קל להבנה","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-27 (שעון UTC)."],[[["\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)"]]