fun local, as a subcommand of Funcraft, can fully simulate the function in the Function Compute locally and provide the single-step debugging function. This makes up for the shortcomings of the Function Compute relative to the traditional application development experience and provides you with a new way to troubleshoot Function Compute problems.

Notice The content described in this article will no longer be maintained later. If your Function Compute resources are managed by Funcraft, we recommend that you migrate the resources to Serverless Devs.

For more information about how to migrate Function Compute-related resources from Funcraft to Serverless Devs, see Migrate resources from Funcraft to Serverless Devs.

For more information about Serverless Devs, see Overview.

For more information about how to use Serverless Devs to debug functions, see Debug Functions and Interconnection between on-premises and cloud applications Using s Local Related Commands.

We apologize for any inconvenience caused.

Background information

fun local provide fun local invoke and fun local start two subcommands:
  • fun local invoke supports running and debugging event functions locally.
  • fun local start supports running and debugging HTTP trigger functions and event functions locally:
    • When debugging an HTTP trigger function using a fun local start run, it is allowed to directly trigger the function to run through a browser.
    • When you use a fun local start to run a debug event function, you are allowed to call the function to run through InvokeFunction API or SDK.

The fun local has been integrated into graphical plug-ins of IDE such as VSCode, IDEA, and Pycharm. Compared with command lines, plug-ins can bring better use experience through graphical methods.

Help information

You can execute the following different commands to obtain help information about related commands.

  • fun local invoke help information.
    fun local invoke -h
    Expected output.
    Usage: fun local invoke [options] <[service/]function>
    
    Execute your function in a local environment which replicates the live environment
    almost identically. You can pass in the event body via stdin or by using the -e (--event)
    parameter.
    
    Options:
      -t, --template [template]       The path of fun template file.
      -c, --config <ide/debugger>     Select which IDE to use when debugging and output related debug config tips for the
                                      IDE. Options:'vscode', 'pycharm'
      -e, --event <event>             Support Event data(strings) or a file containing event data passed to the function
                                      during invocation.
      -f, --event-file <path>         A file containing event data passed to the function during invoke.
      -s, --event-stdin               Read from standard input, to support script pipeline.
      -d, --debug-port <port>         Specify the sandboxed container starting in debug mode, and exposing this port on localhost
    
      --no-reuse                      Do not reuse the container which was started by the 'fun local start
                                      {service}/{function}' command.
      --tmp-dir <tmpDir>              The temp directory mounted to /tmp , default to
                                      './.fun/tmp/invoke/{service}/{function}/'
      --debug-args <debugArgs>        additional parameters that will be passed to the debugger
      --debugger-path <debuggerPath>  the path of the debugger on the host
    
      -h, --help                      display help for command
  • fun local start help information.
    fun local start -h
    Expected output.
    Usage: fun local start [options] <[service/]function>
    
    
        Allows you to run the Function Compute applicatoin locally for quick development & testing.
        It will start a http server locally to receive requests for http triggers and apis.
        It scans all functions in template.yml. If the resource type is HTTP, it will be registered to this http server, which can be triggered by the browser or any http tools.
        For other types of functions, they will be registered as apis, which can be called by sdk in each language or directly via api.
    
        Function Compute will look up the code by CodeUri in template.yml.
        For interpreted languages, such as node, python, php, the modified code will take effect immediately without restarting the http server.
        For compiled languages such as java, we recommend you set CodeUri to the compiled or packaged location.
        Once compiled or packaged result changed, the modified code will take effect immediately without restarting the http server.
    
    Options:
      -t, --template [template]       The path of fun template file.
      -d, --debug-port <port>         Specify the sandbox container starting in debug mode, and exposing this port on
                                      localhost
      -c, --config <ide/debugger>     Select which IDE to use when debugging and output related debug config tips for the
                                      IDE. Options:'vscode', 'pycharm'
      --debugger-path <debuggerPath>  The path of the debugger on the host
      --debug-args <debugArgs>        Additional parameters that will be passed to the debugger
      -h, --help                      display help for command

References

  • fun local invoke access to services on the host
    If you are using Docker-for-Mac or Docker-for-Windows 18.03 +, you only need to use host.docker.internal as IP to access your local service. The following table provides examples of the required parameters and supported syntax.
    client = fc2.Client(endpoint='http://host.docker.internal:8000', accessKeyID='xxx', accessKeySecret='xxx')
    resp = client.invoke_function('localdemo', 'nodejs8', json.dumps({'code': 123}))
    If the Docker version is less than 18.03, run commands on the host based on different operating systems to view the IP address of the host, and then configure the IP address of the host to the endpoint.
    • Linux or macOS: ifconfig
    • Windows operating system: ipconfig
  • Environment variables

    The environment variables configured in the template.yml file will be the same as the online behavior. When the function is run time, it can be obtained through code. For more information about environment variables, see Environment variables.

    You can use environment variables to distinguish whether a function is running locally or online. This allows you to perform some special logical processing. For more information, see EnvironmentVariables.

  • Credentials

    You can access other Alibaba Cloud services by using the AccessKey information stored in Credentials. The policy for finding the AccessKey information when the fun local runs the function locally is the same as that of the fun deploy.

    The following is an example of using the Credentials provided by the function to configure an OSS client based on the local and online environments.
    local = bool(os.getenv('local', ""))
    if (local):
        print 'thank you for running function in local!!!!!!'
        auth = oss2.Auth(creds.access_key_id,
                         creds.access_key_secret)
    else:
        auth = oss2.StsAuth(creds.access_key_id,
                            creds.access_key_secret,
                            creds.security_token)