Creating a trigger

Triggers is currently at the Preview stage.

Create a trigger for the device topic or Yandex IoT Core service registry, and process message copies using a function Cloud Functions.

Important

The trigger must be in the same cloud with the registry or device it reads messages from.

Before you start

To create a trigger, you need:

  1. A service account with rights for function call. If you don't have a service account, create one.
  2. A registry or device whose topics the trigger will accept copies of messages from. If you don't have either:
  3. Functions the trigger will send messages to for processing. If you don't have a function:

Create a trigger

Create a trigger for the device topic.

If you don't have the Yandex.Cloud command line interface yet, install 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.

Create a trigger:

  • --name: Trigger name.
  • --registry-id: A unique registry ID.
  • --device-id: The unique device ID. If you are creating a trigger for a registry topic, you can skip this parameter.
  • --mqtt-topic: The topic you want to create a trigger for.
  • --invoke-function-id: Unique function ID.
  • --invoke-function-service-account-id: Service account with rights to call the function.
$ yc serverless trigger create internet-of-things 
    --name iot-trigger \
    --registry-id arenou2oj4ct42eq8g3n \
    --device-id areqjd6un3afc3cefcvm \
    --mqtt-topic '$devices/areqjd6un3afc3cefcvm/events' \
    --invoke-function-id d4eofc7n0m03lmudse8l \
    --invoke-function-service-account-id aje3932acd0c5ur7dagp
id: a1sl0mkmimfj3uv52fr8
folder_id: b1g88tflru0ek1omtsu0
created_at: "2019-09-25T13:54:35.654935Z"
name: iot-trigger
rule:
  iot_message:
    registry_id: arenou2oj4ct42eq8g3n
    device_id: areqjd6un3afc3cefcvm
    mqtt_topic: $devices/areqjd6un3afc3cefcvm/events
    invoke_function:
      function_id: d4eofc7n0m03lmudse8l
      function_tag: $latest
      service_account_id: aje3932acd0c5ur7dagp
      retry_settings:
        retry_attempts: "1"
        interval: 10s   

Check the results

Check the function logs to see if the function was run:

  1. The function logs should contain information about the function call. In this example, the function outputs information about the messages processed.

    $ yc serverless function logs b09e5lu91ta21vdrrgma
    
    2019-09-25 14:00:00     MESSAGE_BATCH
    2019-09-25 14:00:00     {
    2019-09-25 14:00:00       "event_metadata": {
    2019-09-25 14:00:00         "event_id": "9e016299-866f-4900-8830-d674cbff3577",
    2019-09-25 14:00:00         "event_type": "yandex.cloud.events.iot.IoTMessage",
    2019-09-25 14:00:00         "created_at": "2019-09-25T13:59:59.208724628Z"
    2019-09-25 14:00:00       },
    2019-09-25 14:00:00       "details": {
    2019-09-25 14:00:00         "registry_id": "arenou2oj4ct42eq8g3n",
    2019-09-25 14:00:00         "device_id": "areqjd6un3afc3cefcvm",
    2019-09-25 14:00:00         "mqtt_topic": "$devices/areqjd6un3afc3cefcvm/events",
    2019-09-25 14:00:00         "payload": "VGVzdCA0"
    2019-09-25 14:00:00       }
    2019-09-25 14:00:00     }
    2019-09-25 14:00:00     END RequestID: 1ba57cbe-c2ce-4d1a-91a3-9592e945b45a
    2019-09-25 14:00:00     REPORT RequestID: 1ba57cbe-c2ce-4d1a-91a3-9592e945b45a Duration: 368.362 ms Billed Duration: 400 ms Memory Size: 128 MB Max Memory Used: 13 MB