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

操作步骤

  1. 执行以下命令,初始化项目。
    s init fc-custom-typescript-event -d fc-custom-typescript-event
    说明 -d用于指定该示例项目文件夹的名称,支持自定义。
  2. 执行以下命令,进入项目目录。
    cd fc-custom-typescript-event
  3. 可选:按需修改函数的示例代码,实现二次开发。
  4. 执行以下命令,部署项目。
    s deploy -y
    输出示例:
    [2022-01-26 03:42:54] [INFO] [S-CLI] - Start the pre-action
    [2022-01-26 03:42:54] [INFO] [S-CLI] - Action: s build --use-docker
    [2022-01-26 03:42:55] [INFO] [FC-BUILD] - Build artifact start...
    [2022-01-26 03:42:55] [INFO] [FC-BUILD] - Use docker for building.
    [2022-01-26 03:42:55] [INFO] [FC-BUILD] - Build function using image: registry.regionid.aliyuncs.com/aliyunfc/runtime-custom:build-1.9.21
    [2022-01-26 03:42:55] [INFO] [FC-BUILD] - skip pulling image registry.regionid.aliyuncs.com/aliyunfc/runtime-custom:build-1.9.21...
    [2022-01-26 03:43:01] [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 03:43:01] [INFO] [S-CLI] - End the pre-action
    ⠋ Checking Function tsEventFunc exists
    Detail:
    
    updated:
      ......
     Checking Service, Function (5.05s)
     Creating Service, Function (118.9s)
    
    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:       tsEventFunc
        runtime:    custom
        handler:    index.handler
        memorySize: 512
        timeout:    3
  5. 执行以下命令,远程调用函数。
    s invoke -e "hello"
    输出示例:
    ========= FC invoke Logs begin =========
    FC Invoke Start RequestId: da1c9d7c-0d84-447c-8ef2-cfb97fd2****
    hello
    FC Invoke End RequestId: da1c9d7c-0d84-447c-8ef2-cfb97fd2****
    
    Duration: 15.97 ms, Billed Duration: 16 ms, Memory Size: 512 MB, Max Memory Used: 167.67 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    hello
    
    
    End of method: invoke