Triggers

Triggers is currently at the Preview stage.

Triggers are conditions that automatically launch a specific function when met. These let you automate work with other Yandex.Cloud services.

You can set up the following types of triggers at the Preview stage:

Triggers for Message Queue

The Message Queue trigger is used to unload a message queue. It takes messages from the queue and passes them to a function for processing. 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.

Message Queue triggers require service accounts to read messages from a message queue and to call a function. You can use the same service account for both operations.

Roles required for the proper operation of a trigger for Message Queue

  • To create a trigger, you need the following roles:
    • viewer for the folder with the message queue that the trigger reads messages from.
    • viewer for the folder with the function that the trigger calls.
    • iam.serviceAccounts.user for the service accounts on whose behalf the trigger performs operations.
  • To run a trigger, service accounts need the following roles:
    • viewer for the folder with the message queue that the trigger reads messages from.
    • serverless.functions.invoker for the folder with the function that the trigger calls.

Triggers for Yandex IoT Core

The trigger for Yandex IoT Core is designed for managing messages exchanged between devices and registries. It's created for topics: it takes copies of messages from them and passes them to the function for processing. The trigger must be in the same cloud as the device or register as the topic it reads messages from.

Triggers can be created:

A trigger for Yandex IoT Core needs a service account to call the function.

Roles required for the proper operation of a trigger for Yandex IoT Core

To run a trigger, the service account needs the serverless.functions.invoker role for the folder containing the function called by the trigger.

Read more about access management.

Triggers call message processing functions based on preset quotas and limits.

Specifics of functions invoked by triggers

When a function is called by a trigger, the following specifics apply:

  • Functions called by triggers are always called with the integration=raw query string parameter. More about invoking functions.
  • Before passing messages to the function, the trigger converts messages to a specific format.

Incoming function message formats

Triggers message functions in the modified format defined for each trigger:

Trigger message format Message Queue

After the trigger receive a message from the queue, it passes it to the function in the following format:

{
   "messages":[
      {
         "event_metadata":{
            "event_id":"cce76685-5828-4304-a83d-95643c0507a0",
            "event_type":"yandex.cloud.events.messagequeue.QueueMessage",
            "created_at":"2019-09-24T00:54:28.980441Z"
         },
         "details":{
            "queue_id": "yrn:yc:ymq:ru-central1:21i6v06sqmsaoeon7nus:event-queue",
            "message":{
               "message_id":"cce76685-5828-4304-a83d-95643c0507a0",
               "md5_of_body":"d29343907090dff4cec4a9a0efb80d20",
               "body":"message body",
               "attributes":{
                  "SentTimestamp":"1569285804456"
               },
               "message_attributes":{
                  "messageAttributeKey":{
                     "dataType":"StringValue",
                     "stringValue":"value"
                  }
               },
               "md5_of_message_attributes":"83eb2d0afefb150c1ffe69f66c3de068"
            }
         }
      },
      {
         "event_metadata":{
            "event_id":"1f32fd25-11fc-4c08-88e7-d871d6ffb91d",
            "event_type":"yandex.cloud.events.messagequeue.QueueMessage",
            "created_at":"2019-09-24T00:54:28.980492Z"
         },
         "details":{
            "queue_id": "yrn:yc:ymq:ru-central1:21i6v06sqmsaoeon7nus:event-queue",
            "message":{
               "message_id":"1f32fd25-11fc-4c08-88e7-d871d6ffb91d",
               "md5_of_body":"d29343907090dff4cec4a9a0efb80d20",
               "body":"message body",
               "attributes":{
                  "SentTimestamp":"1569285806456"
               },
               "message_attributes":{
                  "messageAttributeKey":{
                     "dataType":"StringValue",
                     "stringValue":"value"
                  }
               },
               "md5_of_message_attributes":"83eb2d0afefb150c1ffe69f66c3de068"
            }
         }
      }
   ]
}

Trigger message format Yandex IoT Core

After the trigger copies the message from the topic, it passes it to the function in the following format:

{
    "messages": [
        {
            "event_metadata": {
                 "event_id": "2153b5d2-c6af-4c69-a28d-74ce965b7613",
                 "event_type": "yandex.cloud.events.iot.IoTMessage",
                 "created_at": "2019-09-25T15:51:17.872320525Z"
            },
            "details": {
                 "registry_id": "arenou2oj4ct42eq8g3n",
                 "device_id": "areqjd6un3afc3cefcvm",
                 "mqtt_topic": "$devices/areqjd6un3afc3cefcvm/events",
                 "payload": "VGVzdCA0"
            }
        },
        {
            "event_metadata": {
                 "event_id": "2153b5d2-c6af-4c69-a28d-74ce965b7613",
                 "event_type": "yandex.cloud.events.iot.IoTMessage",
                 "created_at": "2019-09-25T15:51:17.872320525Z"
            },
            "details": {
                 "registry_id": "arenou2oj4ct42eq8g3n",
                 "device_id": "areqjd6un3afc3cefcvm",
                 "mqtt_topic": "$devices/areqjd6un3afc3cefcvm/events",
                 "payload": "VGVzdCA0"
            }
        }
    ]
}