Yandex Cloud
  • Services
  • Solutions
  • Why Yandex Cloud
  • Blog
  • Pricing
  • Documentation
  • Contact us
Get started
Language / Region
Yandex project
© 2023 Yandex.Cloud LLC
Yandex Cloud Functions
  • Comparison with other Yandex Cloud services
  • Getting started
    • Overview
    • Creating a function
      • Overview
      • Node.js
      • Python
      • Go
      • PHP
      • Bash
      • Java
  • Step-by-step instructions
    • All instructions
    • Using functions to get an IAM token for a service account
    • Connecting to managed databases from functions
    • Getting information about a function
      • Getting a list of functions
      • Getting a list of function versions
      • Getting information about a function
      • Getting information about a function version
    • Managing rights to access functions
      • Making a function public
      • Making a function private
      • Viewing roles assigned to a function
      • Assigning roles to a function
      • Revoking roles assigned to a function
    • Creating a function
      • Creating a function
      • Creating a function version
    • Invoking a function
      • Invoking a function
      • Authenticating when invoking a private function via HTTPS
    • Managing functions
      • Updating a function
        • Updating a name
        • Updating a description
      • Scaling a function
        • Viewing scaling settings
        • Adding scaling settings
        • Deleting scaling settings
      • Adding environment variables
      • Specifying a cloud network
      • Transmitting Yandex Lockbox secrets
      • Managing tags
        • Adding a tag
        • Removing a tag
      • Managing labels
        • Adding a label
        • Updating a label
        • Deleting a label
      • Viewing monitoring charts
      • Managing logs
        • Viewing logs
        • Writing logs
      • Deleting a function
    • Getting information about a trigger
      • Getting a list of triggers
      • Getting information about a trigger
    • Creating a trigger
      • Create a timer
      • Creating a trigger for Message Queue
      • Creating a trigger for Object Storage
      • Creating a trigger for Container Registry
      • Creating a trigger for Cloud Logs
      • Creating a trigger for Cloud Logging
      • Creating a trigger for Yandex IoT Core
      • Creating a trigger for budgets
      • Creating a trigger for Data Streams
      • Creating an email trigger
    • Managing triggers
      • Updating a trigger
        • Updating a name
        • Updating a description
      • Managing labels
        • Adding a label
        • Updating a label
        • Deleting a label
      • Viewing monitoring charts
      • Deleting a trigger
  • Concepts
    • Overview
    • Function
    • Invoking a function
    • Networking
    • Runtime environment
      • Overview
      • Environment
      • Execution context
      • Preloaded runtime environment
    • Builder
    • Trigger
      • Overview
      • Timer
      • Trigger for Message Queue
      • Trigger for Object Storage
      • Trigger for Container Registry
      • Trigger for Cloud Logs
      • Trigger for Cloud Logging
      • Trigger for Yandex IoT Core
      • Trigger for budgets
      • Trigger for Data Streams
      • Email trigger
    • Dead Letter Queue
    • Function logs
    • Backups
    • Quotas and limits
  • Developing in Node.js
    • Overview
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in Python
    • Overview
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in Go
    • Overview
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in PHP
    • Overview
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
  • Developing in Bash
    • Overview
    • Request handler
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in Java
    • Overview
    • Programming model
      • Overview
      • Function interface
      • YcFunction interface
      • HttpServlet class
      • Spring Boot
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Developing in R
    • Overview
    • Programming model
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
  • Developing in C#
    • Overview
    • Programming model
      • Overview
      • Function interface
      • YcFunction interface
    • Managing dependencies
    • Request handler
    • Invocation context
    • Logging
    • Handling errors
    • Using the SDK
  • Practical guidelines
    • All practical guidelines
    • Creating skills for Alice
    • Deploying a web application
    • Developing a skill for Alice and a website with authorization
    • Writing data from a device to Managed Service for PostgreSQL
    • Developing a Slack bot
    • Developing a Telegram bot
    • Connecting to a YDB database from a Python function
    • Connecting to a YDB database from a function in Node.js
    • Converting a video to a GIF in Python
    • Creating a Node.js function using TypeScript
    • Developing user integration
    • Creating a trigger for budgets that invokes a function to stop VM instances
  • Pricing policy
  • Access management
  • API Functions reference
    • Authentication in the API
    • gRPC
      • Overview
      • FunctionService
      • OperationService
    • REST
      • Overview
      • Function
        • Overview
        • create
        • createVersion
        • delete
        • get
        • getVersion
        • getVersionByTag
        • list
        • listAccessBindings
        • listOperations
        • listRuntimes
        • listScalingPolicies
        • listTagHistory
        • listVersions
        • removeScalingPolicy
        • removeTag
        • setAccessBindings
        • setScalingPolicy
        • setTag
        • update
        • updateAccessBindings
  • API Triggers reference
    • Authentication in the API
    • gRPC
      • Overview
      • TriggerService
      • OperationService
    • REST
      • Overview
      • Trigger
        • Overview
        • create
        • delete
        • get
        • list
        • listOperations
        • pause
        • resume
        • update
  • Questions and answers
  1. Step-by-step instructions
  2. Creating a trigger
  3. Creating a trigger for Cloud Logging

Creating a trigger for Cloud Logging that invokes a Cloud Functions function

Written by
Yandex Cloud
  • Before you begin
  • Creating a trigger
  • Checking the result
  • See also

Create a trigger for Cloud Logging that calls a Cloud Functions function when entries are added to a log group.

Before you begin

To create a trigger, you need:

  • A function that the trigger will invoke. If you don't have a function:

    • Create a function.
    • Create a function version.
  • (optional) A Dead Letter Queue where messages that could not be processed by a function will be redirected. If you don't have a queue, create one.

  • Service accounts with rights to invoke the function and (optionally) write messages to the Dead Letter Queue. You can use the same service account or different ones. If you don't have a service account, create one.

  • The log group that activates the trigger when records are added to it. If you don't have a log group, create one.

Creating a trigger

Note

The trigger is initiated within 5 minutes of being created.

Management console
CLI
API
Terraform
  1. In the management console, select the folder where you wish to create your trigger.

  2. Select Cloud Functions.

  3. On the left-hand panel, select Triggers.

  4. Click Create trigger.

  5. Under Basic parameters:

    • Enter a name and description for the trigger.
    • In the Type field, select Cloud Logging.
    • In the Launched resource field, select Function.
  6. Under Cloud Logging settings, specify:

    • The log group.
    • (Optional) Resource types: Yandex Cloud or your services, such as, serverless.function.
    • (Optional) IDs of Yandex Cloud or your resources, such as Cloud Functions functions.
    • (Optional) Logging levels.

    A trigger activates when records that match all the optional settings are added to the specified log group. If an optional setting is undefined, the trigger activates for any value of the setting.

  7. (optional) Under Batch message settings, specify:

    • Batch size. Valid values range from 1 to 100. The default is 1.
    • Maximum wait time. Valid values range from 1 to 60 seconds. The default is 1 second.

    The trigger groups messages for a period of time not exceeding the specified timeout and sends them to a function. However, the number of messages does not exceed the specified group size.

  8. Under Function settings, select a function and specify:

    • Tag of the function version.
    • A service account to be used to invoke the function.
  9. (optional) Under Repeat request settings:

    • In the Interval field, specify the time after which the function will be invoked again if the current attempt fails. Values can be from 10 to 60 seconds. The default is 10 seconds.
    • In the Number of attempts field, specify the number of invocation retries before the trigger moves a message to the Dead Letter Queue. Values can be from 1 to 5. The default is 1.
  10. (optional) Under Dead Letter Queue settings, select the Dead Letter Queue and the service account with write privileges for this queue.

  11. Click Create trigger.

If you don't have the Yandex Cloud command line interface yet, install and initialize it.

The folder specified in the CLI profile is used by default. You can specify a different folder using the --folder-name or --folder-id parameter.

To create a trigger that invokes a function, run the command:

yc serverless trigger create logging \
  --name <trigger name> \
  --log-group-name <log group name> \
  --batch-size 1 \
  --batch-cutoff 1s \
  --invoke-function-id <function ID> \
  --invoke-function-service-account-id <service account ID> \
  --retry-attempts 1 \
  --retry-interval 10s \
  --dlq-queue-id <Dead Letter Queue ID> \
  --dlq-service-account-id <service account ID>

Where:

  • --name: Trigger name.
  • --log-group-name: The name of the log group that will cause the function to be called when records are added.
  • --batch-size: Message batch size. Optional. Valid values range from 1 to 100. The default is 1.
  • --batch-cutoff: Maximum waiting time. Optional. Valid values range from 0 to 60 seconds. The default is 1 second. The trigger groups messages for a period not exceeding batch-cutoff and sends them to a function. At the same time, the number of messages does not exceed batch-size.
  • --invoke-function-id: Function ID.
  • --invoke-function-service-account-id: Service account with rights to invoke the function.
  • --retry-attempts: The time after which the function will be invoked again if the current attempt fails. Optional. Values can be from 10 to 60 seconds. The default is 10 seconds.
  • --retry-interval: The number of invocation retries before the trigger moves a message to the Dead Letter Queue. Optional. Values can be from 1 to 5. The default is 1.
  • --dlq-queue-id: Dead Letter Queue ID. Optional.
  • --dlq-service-account-id: Service account with rights to write messages to the Dead Letter Queue. Optional.

Result:

id: a1sfe084v4**********
folder_id: b1g88tflru**********
created_at: "2019-12-04T08:45:31.131391Z"
name: logging-trigger
rule:
  logging:
    log-group-name: default
    batch_settings:
      size: "1"
      cutoff: 1s
    invoke_function:
      function_id: d4eofc7n0m**********
      function_tag: $latest
      service_account_id: aje3932acd**********
      retry_settings:
        retry_attempts: "1"
        interval: 10s
      dead_letter_queue:
        queue-id: yrn:yc:ymq:ru-central1:aoek49ghmk**********:dlq
        service-account-id: aje3932a**********
status: ACTIVE

You can create a trigger for Cloud Logging using the create.

With Terraform, you can quickly create a cloud infrastructure in Yandex Cloud and manage it by configuration files. They store the infrastructure description in HashiCorp Configuration Language (HCL). Terraform and its providers are distributed under the Mozilla Public License.

For more information about the provider resources, see the documentation on the Terraform site or mirror site.

If you change the configuration files, Terraform automatically determines which part of your configuration is already deployed and what should be added or removed.

If you don't have Terraform, install it and configure the Yandex Cloud provider.

To create a trigger for Cloud Logging:

  1. In the configuration file, describe the trigger parameters.

    Example configuration file structure:

    resource "yandex_function_trigger" "my_trigger" {
      name        = "<trigger name>"
      description = "<trigger description>"
      logging {
         group_id       = "<log group ID>"
         resource_types = [ "<resource type>" ]
         resource_ids   = [ "<resource ID>" ]
         levels         = [ "INFO", "ERROR" ]
         batch_cutoff   = 1
         batch_size     = 1
      function {
         id                 = "<function ID>"
         service_account_id = "<service account ID>"
      }
    }
    

    Where:

    • name: Trigger name. Name format:

      • The length can be from 3 to 63 characters.
      • It may contain lowercase Latin letters, numbers, and hyphens.
      • The first character must be a letter. The last character can't be a hyphen.
    • description: Trigger description.

    • logging: Logging parameters, which will activate the trigger when added to the log group, and the batch message settings:

      • group_id: Log group ID.
      • resource_types: Resource types: your services or Yandex Cloud services, for example, resource_types = [ "serverless.function" ]. You can specify more than one service at a time.
      • resource_ids: IDs of your resources or Yandex Cloud resources, for example, functions resource_ids = [ "<function ID>" ]. You can specify multiple IDs.
      • levels: Logging levels. For example, levels = [ "INFO", "ERROR"].
      • batch_cutoff: Maximum wait time. Acceptable values are from 0 to 60 seconds. The trigger groups messages for a period of time not exceeding the specified timeout and sends them to a function or container. At the same time, the number of messages does not exceed the specified batch-size group.
      • batch_size: Message batch size. Acceptable values are from 1 to 100.
    • function: Settings for the function, which will be activated by the trigger:

      • id: Function ID.
      • service_account_id: ID of the service account with rights to invoke a function.

    For more information about resource parameters in Terraform, see the provider documentation.

  2. Make sure that the configuration files are valid.

    1. In the command line, go to the directory where you created the configuration file.

    2. Run the check using the command:

      terraform plan
      

    If the configuration is described correctly, the terminal displays a list of created resources and their parameters. If the configuration contains errors, Terraform will point them out.

  3. Deploy the cloud resources.

    1. If the configuration doesn't contain any errors, run the command:

      terraform apply
      
    2. Confirm the resource creation: type yes in the terminal and press Enter.

      Afterwards, all the necessary resources are created in the specified folder. You can verify that the resources are there and properly configured in the management console or using the following CLI command:

      yc serverless trigger get <trigger ID>
      

Checking the result

Make sure the trigger is working properly. To do this, view function logs: they show information about calls.

See also

  • Trigger for Cloud Logging that invokes a Serverless Containers container.

Was the article helpful?

Language / Region
Yandex project
© 2023 Yandex.Cloud LLC
In this article:
  • Before you begin
  • Creating a trigger
  • Checking the result
  • See also