All Products
Search
Document Center

Function Compute:Use Serverless Devs to manage functions

Last Updated:Feb 28, 2024

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 an application template in Function Compute to develop and deploy an application. In this example, a function that is deployed in the Node.js 16 runtime is used as an example.

Background information

This topic describes two methods to initialize a project. The first time you use Serverless Devs, 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 with Serverless Devs, we recommend that you use Method 2.

Before you begin

  1. Activate Function Compute

  2. Install Serverless Devs and dependencies

  3. Configure Serverless Devs

Method 1: Run the s command to manage a function

Run the following command to go to the project initialization module:

sudo s

Follow the on-screen prompts to complete project initialization. Sample command output:

 ? No Serverless-Devs project is currently detected. Do you want to create a new project? Yes

  More applications: https://registry.serverless-devs.com
? Hello Serverless for Cloud Vendors Alibaba Cloud Serverless
? Hello, serverlesser. Which template do you like? Quick start [Deploy a Hello World function to FaaS]
? Which template do you like? Node.js

 Create application command: [s init start-fc3-nodejs]

? Please input your project name (init dir) start-fc3-nodejs
Downloading[/v3/packages/start-fc3-nodejs/zipball/0.0.9]...
Download start-fc3-nodejs successfully

  Serverless Devs Application Case

    Cloud services required:
    - FC : https://fc.console.aliyun.com/

    Tips:
    - FC Component: https://github.com/devsapp/fc3/blob/master/docs/zh/readme.md
The ID of the region where the application is created.
? Example: cn-hangzhou.
The function name can contain letters, digits, underscores (_), and hyphens (-). It cannot start with a digit or hyphen (-). It must be 1 to 128 characters in length. 
? Function name: start-nodejs-at63.
The region where the application is created.
? Runtime: Node.js 16.
? please select credential alias default

    * Before using, please check whether the actions command in Yaml file is available
    * Carefully reading the notes in s.yaml is helpful for the use of the tool
    * If need help in the use process, please apply to join the Dingtalk Group: 33947367


 Thanks for using Serverless-Devs
 You could [cd /Users/start-fc3-nodejs] 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
 More applications: https://registry.serverless-devs.com

Method 2: Run the s init command to manage a function

  1. Initialize a project.

    Run the following command in the project directory to initialize the project:

    sudo s init start-fc3-nodejs
    Note

    A function that runs in the Node.js 16 runtime is used as an example to describe how to initialize a project. If you need to deploy a function in other runtimes, you can select the runtime when you initialize the project.

    Sample command output:

      More applications: https://registry.serverless-devs.com
    ? Please input your project name (init dir) start-fc3-nodejs
    Downloading[/v3/packages/start-fc3-nodejs/zipball/0.0.9]...
    Download start-fc3-nodejs successfully
    
      Serverless Devs Application Case
    
        Cloud services required:
        - FC : https://fc.console.aliyun.com/
    
        Tips:
        - FC Component: https://github.com/devsapp/fc3/blob/master/docs/zh/readme.md
    The region where the application is created.
    ? Example: cn-hangzhou.
    The function name can contain letters, digits, underscores (_), and hyphens (-). It cannot start with a digit or hyphen (-). It must be 1 to 128 characters in length. 
    ? Function name: start-nodejs-900k.
    The region where the application is created.
    ? Runtime: Node.js 16.
    ? please select credential alias default
    
        * Before using, please check whether the actions command in Yaml file is available
        * Carefully reading the notes in s.yaml is helpful for the use of the tool
        * If need help in the use process, please apply to join the Dingtalk Group: 33947367
    
    
      Thanks for using Serverless-Devs
      You could [cd /Users/start-fc3-nodejs] 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
      More applications: https://registry.serverless-devs.com

    After the project is initialized, the start-fc3-nodejs folder is generated in the current directory that contains the following files:

    • s.yaml: the YAML file that defines Function Compute resources. For more information, 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, when you deploy an application, add the parameter -t to the command. The new command is s deploy -t s_en.yaml -y.

    • 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 dependent modules and the configurations of the project.

  2. Run the following command to access the project directory:

    cd start-fc3-nodejs
  3. Optional. Debug the application locally.

    Run the following command to debug the application locally:

    sudo s local invoke -e "{\"key\": \"value\"}"
  4. Deploy the application.

    Run the following command to deploy the application in Function Compute:

    sudo s deploy -y

    Sample command output:

      Steps for [deploy] of [hello-world-app]
    ====================
    
     [hello_world] completed (1.36s)
    
      Result for [deploy] of [hello-world-app]
    ====================
    region:         cn-hangzhou
    description:    hello world by serverless devs
    functionName:   start-nodejs-900k
    handler:        index.handler
    internetAccess: true
    memorySize:     128
    role:
    runtime:        nodejs16
    timeout:        30

    If the execution is successful, the application is deployed to Function Compute.

  5. Optional. Debug the application remotely.

    Run the following command to remotely debug the application:

    sudo s invoke -e "{\"key\": \"value\"}"

    Sample command output:

      Steps for [invoke] of [hello-world-app]
    ====================
    ========= FC invoke Logs begin =========
    FC Invoke Start RequestId: 1-65bb583a-15b24a10-58ded3270984
    load code for handler:index.handler
    FC Invoke End RequestId: 1-65bb583a-15b24a10-58ded3270984
    
    Duration: 4.43 ms, Billed Duration: 5 ms, Memory Size: 128 MB, Max Memory Used: 8.90 MB
    ========= FC invoke Logs end =========
    
    Invoke instanceId: c-65bb583a-15416274-0a3cdda6c4ee
    Code Checksum: 16688953495441179501
    Qualifier: LATEST
    RequestId: 1-65bb583a-15b24a10-58ded3270984
    
    Invoke Result:
    hello world
     [hello_world] completed (0.6s)
  6. Optional. Delete the application.

    Run the following command to delete the application:

    sudo s remove

    Sample command output:

      Steps for [remove] of [hello-world-app]
    ====================
    Remove function: cn-hangzhou/start-nodejs-900k
    
         ? Are you sure you want to delete the resources listed above yes
     [hello_world] completed (4.09s)
    

    If the execution is successful, the application is deleted.

FAQ