Create a Function

  1. Under User Functions, click Functions.
  2. Click New Function.


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


    Figure 1.
  4. From the Template drop down menu, choose the type of Function you want to code:
    • FMU: Upload an FMU file to run a simulation model
    • Go: Code your serverless Function using Go language
    • Python: Code your serverless Function using Python language
  5. If you choose to run a simulation model by using a FMU, upload the FMU file and click Save. Go to Step 9.
  6. If you choose to code your serverless function (either using Go or Python language), the libraries drop-down displays the available libraries you can use while coding. To use any library, select it from the list provided.
    Note: This action will allow you to add it to your template, but you will still need to add the import line to your code.


    Figure 2.
    The code appears in the panel.


    Figure 3.

Set the Environment Variable

Environment variables allow defining dynamic values that are external to the code but are accessible to the function during runtime. These variables are set outside the function code and can be modified without changing the code itself, providing flexibility and security. They are commonly used to store configuration settings, sensitive data or credentials, or any other information that the function may need to operate.

  1. Click + Add Environment variable.
    Figure 4.
  2. If you are using environment variables, include the following in your code:
    • Python:
      import os
      var_name = os.environ["var_name"]
      
    • GO:
      package function
        
      import (
        "io/ioutil"
        "net/http"
        "os"
      )
         
      // Handle a function invocation
      func Handle(w http.ResponseWriter, r *http.Request) {
          varName := os.Getenv("var_name")
         
          // ...
      }
      
    Note: The environment variables are type string, so include the appropriate changes if you want to define them as any other type (number, object…)

Add an Event Trigger

Under Event Trigger, you can define the Event or Action that will initiate the execution of your Function by using MQTT topics or by scheduling the invocation of the function.
Note: As the platform offers its own MQTT broker, all the data sent to the platform (even if it is by HTTP method) is republished to the corresponding MQTT topics.

Wildcards can be used for subscribing to multiple MQTT topics simultaneously. There are two types of wildcards:

  • single-level: Represented by a symbol (+). By subscribing to a topic with a single-level wildcard, the client receives all messages from any topic that contains an arbitrary string in place of the wildcard.
  • multi-level: Represented by a hash symbol (#) and must be placed as the last character in the topic, preceded by a forward slash. By subscribing to a topic with a multi-level wildcard the client receives all messages of any topic that begins with the pattern before the #.


Figure 5.
  1. Click + Add Topic to define the MQTT endpoint topic that will trigger the invocation of the Function.
  2. Enter a Cron Expression if you want to schedule the Function's invocation. Check here for Cron expressions requirements.
  3. Click Save to start building your Function.
    The Function will display in the panel with status Pending, which means it has been created but is waiting to be built. The status of your Function will be updated to a Building status while the image is being built (could take 1 - 2 minutes) and, eventually, it will become Running, which means the Function has been deployed and should be available for invocation.

    If something goes wrong and building process fails, your Function status will be updated to Failed.

    Note: A Running Function doesn't mean everything will work as the code itself is not being debugged.