Yandex.Cloud
  • Services
  • Why Yandex.Cloud
  • Pricing
  • Documentation
  • Contact us
Get started
Yandex Cloud Functions
  • Getting started
    • Overview
    • Creating and executing functions
    • Creating 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 IoT Core
  • Step-by-step instructions
    • All instructions
    • Using functions to get an IAM token for a service account
    • Managing rights to access functions
    • Managing functions
      • Creating a function
      • Managing function versions
      • Working in the code editor
      • Invoking a function
      • Updating a function
      • Viewing monitoring charts
      • Viewing the execution log
      • Deleting a function
    • Managing triggers
      • Getting information about a trigger
      • Creating 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 Yandex IoT Core
      • Updating a trigger
      • Deleting a trigger
  • Concepts
    • Overview
    • Function
    • Invoking a function
    • Runtime
      • Overview
      • Environment
      • Execution context
    • Builder
    • Trigger
      • Overview
      • Timer
      • Trigger for Message Queue
      • Trigger for Object Storage
      • Trigger for Container Registry
      • Trigger for Cloud Logs
      • Trigger for Yandex IoT Core
    • Dead Letter Queue
    • Log groups
    • Monitoring
    • 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
  • Use cases
    • Creating skills for Alice
  • 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
        • listTagHistory
        • listVersions
        • removeTag
        • setAccessBindings
        • 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. Managing triggers
  3. Creating a trigger for Message Queue

Creating a trigger for Message Queue

  • Before you start
  • Create a trigger
  • Check the results

Create a trigger for the message queue of Yandex Message Queue and process messages using the function Cloud Functions.

Warning

  • The trigger must be in the same cloud as the queue that messages are read from.
  • Only one trigger can be created for each message queue.

Before you start

To create a trigger, you need:

  1. Functions the trigger will send messages to for processing. If you don't have a function:
    • Create a function.
    • Create a function version.
  2. A message queue that the trigger will pick up messages from. If you don't have a queue, create one.
  3. Service accounts with rights to read messages from a message queue and to call a function. You can use the same service account or different ones. If you don't have a service account, create one.

Create a trigger

Note

The trigger is initiated within 5 minutes of being created.

Management console
CLI
API
  1. In the management console, go to the folder where you want to create a trigger.
  2. Open Cloud Functions.
  3. Go to the Triggers tab.
  4. Click Create trigger.
  5. Under Basic parameters:
    • Enter a name and description for the trigger.
    • In the Type field, select Message Queue.
  6. Under Message Queue settings, select a message queue and a service account with rights to read messages from it.
  7. Under Batch message settings, specify the message batch size and the maximum waiting time. The trigger will send the batch of messages to the function when the number of messages in the queue reaches the specified batch size or the maximum waiting time expires.
  8. Under Function settings:
    • Select the function for the trigger to call.
    • Specify the function version tag.
    • Specify the service account to be used to call the function.
  9. 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, run the command:

yc serverless trigger create message-queue \
    --name <trigger name> \
    --queue <queue ID> \
    --queue-service-account-id <service account ID> \
    --invoke-function-id <function ID> \
    --invoke-function-service-account-id <service account ID> \
    --batch-size 1 \
    --batch-cutoff 10s

where:

  • --name: Trigger name.
  • --queue: Queue ID.
    To find out the queue ID:
    1. In the management console, go to the folder where the queue is located.
    2. Open Yandex Message Queue.
    3. Select the desired queue.
    4. You can see the queue ID under General information in the ARN field.
  • --invoke-function-id: Function ID.
  • --queue-service-account-name: Service account with rights to read messages from the queue.
  • --invoke-function-service-account-id: Service account with rights to call the function.
  • --batch-size: Message batch size. Values can be from 1 to 10. The default is 1.
  • --batch-cutoff: Maximum waiting time. Values can be from 0 to 20 seconds. The default is 10 seconds. The trigger will send the batch of messages to the function when the number of messages in the queue reaches the batch-size or the batch-cutoff expires.

Result:

id: dd0cspdch6dslu9n3i5g
folder_id: aoek49ghmknnpj1ll45e
created_at: "2019-08-28T12:14:45.762915Z"
name: ymq-trigger
rule:
  message_queue:
    queue_id: yrn:yc:ymq:ru-central1:aoek49ghmknnpj1ll45e:my-mq
    service_account_id: bfbqqeo6jkpls2tse5o6
    batch_settings:
      size: "1"
      cutoff: 10s
    invoke_function:
      function_id: b09e5lu91ta21vdrrgma
      function_tag: $latest
      service_account_id: bfbqqeo6jkpls2tse5o6  
status: ACTIVE      

You can create a trigger for Yandex Message Queue using the create API method.

Check the results

Functions
Message Queue

Make sure the trigger is working properly. To do this, view the function logs that show information about its invocations.

Check that the number of enqueued messages is decreasing. To do this, view the queue statistics:

  1. Open Message Queue.
  2. Select the queue that you created the trigger for.
  3. Go to Monitoring. View the Messages in queue chart.
In this article:
  • Before you start
  • Create a trigger
  • Check the results
Language
Careers
Privacy policy
Terms of use
© 2021 Yandex.Cloud LLC