You can use the custom runtime of Function Compute to write TypeScript functions in Function Compute. This topic describes how to deploy and invoke a TypeScript function. You can modify the TypeScript sample code as needed for custom development.

Procedure

  1. Run the following command to initialize a project:
    s init fc-custom-typescript-event -d fc-custom-typescript-event
    Note -d specifies the name of the folder in which the sample project resides. You can customize the folder name as required.
  2. Run the following command to go to the project directory:
    cd fc-custom-typescript-event
  3. Optional: Modify the sample code as needed for custom development.
  4. Run the following command to deploy the project:
    s deploy -y
    Sample command output:
    [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. Run the following command to remotely invoke the function:
    s invoke -e "hello"
    Sample command output:
    ========= 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