그림 1: 예산 알림을 통해 프로그래매틱 알림용 Pub/Sub와 Cloud Run 함수를 사용하여 비용 관리 응답을 자동화하여 응답을 자동화한 예시
이 문서에서는 Cloud Run 함수 트리거를 사용하여 Pub/Sub 주제를 구독하여 프로그래매틱 예산 알림을 리슨하는 방법을 설명합니다. 구독자가 없으면 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-04-22(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)"]]