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

背景信息

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

前提条件

  1. 安装Serverless Devs和Docker
  2. 配置Serverless Devs

方式一:使用s命令管理资源

执行以下命令,初始化项目。
s
请根据输出的交互提示,初始化项目。输出示例:
 No Serverless-Devs project is currently detected. Do you want to create a new project? Yes//在项目文件内未检测到Serverless-Devs项目。选择是否创建一个项目。

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

 Hello Serverless for Cloud Vendors Alibaba Cloud Serverless//选择目标云厂商,本示例以阿里云为例。
 Hello, serverlesser. Which template do you like? Quick start [Deploy a Hello World function to FaaS]//选择您需要创建的模板。本示例以快速部署运行环境为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//Serverless Devs默认为您生成一个项目名称,您也可以自定义该名称。
 file decompression completed

     ____  _     _ ___  _ _     _        _____ ____
    /  _ \/ \   / \\  \/// \ /\/ \  /|  /    //   _\
    | / \|| |   | | \  / | | ||| |\ ||  |  __\|  /
    | |-||| |_/\| | / /  | \_/|| | \||  | |   |  \__
    \_/ \|\____/\_//_/   \____/\_/  \|  \_/   \____/
 please select credential alias default//选择您在配置Serverless Devs时设置的别名。

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

     * 项目初始化完成,您可以直接进入项目目录下,并使用s deploy进行项目部署。



 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//选择是否部署项目,本示例以选择部署项目为例。
[2021-12-16 06:22:57] [INFO] [S-CLI] - Start ...
......
[2021-12-16 06:23:02] [INFO] [FC-DEPLOY] - Checking Function event-nodejs12 exists

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:       event-nodejs12
    runtime:    nodejs12
    handler:    index.handler
    memorySize: 128
    timeout:    60

方式二:使用s init命令管理资源

  1. 初始化项目。
    在目标文件内执行以下命令,初始化项目:
    s init devsapp/start-fc-http-nodejs12
    说明 本示例以部署一个运行环境为Node.js 12的HTTP函数为例,介绍如何快速初始化一个项目。当需要部署其他运行环境的示例时,您可以将http替换为其他类型的触发方式例如event,也可以将nodejs12替换为其他类型的运行环境。
    执行输出:
     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//Serverless Devs会默认生成一个项目名称,您也可以自定义该名称。
     file decompression completed
    
         ____  _     _ ___  _ _     _        _____ ____
        /  _ \/ \   / \\  \/// \ /\/ \  /|  /    //   _\
        | / \|| |   | | \  / | | ||| |\ ||  |  __\|  /
        | |-||| |_/\| | / /  | \_/|| | \||  | |   |  \__
        \_/ \|\____/\_//_/   \____/\_/  \|  \_/   \____/
     please select credential alias default//选择您在配置Serverless Devs时设置的别名。
    
        Welcome to the Aliyun FC start application
         This application requires to open these services:
             FC : https://fc.console.aliyun.com/
    
         * 项目初始化完成,您可以直接进入项目目录下,并使用s deploy进行项目部署。
    
     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//选择部署项目。
    当成功初始化项目后,会在当前目录中生成一个start-fc-http-nodejs12文件,该文件内包含以下文件:
    • s.yaml:函数计算资源,该资源是以YAML规范定义的。关于YAML规范的详细信息,请参见YAML规范
    • codecode文件夹内包含index.js文件,可在该文件内编辑函数代码逻辑。
    • readme.md:依赖清单文件,该文件内定义了项目所需的各个模块和项目的配置信息。
  2. 执行以下命令,进入项目目录内:
    cd start-fc-http-nodejs12
  3. 可选:构建应用。
    在项目目录内执行以下命令,构建应用:
    s build
    执行输出:
    [2021-09-26T17:38:13.480] [INFO ] [FC-BUILD] - Build artifact successfully.
    [2021-12-16 07:15:23] [INFO] [S-CLI] - Start ...
    [2021-12-16 07:15:24] [INFO] [FC-BUILD] - Build artifact start...
    [2021-12-16 07:15:24] [INFO] [FC-BUILD] - No need build for this project.
    [2021-12-16 07:15:24] [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
    执行成功该步骤后,会在当前目录中生成一个.s文件,存放输出的构建产物,该文件可用于本地调试和部署。
  4. 可选:本地调试应用。
    当您在本地完成应用的开发构建后,可能需要在本地调试验证功能是否正常。您可以按需选择命令调试函数:
    • HTTP函数
      s local start
      执行输出:
      [2021-12-16 07:17:04] [INFO] [S-CLI] - Start ...
      []
      [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/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!
    • 事件函数
      s local invoke
    当您执行调试命令后,由于函数类型不同返回结果也会不同:
    • HTTP函数
      当您的函数是HTTP函数时,执行完调试命令后,将在执行输出中返回一个URL地址,此时调试流程阻塞在这里,您可以使用以下方式触发函数执行:
      • 使用cURL工具触发函数执行
        curl http://localhost:7013/2016-08-15/proxy/hello-world-service/http-trigger-nodejs12/
      • 使用浏览器访问HTTP函数
        http://localhost:7013/2016-08-15/proxy/hello-world-service/http-trigger-nodejs12/
    • 事件函数

      如果您的函数是事件函数,执行完调试命令后,将立即返回调试结果。

  5. 部署应用。
    执行以下命令,将应用部署至函数计算:
    s deploy
    执行输出:
    [2021-12-16 07:22:02] [INFO] [S-CLI] - Start ...
    [2021-12-16 07:22:03] [INFO] [FC-DEPLOY] - Using region: cn-hangzhou
    [2021-12-16 07:22:03] [INFO] [FC-DEPLOY] - Using access alias: default
    [2021-12-16 07:22:03] [INFO] [FC-DEPLOY] - Using accessKeyID: LTAI4G4cwJkK4Rza6xd9****
    [2021-12-16 07:22:03] [INFO] [FC-DEPLOY] - Using accessKeySecret: eCc0GxSpzfq1DVspnqqd6nmYNN****
    ......
    
    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:
        system_url:    https://188077086902****.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/hello-world-service/http-trigger-nodejs12/
        custom_domain:
          -
            domain: http://http-trigger-nodejs12.hello-world-service.188077086902****.cn-hangzhou.fc.devsapp.net
      triggers:
        -
          type: http
          name: httpTrigger
    执行成功后,该命令会将应用部署至函数计算。
  6. 可选:远程调试应用。
    执行以下命令,远程调试应用:
    s invoke -e '{"body": "","method":"GET","headers":{"header_1":"v1"},"queries":{"key":"value"},"path":"/"}'
    执行输出:
    [2021-12-16 07:23:24] [INFO] [S-CLI] - Start ...
    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: c80660b0-9cff-48dd-9c8d-2fb3e758****
    load code for handler:index.handler
    2021-12-16T07:23:25.759Z c80660b0-9cff-48dd-9c8d-2fb3e758**** [verbose] hello world
    FC Invoke End RequestId: c80660b0-9cff-48dd-9c8d-2fb3e758****
    
    Duration: 112.54 ms, Billed Duration: 113 ms, Memory Size: 128 MB, Max Memory Used: 38.34 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result[Code: 200]:
    {
      path: '/',
      queries: {},
      headers: {
        accept: 'application/json',
        authorization: 'FC LTAI4G4cwJkK4Rza6xd9****:1zZLOQrySQxwS7ratNUoZko16qCcwoV/TB+Pe1XQ****',
        date: 'Thu, 16 Dec 2021 07:23:25 GMT',
        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/',
      clientIP: '47.97.xx.xx',
      body: ''
    }
    
    
    End of method: invoke
    由于本示例部署的函数是HTTP函数,所以在成功执行远程调试命令后,执行输出会返回一个URL,您可以使用cURL工具触发执行线上的函数:
    curl https://188077086902****.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/hello-world-service/http-trigger-nodejs12/
  7. 可选:删除应用。
    执行以下命令删除应用:
    s remove service
    执行输出:
    [2021-12-16 07:25:08] [INFO] [FC] - Getting list on-demand: hello-world-service
    [2021-12-16 07:25:09] [INFO] [FC] - Getting list provision: hello-world-service
    [2021-12-16 07:25:09] [INFO] [FC] - Getting listAliases: hello-world-service
    [2021-12-16 07:25:09] [INFO] [FC] - Getting listVersions: hello-world-service
    [2021-12-16 07:25:09] [INFO] [FC-DEPLOY] - Using region: cn-hangzhou
    [2021-12-16 07:25:09] [INFO] [FC-DEPLOY] - Using access alias: default
    [2021-12-16 07:25:09] [INFO] [FC-DEPLOY] - Using accessKeyID: LTAI4G4cwJkK4Rza6xd9****
    [2021-12-16 07:25:09] [INFO] [FC-DEPLOY] - Using accessKeySecret: eCc0GxSpzfq1DVspnqqd6nmYNN****
    [2021-12-16 07:25:09] [INFO] [FC-DEPLOY] - Checking Service hello-world-service exists
    [2021-12-16 07:25:09] [INFO] [FC-DEPLOY] - Service: hello-world-service already exists online.
    [2021-12-16 07:25:09] [INFO] [FC-DEPLOY] - Checking Function http-trigger-nodejs12 exists
    [2021-12-16 07:25:09] [INFO] [FC-DEPLOY] - Function: http-trigger-nodejs12 already exists online.
    [2021-12-16 07:25:09] [INFO] [FC-DEPLOY] - Checking Trigger httpTrigger exists
    [2021-12-16 07:25:10] [INFO] [FC-DEPLOY] - Trigger: httpTrigger already exists online.
     Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch.
     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.
    helloworld:
      triggers:
        - httpTrigger
      functions:
        - http-trigger-nodejs12
      service:   hello-world-service
    当成功执行该命令时,表示应用已被删除。