Yandex Cloud
  • Services
  • Solutions
  • Why Yandex Cloud
  • Blog
  • Pricing
  • Documentation
  • Contact us
Get started
Language / Region
Yandex project
© 2023 Intertech Services AG
Yandex Cloud Functions
  • Comparison with other Yandex Cloud services
  • Getting started
  • Step-by-step guides
    • All guides
    • Using functions to get an IAM token for a service account
    • Connecting to managed databases from functions
    • Getting information about a function
    • Managing rights to access functions
    • Creating a function
    • Invoking a function
    • Managing functions
    • 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
  • Concepts
  • Developing in Node.js
  • Developing in Python
  • Developing in Go
  • Developing in PHP
  • Developing in Bash
  • Developing in Java
  • Developing in R
  • Developing in C#
  • Tutorials
  • Pricing policy
  • Access management
  • API Functions reference
  • API Triggers reference
  • FAQ
  1. Step-by-step guides
  2. Creating a trigger
  3. Creating a trigger for Yandex IoT Core

Creating a trigger for Yandex IoT Core that passes messages to the Cloud Functions function

Written by
Yandex Cloud
,
improved by
amatol
  • Getting started
  • Creating a trigger
  • Checking the result
  • See also

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

Warning

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

Getting started

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.

  • Registry or device from whose topics the trigger will receive copies of messages. If you have neither:

    • Create a registry.
    • Create a device.

Creating a trigger

Note

The trigger is initiated within 5 minutes of being created.

Management console
CLI
Terraform
API
  1. In the management console, select the folder where you want 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 Yandex IoT Core.
    • In the Launched resource field, select Function.
  6. Under Yandex IoT Core message settings, specify the registry, device, and MQTT topic to create a trigger for. If you are creating a trigger for a registry topic, you don't need to specify a device or an MQTT topic.

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

    • Tag of the function version.
    • A service account to be used to invoke the function.
  8. (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.
  9. (optional) Under Dead Letter Queue settings, select the Dead Letter Queue and the service account with write privileges for this queue.

  10. 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 this command:

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> \
  --retry-attempts 1 \
  --retry-interval 10s \
  --dlq-queue-id <Dead Letter Queue ID> \
  --dlq-service-account-id <service account ID>

Where:

  • --name: Trigger name.
  • --registry-id: Registry ID.
  • --device-id: Device ID. If you're creating a trigger for a registry topic, you can omit this parameter.
  • --mqtt-topic: The topic you want to create a trigger for.
  • --invoke-function-id: Function ID.
  • --invoke-function-service-account-id: Service account with rights to invoke the function.
  • --retry-attempts: Number of invocation retries before the trigger moves a message to the Dead Letter Queue. This is an optional parameter. You can specify values from 1 to 5, 1 being default.
  • --retry-interval: Time after which the function will be invoked again if the current attempt fails. This is an optional parameter. You can specify values from 10 to 60 seconds, 10 being default.
  • --dlq-queue-id: Dead Letter Queue ID. This is an optional parameter.
  • --dlq-service-account-id: Service account with the permission to write messages to the Dead Letter Queue. This is an optional parameter.

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

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 do not have Terraform yet, install it and configure the Yandex Cloud provider.

To create a trigger for Yandex IoT Core:

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

    Example of the configuration file structure:

    resource "yandex_function_trigger" "my_trigger" {
      name        = "<trigger name>"
      description = "<trigger description>"
      iot {
        registry_id = "<registry ID>"
        device_id   = "<device ID>"
        topic       = "<topic ID>"
      }
      function {
        id                 = "<function ID>"
        service_account_id = "<service account ID>"
      }
    }
    

    Where:

    • name: Trigger name. The name format is as follows:

      • 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.

    • iot: Topic parameters:

      • registry-id: Registry ID.
      • device-id: Device ID. If you're creating a trigger for a registry topic, you can omit this parameter.
      • topic: ID of the topic you want to create a trigger for.
    • 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 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 this command:

      terraform plan
      

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

  3. Deploy cloud resources.

    1. If the configuration does not contain any errors, run this command:

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

      Once you are done, all the resources you need will be created in the specified folder. You can verify that the resources are there and their configuration is correct using the management console or the following CLI command:

      yc serverless trigger get <trigger ID>
      

To create a trigger for Yandex IoT Core, use the create REST API method for the Trigger resource or the TriggerService/Create gRPC API call.

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 Yandex IoT Core that sends messages to the Serverless Containers container.

Was the article helpful?

Language / Region
Yandex project
© 2023 Intertech Services AG
In this article:
  • Getting started
  • Creating a trigger
  • Checking the result
  • See also