简介

更新时间: 2024-02-05 16:40:07

fun local作为Funcraft的一个子命令,可以将函数计算中的函数在本地完全模拟运行,并提供单步调试的功能,弥补了函数计算相对于传统应用开发体验上的短板,为您提供了一种解决函数计算问题排查的新途径。

重要

本文介绍的内容后期将不再维护。如果您的函数计算资源是使用Funcraft管理的,建议您将资源迁移至Serverless Devs管理。

关于如何将函数计算的相关资源从Funcraft迁移到Serverless Devs进行管理的详细操作,请参见从Funcraft迁移到Serverless Devs

关于Serverless Devs的详细信息,请参见什么是Serverless Devs

关于如何使用Serverless Devs调试函数,请参见使用s local的相关命令调试函数端云联调简介

由此带来的不便,敬请谅解!

背景信息

fun local提供fun local invokefun local start两个子命令:

  • fun local invoke支持本地运行调试事件函数。

  • fun local start支持本地运行调试HTTP触发器函数及事件函数:

    • 当使用fun local start运行调试HTTP触发器函数时,允许通过浏览器直接触发函数运行。

    • 当使用fun local start运行调试事件函数时,允许通过InvokeFunction API或者SDK进行调用函数运行。

fun local已集成到VSCode、IDEA及PyCharm等IDE的图形化插件中,相较于命令行,插件通过图形化的方式可以带来更好的使用体验。

帮助信息

您可以执行以下不同的命令,获取相关命令的帮助信息。

  • fun local invoke的帮助信息。

    fun local invoke -h

    预期输出:

    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的帮助信息。

    fun local start -h

    预期输出:

    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

相关信息

  • fun local invoke访问宿主机上的服务

    如果您使用的是Docker-for-Mac或Docker-for-Windows 18.03+,只需使用host.docker.internal作为IP即可访问您的本地服务。例如:

    client = fc2.Client(endpoint='http://host.docker.internal:8000', accessKeyID='xxx', accessKeySecret='xxx')
    resp = client.invoke_function('localdemo', 'nodejs8', json.dumps({'code': 123}))

    如果Docker版本小于18.03,在宿主机上根据不同操作系统执行命令查看宿主机IP,然后将宿主机的IP地址配置到Endpoint即可。

    • Linux或macOS操作系统:ifconfig

    • Windows操作系统:ipconfig

  • 环境变量

    template.yml文件中配置的环境变量会与线上行为一致,当函数运行时,可以通过代码获取。关于环境变量,请参见环境变量

    您可以通过环境变量区分函数是在本地运行还是在线上运行,方便您进行一些特殊的逻辑处理。详细信息,请参见EnvironmentVariables

  • Credentials

    您可以通过Credentials中存储的AccessKey信息访问阿里云的其他服务。fun local在本地运行函数时查找AccessKey信息的策略与fun deploy的策略相同。

    以下是一个根据本地、线上环境的不同,利用函数提供的Credentials配置OSS Client的例子。

    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)
阿里云首页 函数计算 相关技术圈