Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
  • Blog
  • Pricing
  • Documentation
Yandex project
© 2025 Yandex.Cloud LLC
Yandex Monitoring
  • Getting started
    • All guides
      • Creating alerts
      • Creating a notification channel with a recipient
      • Creating a notification channel that invokes a function
      • Creating a webhook that calls an external API
      • Creating an escalation policy
      • Deleting an alert
  • Access management
  • Terraform reference
  • Release notes

In this article:

  • Creating a service account
  • Creating a function
  • Creating a channel
  • Creating an alert
  • Testing function invocation
  1. Step-by-step guides
  2. Working with alerts
  3. Creating a webhook that calls an external API

Webhook using Cloud Functions

Written by
Yandex Cloud
Updated at April 17, 2025
  • Creating a service account
  • Creating a function
  • Creating a channel
  • Creating an alert
  • Testing function invocation

To automate processing of incidents and other events in your cloud, Yandex Monitoring supports invoking functions in Cloud Functions. This section provides an example of a webhook configured to send POST requests when an alert triggers. This means you can use alerts to call external API methods. You can also use escalations to invoke functions in Cloud Functions.

To send POST requests when an alert triggers:

  1. Deploy the service for POST request processing with the following attributes:

    • https://my.url/route/for/alarm: URL to process a request when the alert gets the Alarm status.
    • https://my.url/route/for/ok: URL to process a request when the alert gets the Ok status.
    • my_secret_token: Token or token file for call authorization.

    You can test the function without deploying the POST request processing service. In this case, when sending a request, the function call logs will display a message saying the specified URL is unavailable.

  2. Create a service account to invoke your function.

  3. Create a function sending POST requests when the alert gets the Alarm or Ok status.

  4. Create a channel that will invoke the function.

  5. Create an alert that will send notifications to the channel with your function.

  6. Test the function.

Creating a service accountCreating a service account

Management console
  1. In the management console, go to the folder containing the resources you need to track in Monitoring.
  2. From the list of services, select Identity and Access Management.
  3. Click Create service account.
  4. Enter a name for the service account, e.g., sa-alert-webhook.
  5. Add the functions.functionInvoker and functions.viewer roles.
  6. Click Create.

Creating a functionCreating a function

Management console
  1. From the list of services, select Cloud Functions.

  2. Click Create function.

  3. Enter a name for the function, e.g., alert-webhook.

  4. Click Create.

  5. Create a function version:

    1. Select the Python runtime environment, disable the Add files with code examples option, and click Continue.

    2. Choose the Code editor method.

    3. Click Create file and specify a file name, e.g., index.

    4. Enter the function code by specifying the URL for processing POST requests and the token:

      import json
      import requests
      
      WEBHOOK_ALARM_URL = "https://my.url/route/for/alarm"
      WEBHOOK_OK_URL = "https://my.url/route/for/ok"
      WEBHOOK_AUTH_TOKEN = "my_secret_token"
      
      def webhook(url, alert_id):
          headers_ = {
              "Authorization": f"OAuth {WEBHOOK_AUTH_TOKEN}",
              "Content-type": "application/json"
          }
          try:
              response = requests.post(url, headers = headers_, json = { "alert_id" : alert_id })
              if response.ok:
                  return { "status": "OK", "url": url, "response": json.dumps(response.json)}
              else:
                  return { "status": "ERROR", "url": url, "code": response.status_code, "error": response.text}
          except Exception as e:
              return { "status": "EXCEPTION", "url": url, "error": e}
          
      
      def handler(event, context):
          alert = event # For convenience, save the event to the alert variable.
          required_attributes = ["alertId", "status"] # Array of required input parameters in JSON format.
          
          # If the function input is not JSON or has missing required parameters, the function will not run.
          if not alert or all(attr in required_attributes for attr in alert):
              return
          
          result = None
          # If the function is invoked when the alert status is ALARM, send a request to WEBHOOK_ALARM_URL.
          # If the function is invoked when the alert status is OK, send a request to WEBHOOK_OK_URL.
          # Do not invoke the function in case of other alert statuses.
          if alert["status"] == "ALARM":
              result = webhook(WEBHOOK_ALARM_URL, alert["alertId"])
          elif alert["status"] == "OK":
              result = webhook(WEBHOOK_OK_URL, alert["alertId"])
          else:
              pass
          
          if not result:
              return
      
          # Output the call result to the log
          if result["status"] == "OK":
              print(f"Succesffully invoked {result['url']}. Response: {result['response']}")
          elif result["status"] == "ERROR":
              print(f"ERROR invoking {result['url']}. Code {result['code']}, error message: {result['error']}")
          else:
              print(f"{result['status']} when invoking {result['url']}. Error message: {result['error']}")
      
  6. Under Parameters, set the version parameters:

    • Entry point: index.handler
    • Service account: sa-alert-webhook
  7. Under Additional settings, enable Asynchronous call.

  8. Click Save changes.

Creating a channelCreating a channel

Management console
  1. Select Monitoring.
  2. Go to the Notification channels section.
  3. Click Create channel.
  4. Enter a name for your notification channel, e.g., channel-function.
  5. From the Method list, select Cloud Functions.
  6. From the Service account list, select the account you created when adding your function.
  7. Click Create.

Creating an alertCreating an alert

Management console
  1. In Monitoring, select Alerts.
  2. Click Create.
  3. Enter a name for the alert, e.g., alert-function.
  4. Enter the query to use for selecting which metrics to track.
  5. Configure the trigger conditions.
  6. Under Notifications, click Edit and then Add.
  7. Select channel-function.
  8. Click Add.
  9. Click Create.

Testing function invocationTesting function invocation

Management console
  1. From the list of services, select Cloud Functions.

  2. Select the alert-webhook function.

  3. Select the Testing tab.

  4. As input data, enter:

    {
      "alertId": "<alert_ID>",
      "alertName": "alert-function",
      "folderId": "<folder_ID>",
      "status": "OK"
    }
    
  5. Click Run test.

  6. In the function call logs, in the Testing or Logs tab, make sure the request was sent to this URL: https://my.url/route/for/ok.

Was the article helpful?

Previous
Creating a notification channel that invokes a function
Next
Creating an escalation policy
Yandex project
© 2025 Yandex.Cloud LLC