Serverless Devs is an open source serverless developer tool. Serverless Devs allows you to develop, create, test, and deploy projects in an efficient manner and manage projects throughout their lifecycles. This topic describes how to use Serverless Devs to download Function Compute application templates and use the templates to develop and deploy function applications. In this topic, functions in Node.js 14 are used.

Background

This topic describes two methods to initialize a project on Serverless Devs. When you use Serverless Devs for the first time, we recommend that you use Method 1. If you want to learn more about how to perform operations such as local debugging, remote invocations, and resource deletion on Serverless Devs, we recommend that you use Method 2.

Before you start

  1. Activate Function Compute. For more information, see Activate Function Compute.
  2. Install Serverless Devs and Docker
  3. Configure Serverless Devs

Method 1: Use the s command to manage resources

Run the following command to initialize the project:
sudo s
Initialize the project based on the command output. The following output is returned:
 No Serverless-Devs project is currently detected. Do you want to create a new project? Yes// No Serverless-Devs projects are found in the project file. Specify whether to create a project. 

 Serverless Awesome: https://github.com/Serverless-Devs/Serverless-Devs/blob/master/docs/zh/awesome.md

 Hello Serverless for Cloud Vendors Alibaba Cloud Serverless// Specify a cloud service provider. In this example, Alibaba Cloud is used. 
 Hello, serverlesser. Which template do you like? Quick start [Deploy a Hello World function to FaaS]// Specify the template that you want to use. In this example, a template is used to deploy an event function in Node.js 14. 
 Which template do you like? [Event] Node.js 14

 Create application command: [s init devsapp/start-fc-event-nodejs14]

 Please input your project name (init dir) start-fc-event-nodejs14// By default, Serverless Devs generates a project name. You can also specify a custom project name. 
 file decompression completed

     ____  _     _ ___  _ _     _        _____ ____
    /  _ \/ \   / \\  \/// \ /\/ \  /|  /    //   _\
    | / \|| |   | | \  / | | ||| |\ ||  |  __\|  /
    | |-||| |_/\| | / /  | \_/|| | \||  | |   |  \__
    \_/ \|\____/\_//_/   \____/\_/  \|  \_/   \____/
 please select credential alias default// Specify an alias that is configured in Serverless Devs. 

    Welcome to the Aliyun FC start application
     This application requires to open these services:
         FC : https://fc.console.aliyun.com/

     * The project is initialized. You can go to the project directory and run the s deploy command to deploy the project. 

 Thanks for using Serverless-Devs
 You could [cd /test/start-fc-event-nodejs14] and enjoy your serverless journey!
 If you need help for this example, you can use [s -h] after you enter folder.
 Document Star: https://github.com/Serverless-Devs/Serverless-Devs

 Do you want to deploy the project immediately? Yes// Specify whether to deploy the project. In this example, the project is deployed. 
 ......
helloworld:
  region:   cn-hangzhou
  service:
    name: hello-world-service
  function:
    name:       event-nodejs14
    runtime:    nodejs14
    handler:    index.handler
    memorySize: 128
    timeout:    60

Method 2: Use the s init command to manage resources

  1. Initialize a project.
    Run the following command in the project directory to initialize a project:
    sudo s init devsapp/start-fc-http-nodejs14
    Note In this example, an HTTP function in Node.js 14 is deployed to describe how to initialize a project. If you want to deploy other types of functions in another runtime environment, replace http in start-fc-http-nodejs14 with the type of trigger that you want to use, such as event. You can also replace nodejs14 with another runtime environment.
    Sample command output:
     Serverless Awesome: https://github.com/Serverless-Devs/Serverless-Devs/blob/master/docs/zh/awesome.md
     Please input your project name (init dir) start-fc-http-nodejs14// By default, Serverless Devs automatically generates a project name. You can also specify a custom project name. 
     file decompression completed
    
         ____  _     _ ___  _ _     _        _____ ____
        /  _ \/ \   / \\  \/// \ /\/ \  /|  /    //   _\
        | / \|| |   | | \  / | | ||| |\ ||  |  __\|  /
        | |-||| |_/\| | / /  | \_/|| | \||  | |   |  \__
        \_/ \|\____/\_//_/   \____/\_/  \|  \_/   \____/
     please select credential alias default// Specify an alias that is configured in Serverless Devs. 
    
        Welcome to the Aliyun FC start application
         This application requires to open these services:
             FC : https://fc.console.aliyun.com/
    
         * The project is initialized. You can go to the project directory and run the s deploy command to deploy the project. 
    
     Thanks for using Serverless-Devs
     You could [cd /test/start-fc-http-nodejs14] and enjoy your serverless journey!
     If you need help for this example, you can use [s -h] after you enter folder.
     Document Star: https://github.com/Serverless-Devs/Serverless-Devs
    
     Do you want to deploy the project immediately? No// Specify whether to deploy the project. 
    After the project is initialized, a file folder named start-fc-http-nodejs14 is generated in the directory. The start-fc-http-nodejs14 file folder contains the following files:
    • s.yaml: the YAML file that defines Function Compute resources. For more information about the YAML format, see YAML syntax and permission management.
    • s_en.yaml: the YAML file. s_en.yaml is the same as s.yaml. By default, the system uses the s.yaml file. To use the s_en.yaml file, explicitly specify the file in the command. For example, add the -t parameter (s deploy -t s_en.yaml -y) in the command when you deploy an application.
    • code: The code folder contains the index.js file in which you can edit the code logic of functions.
    • readme.md: The file that defines the dependencies, such as dependent modules and the configurations of the project.
  2. Run the following command to go to the project directory:
    cd start-fc-http-nodejs14
  3. Optional: Create an application.
    Run the following command in the project directory to create the application:
    sudo s build
    After you perform this step, the .s file is generated in the current directory to store the outputs. You can use the file to debug and deploy applications on your computer.
  4. Optional: Debug the application on your computer.
    After you build the application, you can debug the application on your computer to check whether the application runs as expected. Run one of the following commands based on the function type:
    • HTTP functions
      sudo s local start
      Sample command output:
      [2021-12-16 07:17:05] [INFO] [FC-LOCAL-INVOKE] - Using trigger for start: name: httpTrigger
      type: http
      config:
        authType: anonymous
        methods:
          - GET
      
      [2021-12-16 07:17:05] [INFO] [FC-LOCAL-INVOKE] - HttpTrigger httpTrigger of hello-world-service/http-trigger-nodejs14 was registered
              url: http://localhost:7013/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14/
              methods: GET
              authType: anonymous
      
      Tips: You can also use these commands to run or debug custom domain resources:
      
      Start with customDomain:
      * s local start auto
      
      Debug with customDomain:
      * s local start -d 3000 auto
      
      
      Tips for next step
      ======================
      * Deploy Resources: s deploy
      helloworld:
        status: succeed
      function compute app listening on port 7013!
    • Event functions
      sudo s local invoke
    The execution result varies with the function type.
    • HTTP functions
      If you debug an HTTP function, a URL is returned after you run the debugging command, and the debugging stops. You can use one of the following methods to trigger function execution:
      • Use cURL to trigger the HTTP function.
        curl http://localhost:7013/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14/
      • Use a browser to trigger the HTTP function.
        http://localhost:7013/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14/
    • Event functions

      If you debug an event function, the debugging result is returned after you run the debugging command.

  5. Deploy the application.
    Run the following command to deploy the application to Function Compute:
    sudo s deploy -y
    Sample command output:
     Checking Service, Function, Triggers (0.82s)
     Creating Service, Function, Triggers (0.6s)
     Creating custom domain (0.31s)
    
    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:       http-trigger-nodejs14
        runtime:    nodejs14
        handler:    index.handler
        memorySize: 128
        timeout:    60
      url:
        custom_domain:
          -
            domain: http://http-trigger-nodejs14.hello-world-service.188077086902****.cn-hangzhou.fc.devsapp.net
      triggers:
        -
          type: http
          name: httpTrigger
    If the execution is successful, the application is deployed to Function Compute.
  6. Optional: Remotely debug the application.
    Run the following command to remotely debug the application:
    sudo s invoke -e '{"body": "","method":"GET","headers":{"header_1":"v1"},"queries":{"key":"value"},"path":"/"}'
    Sample command output:
    Request url: https://188077086902****.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14/
    ========= FC invoke Logs begin =========
    FC Invoke Start RequestId: 6a3adedb-5d5d-4ac5-aa0b-2d43f4ace65c
    load code for handler:index.handler
    2022-03-04T08:39:14.770Z 6a3adedb-5d5d-4ac5-aa0b-2d43f4ace65c [verbose] hello world
    FC Invoke End RequestId: 6a3adedb-5d5d-4ac5-aa0b-2d43f4ace65c
    
    Duration: 66.23 ms, Billed Duration: 67 ms, Memory Size: 128 MB, Max Memory Used: 42.48 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result[Code: 200]:
    {
      path: '//',
      queries: { key: 'value' },
      headers: {
        accept: 'application/json',
        authorization: 'FC LTAI4G4cwJkK4Rza6xd9****:KZsoanKle4SoRO8EjyOjY3duLlVlR1mp78hjV9WG****',
        date: 'Fri, 04 Mar 2022 08:39:14 GMT',
        header_1: 'v1',
        host: '188077086902****.cn-hangzhou.fc.aliyuncs.com',
        'user-agent': 'Node.js(v14.17.4) OS(linux/x64) SDK(@alicloud/fc2@v2.5.0)',
        'x-forwarded-proto': 'http'
      },
      method: 'GET',
      requestURI: '/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14//?key=value',
      clientIP: '47.97.XX.XX',
      body: ''
    }
    
    End of method: invoke
    In this example, an HTTP function is deployed. A URL is returned after you run the remote debugging command. You can use cURL to trigger the HTTP function in Function Compute:
    curl https://188077086902****.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/hello-world-service/http-trigger-nodejs14/
  7. Optional: Delete the application.
    Run the following command to delete the application:
    sudo s remove service
    Sample command output:
    Need to delete the resource in the cn-hangzhou area, the operation service is hello-world-service:
     ......
     Delete trigger hello-world-service/http-trigger-nodejs14/httpTrigger success.
     Delete function hello-world-service/http-trigger-nodejs14 success.
     Delete service hello-world-service success.
    [2022-03-04 08:40:26] [INFO] [FC] - Getting list on-demand: _FC_NAS_hello-world-service-ensure-nas-dir-exist-service
    [2022-03-04 08:40:26] [INFO] [FC] - Getting list provision: _FC_NAS_hello-world-service-ensure-nas-dir-exist-service
    [2022-03-04 08:40:26] [INFO] [FC] - Getting listAliases: _FC_NAS_hello-world-service-ensure-nas-dir-exist-service
    [2022-03-04 08:40:26] [INFO] [FC] - Getting list on-demand: _FC_NAS_hello-world-service
    [2022-03-04 08:40:26] [INFO] [FC] - Getting list provision: _FC_NAS_hello-world-service
    [2022-03-04 08:40:26] [INFO] [FC] - Getting listAliases: _FC_NAS_hello-world-service
    End of method: remove
    If the execution is successful, the application is deleted.