IoT Function as a Service Kickstart

IoT Kickstart Overview

An IoT Kickstart is a Scaleway instance providing an application for a typical IoT use case.

Scaleway will spawn, install and configure the instance for you with the required software so messages flowing through your IoT Hub end up in the application.

IoT Function as a Service Kickstart Overview

The function as a service Kickstart provides a FaaS engine to trigger functions upon receiving messages on your IoT Hub.

It will setup and configure Nuclio on a Scaleway instance.

Creating Kickstart

First go to the Kickstarts page in your Scaleway console and click the + button, then :

  • Select the Kickstart purpose.
  • Choose the Hub to connect to, if you have not yet created a Hub please follow these instructions.
  • Choose the Device to connect to, if you have not yet added a device to your Hub please follow these instructions. Please note the Device should allow insecure connections. The reason for this is we don’t keep the Device certificates we provide you when devices are created, so we can’t provide the device certificate to the Kickstart to establish a secure, TLS mutually authenticated, connexion to your Hub. Device not allowing insecure connections won’t be listed here.
  • Choose a region and name for the Scaleway instance we will spawn for you.
  • Choose a password for the Kickstart application dashboard (user name is always admin)
  • Create the Kickstart and go get a coffee while it is being set up (this may take a while). You will know the Kickstart is up and running when the Dashboard link (on the Kickstart list page) will open the application dashboard. Use the credentials provided previously.

Using Kickstart

A first example function will be pre-configured for you. It sends a message to a topic named events when triggered and is configured to be triggered when a message is published to any topic under please/react/to/ on your IoT Hub.

You can test it pretty easily :

  • find your IoT Hub endpoint in Hub overview page in your Scaleway console. Let’s call it ENDPOINT.
  • add 2 more devices to the IoT Hub you connected the Kickstart to (allow insecure connections, for the sake of simplicity). Let’s call their IDs ID_A and ID_B.
  • Use device A to subscribe to events topic: mosquitto_sub -h ENDPOINT -i ID_A -t events.
  • Use device B to publish a message to please/react/to/hello/world topic: mosquitto_pub -h ENDPOINT -i ID_B -t please/react/to/hello/world -m "Hi there.".

You should have received a message on device A with the following payload: Received a message on please/react/to/hello/world (length: 9). This message was generated by the example function running in Nuclio.

That’s it ! You’re done creating and using the Kickstart. Next we’ll see how to tweak Kickstart to betted fit your needs.

Tweaking Kickstart

Now you’ve seen how to use IoT function as a service Kickstart, here is how to go further.

Processing and sending messages

The function which will handle your messages will receive 2 arguments:

  • events.url: the topic the message was published to
  • events.body: the message payload

When the function is done processing the message, it may return a message to be published using the following parameters:

  • response status code: 200
  • response body: the message payload (any data)
  • response header X-MQTT-Topic: the topic to publish message to (any valid topic string)
  • response header X-MQTT-Retain: whether to set the retain flag on the message (boolean value) (optional, default: false)

Triggering the function

Nuclio connects a MQTT client to the IoT Hub and subscribes to a topic. When a message is received, it runs the function against it and, if requested by the function, publishes back a message on the IoT Hub.

Because of the way triggers are implemented in Nuclio, it spawns one MQTT client per function. This means you need to add an IoT Device to your IoT Hub for each function.

To create the trigger for your function:

  • Go to the trigger tab in your function editor in Nuclio dashboard.
  • Select MQTT as trigger class
  • Enter as URL (this may vary, refer to the IoT Hub overview page in your Scaleway console)
  • Enter your device ID as Username, no password required
  • Create a new subscription
  • Choose the topic (MQTT topics documentation) and QoS level (MQTT QoS documentation)
  • Put anything you want in trigger name and worker allocator name
  • Deploy your function

Learn more about Scaleway IoT Hub, discover how to add Devices to the hub, check the IoT Hub metrics or learn how to interact with other parts of the Scaleway Elements ecosystem by setting up IoT Hub Routes.

Discover the Cloud That Makes Sense