You can use the custom runtime of Function Compute to write Node.js functions in Function Compute. Existing Node.js applications supported by the custom runtime can be migrated to the custom runtime without transformation. This topic describes how to deploy and invoke a Node.js 10 function or a Node.js 12 function. You can modify the Node.js sample code as needed for custom development.

Procedure

Deploy and invoke a Node.js 10 function

  1. Run the following command to initialize a project:
    s init fc-custom-nodejs10-event -d fc-custom-nodejs10-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-nodejs10-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 09:37:29] [INFO] [S-CLI] - Start the pre-action
    ......
    
    helloworld:
      region:   cn-hangzhou
      service:
        name: hello-world-service
      function:
        name:       node10EventFunc
        runtime:    custom
        handler:    index.handler
        memorySize: 1024
        timeout:    30
  5. Run the following command to remotely invoke the function:
    s invoke -e "{}"
    Sample command output:
    ========= FC invoke Logs begin =========
    
    Duration: 30.28 ms, Billed Duration: 31 ms, Memory Size: 1024 MB, Max Memory Used: 18.99 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    OK
    
    
    End of method: invoke

Deploy and invoke a Node.js 12 function

  1. Run the following command to initialize a project:
    s init fc-custom-nodejs12-event -d fc-custom-nodejs12-event
  2. Run the following command to go to the project directory:
    cd fc-custom-nodejs12-event
  3. Run the following command to build the project:
    s build --use-docker
    Sample command output:
    [2022-01-26 10:30:35] [INFO] [FC-BUILD] - Build artifact start...
    [2022-01-26 10:30:35] [INFO] [FC-BUILD] - Use docker for building.
    [2022-01-26 10:30:35] [INFO] [FC-BUILD] - Build function using image: aliyunfc/runtime-custom:build-1.10.1
    build-1.10.1: Pulling from aliyunfc/runtime-custom
    eb18d230e067: Pulling fs layer
    ......
    Digest: sha256:d97461710526217f55bd45c33fd92b885e0339bcfaf5555741c8b25a4117****
    Status: Downloaded newer image for aliyunfc/runtime-custom:build-1.10.1
    [2022-01-26 10:32:32] [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. Run the following command to deploy the project:
    s deploy -y
    Sample command output:
    [2022-01-26 10:38:52] [INFO] [S-CLI] - Start the pre-action
    ......
    helloworld:
      region:   cn-hangzhou
      service:
        name: hello-world-service
      function:
        name:       node12EventFunc
        runtime:    custom
        handler:    index.handler
        memorySize: 1024
        timeout:    30
  5. Run the following command to remotely invoke the function:
    s invoke -e "{}"
    Sample command output:
    ========= FC invoke Logs begin =========
    
    Duration: 14.52 ms, Billed Duration: 15 ms, Memory Size: 1024 MB, Max Memory Used: 61.39 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    OK
    
    
    End of method: invoke