Creating a trigger for Yandex IoT Core
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.
Warning
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:
- A service account with rights for function call. If you don't have a service account, create one.
- Registries or device whose topics the trigger will accept copies of messages from. If you don't have either:
- 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.
Note
The trigger is initiated within 5 minutes of being created.
To create a trigger:
- In the management console, select the folder where you want to create a trigger.
- Select Cloud Functions.
- In the left part of the window, select Triggers.
- Click Create trigger.
- In the Name field, enter the trigger name.
- In the Type drop-down list, select
Yandex IoT Core
. - Under Yandex IoT Core message settings, enter the parameters previously set for your registry and device:
- Registry: Select the registry with the desired device.
- Device: Select the desired device.
- MQTT topic: Enter the topic to create a trigger for.
- Under Function settings, enter the parameters previously set for your function:
- Function: Select the desired function.
- Service account: Select the service account to perform the function for.
- Click "Create trigger".
The trigger is created and displayed in the table's Triggers section.
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.
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 <trigger name> \
--registry-id <registry ID> \
--device-id <device ID> \
--mqtt-topic '$devices/<device ID>/events' \
--invoke-function-id <function ID> \
--invoke-function-service-account-id <service account ID>
Result:
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
status: ACTIVE
Check the results
Check the function logs to see if the function was run:
-
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 <function ID>
Result:
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