文档

通过Serverless Devs管理函数

更新时间:
一键部署

Serverless Devs是一个开源开放的Serverless开发者工具,通过该工具您可以快速地开发、创建、测试和部署项目,实现项目的全生命周期管理。本文以部署运行环境为Node.js 16的函数为例,介绍如何使用Serverless Devs下载对应的函数计算应用模板,然后快速开发部署应用。

背景信息

本文介绍两种方式初始化项目。当您初次使用Serverless Devs时,建议您使用方式一体验Serverless Devs。如果您想了解关于Serverless Devs的更多信息,例如本地调试、远程调用、删除资源等操作,建议您使用方式二体验。

前提条件

  1. 开通函数计算服务

  2. 安装Serverless Devs工具及依赖

  3. 配置Serverless Devs

方式一:使用s命令管理函数

执行以下命令,进入项目初始化引导模块。

sudo s

请根据引导操作完成项目初始化。输出示例:

 ? 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
创建应用所在的地区
? 地域 cn-hangzhou
只能包含字母、数字、下划线和中划线。不能以数字、中划线开头。长度在 1-128 之间。
? 函数名称 start-nodejs-at63
创建应用所在的地区
? nodejs 运行时 nodejs16
? 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

方式二:使用s init命令管理函数

  1. 初始化项目。

    在目标文件内执行以下命令,初始化项目:

    sudo s init start-fc3-nodejs
    说明

    本示例以部署一个运行环境为Node.js 16的函数为例,介绍如何快速初始化一个项目。当需要部署其他运行环境的示例时,您可以在初始化项目时选择对应的运行时。

    执行输出:

      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
    创建应用所在的地区
    ? 地域 cn-hangzhou
    只能包含字母、数字、下划线和中划线。不能以数字、中划线开头。长度在 1-128 之间。
    ? 函数名称 start-nodejs-900k
    创建应用所在的地区
    ? nodejs 运行时 nodejs16
    ? 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

    当成功初始化项目后,会在当前目录中生成一个start-fc3-nodejs文件夹,该文件夹内包含以下文件:

    • s.yaml:函数计算资源,该资源是以YAML规范定义的。关于YAML规范的详细信息,请参见YAML规范

    • s_en.yaml:同s.yaml。系统默认使用s.yaml文件,如需使用s_en.yaml文件,请在命令中显式指定。例如,部署应用时,命令中增加参数-t,即s deploy -t s_en.yaml -y

    • codecode文件夹内包含index.js文件,可在该文件内编辑函数代码逻辑。

    • readme.md:依赖清单文件,该文件内定义了项目所需的各个模块和项目的配置信息。

  2. 执行以下命令,进入项目目录内:

    cd start-fc3-nodejs
  3. 可选:本地调试应用。

    执行以下命令进行本地调试。

    sudo s local invoke -e "{\"key\": \"value\"}"
  4. 部署应用。

    执行以下命令,将应用部署至函数计算:

    sudo s deploy -y

    执行输出:

      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

    执行成功后,该命令会将应用部署至函数计算。

  5. 可选:远程调试应用。

    执行以下命令,远程调试应用:

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

    执行输出:

      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. 可选:删除应用。

    执行以下命令删除应用:

    sudo s remove

    执行输出:

      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)
    

    当成功执行该命令时,表示应用已被删除。

常见问题

  • 本页导读 (1)
文档反馈