Create a Function

  1. Under Functions, click Workers.
  2. Click New Worker.

  3. Enter the Function's name and description (optional).

    Figure 1.
  4. Create your own Serverless Function by coding it. Currently, you can do this using Python or Go by choosing the appropriate language template.

    Each template gives you a basic structure to find a single Function within which all the tasks must run. This is your main Function, and everything starts and ends with it. You can (and probably should) use auxiliary Functions to refactor your code, but make sure the main Function is there. Please don't rename it or change its definition.

    This main Function has some input parameters that will give you the context of the HTTP request made to invoke the function:

    • Path
    • Method (POST, GET, PUT, PATCH, or DELETE)
    • Headers
    • Query Parameters
    • Body

    This information is the data you can provide to your Function.

    When the task of your Function has finished, return some result or feedback in your response. For this you can:

    • Set a status code
    • Use the response body
    • Add headers to the response

    The details on how to get the request data and how to set the response info is language and template specific, so check the documentation about the template you want to use to build your Function:

    • Go template documentation
    • Python template documentation
  5. Select the appropriate template (GO or Python 3).
    The code appears in the panel.

    Figure 2.
  6. To have your function be subscribed to an Event Trigger so that you can capture events and messages from different sources and redirect them to your Function, under Event Trigger, click Add Topic and enter the MQTT topic.

    Figure 3.
  7. To schedule your functions’ invocation, enter a Cron Expression in the field.
    Check here for Cron expressions requirements.
  8. Click Save.