Serverless Devs is an open source platform for serverless application developers. You can use Serverless Devs to develop, create, test, and deploy projects and manage lifecycles of the projects in an efficient manner. 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 12 are used.

Background information

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 begin

  1. Install Serverless Devs and Docker
  2. Configure Serverless Devs

Method 1: Use the s command to manage resources

Run the following command to initialize a project:
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 12. 
 Which template do you like? [Event] Node.js 12

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

 Please input your project name (init dir) start-fc-event-nodejs12// 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-nodejs12] 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-nodejs12
    runtime:    nodejs12
    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:
    s init devsapp/start-fc-http-nodejs12
    Note In this example, an HTTP function in Node.js 12 is deployed to describe how to initialize a project. If you want to deploy other types of functions in another runtime environment, replace http with the types of triggers that you want to use, such as event. You can also replace nodejs12 with another runtime environment.
    The following output is returned:
     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-nodejs12// 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-http-nodejs12] 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 named start-fc-http-nodejs12 is generated in the directory. The start-fc-http-nodejs12 file 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.
    • code: The code folder contains the index.js file. You can edit the code logic of functions in this file.
    • 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-nodejs12
  3. Optional: Create an application.
    Run the following command in the project directory to create the application:
    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 function
      s local start
      The following output is returned:
      [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-nodejs12 was registered
              url: http://localhost:7013/2016-08-15/proxy/hello-world-service/http-trigger-nodejs12/
              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 function
      s local invoke
    The execution result varies with the function type.
    • HTTP function
      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-nodejs12/
      • Use a browser to trigger the HTTP function.
        http://localhost:7013/2016-08-15/proxy/hello-world-service/http-trigger-nodejs12/
    • Event function

      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:
    s deploy -y
    The following output is returned:
     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-nodejs12
        runtime:    nodejs12
        handler:    index.handler
        memorySize: 128
        timeout:    60
      url:
        custom_domain:
          -
            domain: http://http-trigger-nodejs12.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:
    s invoke -e '{"body": "","method":"GET","headers":{"header_1":"v1"},"queries":{"key":"value"},"path":"/"}'
    The following output is returned:
    Request url: https://188077086902****.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/hello-world-service/http-trigger-nodejs12/
    ========= 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-nodejs12//?key=value',
      clientIP: '47.97.212.57',
      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-nodejs12/
  7. Optional: Delete the application.
    Run the following command to delete the application:
    s remove service
    The following output is returned:
    Need to delete the resource in the cn-hangzhou area, the operation service is hello-world-service:
     ......
     Delete trigger hello-world-service/http-trigger-nodejs12/httpTrigger success.
     Delete function hello-world-service/http-trigger-nodejs12 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.