Before deploying an application to Function Compute, you can use Fun to test or step through the application on your computer. For example, you can use Fun to check whether the application runs as expected, find errors, and fix them.

The debug mode of Fun allows you to attach a debugger to the target function. Then you can step through the code of the function, view variable settings, and fix errors found.

Start and stop the local mock Link IoT Edge environment

Fun provides a local mock Link IoT Edge environment that behaves similar to the officially released Link IoT Edge. You can test and debug your application in the local mock Link IoT Edge environment before deploying the application to Function Compute.

  • Start the local mock Link IoT Edge environment
    Run the following command on your computer where Node.js and Docker are installed:
    fun edge start
    Note
    • If this is not the first time you run the fun edge start command, the command directly starts the local mock Link IoT Edge environment.
    • If this is the first time you run the fun edge start command, the command builds a local mock Link IoT Edge environment on your computer. Enter certificate information about the gateway device as prompted. The command output is as follows:
      $ fun edge start
      No local container found, try to create one...
      linkedge_2019_01_21: Pulling from aliot-linkedge/linkedge-alpine
      Status: Image is up to date for reg.docker.alibaba-inc.com/aliot-linkedge/linkedge-alpine:linkedge_2019_01_21
      
      Please enter secret triples for setup:
      ? Product key <Gateway Product Key>
      ? Device name <Gateway Device Name>
      ? Device secret <Gateway Device Secret>
      Applying secret triples...
      Create container with name linkedge-debug.
      Start container linkedge-debug.
  • Stop the local mock Link IoT Edge environment
    Run the following command on your computer where Node.js and Docker are installed:
    fun edge stop

Run a function

You can run the fun edge invoke command with a function name and an event file name specified to call and run the function in the local mock Link IoT Edge environment on your computer.
# Invoking function with event file
$ fun edge invoke helloworld/helloworld -e event.json

# Invoking function with event via stdin
$ echo '{"message": "Hello?" }' | fun edge invoke helloworld/helloworld

# For more options
$ fun edge invoke --help
Note If you neither specify any event file names in the fun edge invoke command nor use a pipe (|) to specify event parameters for the command, Fun enters the state of waiting for reading the standard input. You can directly enter the event content on the command line, press Enter, and then press Ctrl+D to end the input.
The following animation illustrates how to develop and run a function in Visual Studio (VS) Code.Develop and run a function in Visual Studio Code

Debug a function

To step through a function, you can run the fun edge invoke command with the debug parameter specified to start the function in debug mode.

Then you can attach a debugger to the function.
fun edge invoke --debug helloworld/helloworld
Note If you want to obtain the configuration of the debugger such as VS Code, specify the --output-debugger-configs parameter in the fun edge invoke command.
$ fun edge invoke helloworld/helloworld --debug --output-debugger-configs --event event.json 
Using template file at /private/tmp/helloworld/template.yml.
Invoking function helloworld/helloworld (nodejs8).

Debugging Configurations
## VS Code ##
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach to Fun (Node.js 8)",
            "type": "node",
            "request": "attach",
            "address": "localhost",
            "port": 5700,
            "localRoot": "/private/tmp/helloworld",
            "remoteRoot": "/tmp/var/run/functions/helloworld/helloworld",
            "protocol": "inspector",
            "stopOnEntry": false
        }
    ]
}
  • Node.js
    To use VS Code to debug a Node.js function started by Fun, follow these steps:
    1. Go to Command Palette in VS Code and run the Shell Command: Install 'code' command in PATH command to install the code command. Then you can run the following command in the application directory of the function to start a VS Code instance:
      code .
      The following animation illustrates how to debug a Node.js function in VS Code.Debug a function in Node.js
    2. Use the following debugger configuration:
      {
          "version": "0.2.0",
          "configurations": [
              {
                  "name": "Attach to Fun (Node.js 8)",
                  "type": "node",
                  "request": "attach",
                  "address": "localhost",
                   "port": 5700,
                   // app dir is the directory of the app code.
                  "localRoot": "${workspaceRoot}/{app dir}",
                   // service name and function name are the ones in template.yml respectively.
                  "remoteRoot": "/tmp/var/run/functions/${service name}/${function name}",
                  "protocol": "inspector",
                  "stopOnEntry": false
              }
          ]
      }
      The following table describes some parameters in the debugger configuration.
      Parameter Description
      localRoot Set this parameter to the local directory specified by CodeUri in the template.yml file. If the value of CodeUri is a file name, set this parameter to the directory where the file is located. Replace the ${workspaceRoot} variable in the preceding configuration with the directory where the .vscode file is located.
      remoteRoot Set this parameter to the directory for storing the code of the function in the local mock Link IoT Edge environment. Replace the ${service name} and ${function name} variables in the preceding configuration with the actual service name and function name, respectively.

    You can run the invoke command with the --output-debugger-configs parameter specified to obtain the debugger configuration.

  • Python
    When you use VS Code to debug a Python function started by Fun, you can use the following debugger configuration:
    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Attach to Fun (Python 3)",
                "type": "python",
                "request": "attach",
                "host": "localhost",
                "port": 5700,
                "pathMappings": [
                    {
                          // app dir is the directory of the app code.
                          "localRoot": "${workspaceRoot}/{app dir}",
                          // service name and function name are the ones in template.yml respectively.
                        "remoteRoot": "/tmp/var/run/functions/{service name}/{function name}"
                    }
                ]
            }
        ]
    }