通过函数计算的Custom Runtime,您可以使用Lua在函数计算平台编写函数。本文介绍快速部署和调用Lua函数的详细步骤。您可以按需修改Lua示例中的代码实现二次开发。

操作步骤

  1. 执行以下命令,初始化项目。
    s init fc-custom-lua-event -d fc-custom-lua-event
    说明 -d用于指定该示例项目文件夹的名称,支持自定义。
  2. 执行以下命令,进入项目目录。
    cd fc-custom-lua-event
  3. 可选:按需修改函数的示例代码,实现二次开发。
  4. 执行以下命令,部署项目。
    s deploy -y
    输出示例:
    [2022-01-26 08:57:00] [INFO] [S-CLI] - Start the pre-action
    [2022-01-26 08:57:00] [INFO] [S-CLI] - Action: s build --use-docker
    [2022-01-26 08:57:01] [INFO] [FC-BUILD] - Build artifact start...
    [2022-01-26 08:57:02] [INFO] [FC-BUILD] - Use docker for building.
    [2022-01-26 08:57:02] [INFO] [FC-BUILD] - No need build for this project.
    [2022-01-26 08:57:02] [INFO] [FC-BUILD] - Build artifact successfully.
    
    Tips for next step
    ======================
    * Invoke Event Function: s local invoke
    * Invoke Http Function: s local start
    * Deploy Resources: s deploy
    End of method: build
    [2022-01-26 08:57:02] [INFO] [S-CLI] - End the pre-action
     Checking Service, Function (3.57s)
     Creating Service, Function (114.37s)
    
    Tips for next step
    ======================
    * Display information of the deployed resource: s info
    * Display metrics: s metrics
    * Display logs: s logs
    * Invoke remote function: s invoke
    * Remove Service: s remove service
    * Remove Function: s remove function
    * Remove Trigger: s remove trigger
    * Remove CustomDomain: s remove domain
    
    helloworld:
      region:   cn-hangzhou
      service:
        name: hello-world-service
      function:
        name:       luaEventFunc
        runtime:    custom
        handler:    index.handler
        memorySize: 1024
        timeout:    30
  5. 执行以下命令,远程调用函数。
    s invoke -e "hello"
    输出示例:
    ========= FC invoke Logs begin =========
    FC Invoke Start RequestId: cbc4a3c1-72bb-4d0d-91f8-061c99a5****, client: 21.0.XX.XX, server: , request: "POST /invoke HTTP/1.1", host: "21.0.XX.XX:9000"
    2022/01/26 09:04:37 [notice] 8#8: *2 [lua] main.lua:26: hello, client: 21.0.XX.XX, server: , request: "POST /invoke HTTP/1.1", host: "21.0.XX.XX:9000"
    2022/01/26 09:04:37 [notice] 8#8: *2 [lua] main.lua:28: FC Invoke End RequestId: cbc4a3c1-72bb-4d0d-91f8-061c99a5****, client: 21.0.3.254, server: , request: "POST /invoke HTTP/1.1", host: "21.0.XX.XX:9000"
    
    Duration: 1.93 ms, Billed Duration: 2 ms, Memory Size: 1024 MB, Max Memory Used: 9.44 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    hello
    
    
    
    End of method: invoke