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

前提条件

您已完成以下操作:

操作步骤

  1. 在目标目录中,执行以下命令,初始化项目。
    s init start-fc-custom-samples -d start-fc-custom-samples
  2. 执行以下命令,进入项目目录。
    cd start-fc-custom-samples/lua/event-demo/
  3. 执行以下命令,安装依赖。
    s build -d
    输出示例:
    [2021-09-09T16:09:41.391] [INFO ] [S-CLI] - Start ...
    [2021-09-09T16:09:41.719] [INFO ] [FC-BUILD] - Build artifact start...
    [2021-09-09T16:09:41.726] [INFO ] [FC-BUILD] - Use docker for building.
    [2021-09-09T16:09:41.733] [INFO ] [FC-BUILD] - No need build for this project.
    [2021-09-09T16:09:41.735] [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
  4. 执行以下命令,部署项目。
    s deploy -y
    输出示例:
    [2021-09-09T16:11:46.026] [INFO ] [S-CLI] - Start ...
    [2021-09-09T16:11:46.551] [INFO ] [FC-DEPLOY] - Using region: cn-hangzhou
    [2021-09-09T16:11:46.552] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-09-09T16:11:46.553] [INFO ] [FC-DEPLOY] - Using accessKeyID: 188077086902****
    [2021-09-09T16:11:46.553] [INFO ] [FC-DEPLOY] - Using accessKeySecret: LTAI4G4cwJkK4Rza6xd9****
      Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch.
    [2021-09-09T16:11:46.943] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
    [2021-09-09T16:11:47.181] [INFO ] [FC-DEPLOY] - Checking Function luaEventFunc exists
      Make service custom-demo success.
      Make function custom-demo/luaEventFunc success.
    [2021-09-09T16:13:43.273] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
    [2021-09-09T16:13:43.452] [INFO ] [FC-DEPLOY] - Checking Function luaEventFunc exists
    
    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
    
    
    
    fc-lua-demo-fc-lua:
      region: cn-hangzhou
      service:
        name: custom-demo
      function:
        name: luaEventFunc
        runtime: custom
        handler: index.handler
        memorySize: 1024
        timeout: 3
  5. 执行以下命令,远程调用函数。
    s invoke -e "hello"
    输出示例:
    [2021-09-09T16:25:14.753] [INFO ] [S-CLI] - Start ...
    ========= FC invoke Logs begin =========
    FC Initialize Start RequestId: 7ed96960-9283-4c62-b7c1-dffb24dc85be, client: 21.0.0.1, server: , request: "POST /initialize HTTP/1.1", host: "21.0.6.148:9000"
    2021/09/09 08:25:16 [notice] 3#3: *2 [lua] main.lua:16: init, client: 21.0.0.1, server: , request: "POST /initialize HTTP/1.1", host: "21.0.6.148:9000"
    2021/09/09 08:25:16 [notice] 3#3: *2 [lua] main.lua:18: FC Initialize End RequestId: 7ed96960-9283-4c62-b7c1-dffb24dc85be, client: 21.0.0.1, server: , request: "POST /initialize HTTP/1.1", host: "21.0.6.148:9000"
    21.0.6.148 21.0.0.1 0.000 [09/Sep/2021:08:25:16 +0000] "POST /initialize HTTP/1.1" 200 11 "-" "Go-http-client/1.1" "-" 7ed96960-9283-4c62-b7c1-dffb24dc85be
    2021/09/09 08:25:16 [notice] 3#3: *2 [lua] main.lua:23: FC Invoke Start RequestId: 7ed96960-9283-4c62-b7c1-dffb24dc85be, client: 21.0.0.1, server: , request: "POST /invoke HTTP/1.1", host: "21.0.6.148:9000"
    2021/09/09 08:25:16 [notice] 3#3: *2 [lua] main.lua:26: hello, client: 21.0.0.1, server: , request: "POST /invoke HTTP/1.1", host: "21.0.6.148:9000"
    2021/09/09 08:25:16 [notice] 3#3: *2 [lua] main.lua:28: FC Invoke End RequestId: 7ed96960-9283-4c62-b7c1-dffb24dc85be, client: 21.0.0.1, server: , request: "POST /invoke HTTP/1.1", host: "21.0.6.148:9000"
    
    Duration: 3.75 ms, Billed Duration: 4 ms, Memory Size: 1024 MB, Max Memory Used: 10.46 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    hello
    
    
    
    End of method: invoke