写点什么

Serverless Devs 新手引导

作者:刘宇
  • 2021 年 12 月 15 日
  • 本文字数:7437 字

    阅读完需:约 24 分钟

Serverless Devs 新手引导

本快速上手案例以 阿里云函数计算 为例,快速上手 Serverless Devs。

工具安装

  • 第一步:安装 Node.js(>=10.8.0) 与 NPM 包管理工具;

  • 第二步:安装 Serverless Devs 开发者工具;


  $ npm install @serverless-devs/s -g
复制代码


  • 第三步:可以通过s -v判断工具是否安装成功,如果安装成功可以看到相对应的版本信息,例如:


  @serverless-devs/s: 2.0.89, @serverless-devs/core: 0.1.7, darwin-x64, node-v12.15.0
复制代码

密钥配置

由于本快速上手文档,将会以 阿里云函数计算 为例,所以此处的密钥配置也是以阿里云密钥配置为例:

  • AccountId 获取页面:https://account.console.aliyun.com/#/secure

  • 获取密钥页面:https://usercenter.console.aliyun.com/#/manage/ak


  • 打开 AccountId获取页面 获取 AccountId :<img src="https://images.devsapp.cn/access/aliyun-accountid.jpg" alt="AccountId 获取页面" style="zoom: 25%;" />

  • 打开 获取密钥页面 获取密钥信息 :<img src="https://images.devsapp.cn/access/aliyun-access.jpg" alt="获取密钥页面" style="zoom:25%;" />

  • 执行s config add,并选择Alibaba Cloud (alibaba)


  $ s config add   ? Please select a template: Alibaba Cloud (alibaba)  🧭 Refer to the document for alibaba key:  http://config.devsapp.net/account/alibaba  ? AccountID () 
复制代码


  • 此时,可以按照引导,进行密钥的配置:


  ? Please select a template: Alibaba Cloud (alibaba)  🧭 Refer to the document for alibaba key:  http://config.devsapp.net/account/alibaba  ? AccountID 此处填写AccountID  ? AccessKeyID 此处填写AccessKeyID  ? AccessKeySecret 此处填写AccessKeySecret  ? Please create alias for key pair. If not, please enter to skip alibaba-access        Alias: alibaba-access      AccountID: 此处填写AccountID      AccessKeyID: 此处填写AccessKeyID      AccessKeySecret: 此处填写AccessKeySecret    Configuration successful
复制代码


  • 为了验证密钥是否正确配置,可以通过s config get -a alibaba-access进行指定密钥的查看:


  $ s config get -a alibaba-access  [2021-10-27T17:39:39.881] [INFO ] [S-CLI] -     alibaba-access:    AccountID: 此处填*******tID    AccessKeyID: 此处填*********yID    AccessKeySecret: 此处填*************ret
复制代码


云账号 AccessKey 是您访问阿里云 API 的密钥,具有该账户完全的权限,请您务必妥善保管!不要通过任何方式(e.g. Github)将 AccessKey 公开到外部渠道,以避免被他人利用而造成 安全威胁

强烈建议您遵循 阿里云安全最佳实践 ,使用 RAM 子用户 AccessKey 来进行 API 调用。

项目上手体验

Serverless:Hello World
  • 执行s命令:


  $ s  ? Serverless Devs project is not detected. Do you want to create a new project? (Y/n) 
复制代码


  • 填写y,并按回车,可以进入到创建引导部分:


  🚀 Serverless Awesome: https://github.com/Serverless-Devs/package-awesome    ? Hello Serverless for Cloud Vendors (Use arrow keys or type to search)  ❯ Alibaba Cloud Serverless     AWS Cloud Serverless     Baidu Cloud Serverless     Huawei Cloud Serverless     Tencent Cloud Serverless     Dev Template for Serverless Devs 
复制代码


  • 此时只需要选择对应的选项,按照引导进行操作,即可。例如选择Alibaba Cloud Serverless,就可以看到阿里云 Serverless 产品下的应用模板分类:


  ? Please select an Serverless-Devs Application (Use arrow keys or type to search)  ❯ fc-runtime-starter - 快速部署一个 FC 函数     fc-custom-container-stater - 快速部署一个 custom-container 应用     web-framework-stater - 快速部署一个 Web 框架     static-website-stater - 快速部署一个静态网站     serverless-best-practice - 快速体验 Serverless 最佳实践 
复制代码


  • 此时可以继续选择某分类下的具体应用进行初始化,例如选择fc-runtime-starter之后,可以看到该分类下的具体模板应用:


  ? Please select an templete (Use arrow keys or type to search)  ❯ fc-http-nodejs - 快速部署一个 nodejs12 http函数     fc-http-python - 快速部署一个 python3 http函数     fc-http-php - 快速部署一个 php http函数     fc-http-java - 快速部署一个 java8 http函数     fc-event-nodejs - 快速部署一个 nodejs12 event函数     fc-event-python - 快速部署一个 python3 event函数     fc-event-php - 快速部署一个 php event函数 
复制代码


选择fc-http-nodejs即可完成创建,在引导的过程中,可能会出现填写项目名称以及选择密钥的过程:


  • 项目名称可以是:start-fc-http-nodejs12

  • 密钥可以选择我们上文中创建过的:alibaba-access


例如:


  🚀 Serverless Awesome: https://github.com/Serverless-Devs/package-awesome    ? Hello Serverless for Cloud Vendors Alibaba Cloud Serverless  ? Please select an Serverless-Devs Application fc-runtime-starter - 快速部署一个 FC 函数  ? Please select an templete fc-http-nodejs - 快速部署一个 nodejs12 http函数    😋 Create application command: [s init devsapp/start-fc-http-nodejs12]    ? Please input your project name (init dir) start-fc-http-nodejs12  ✔ file decompression completed  ? please select credential alias alibaba-access    ... ...    🏄‍ Thanks for using Serverless-Devs  👉 You could [cd /Users/jiangyu/start-application/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    ? 是否立即部署该项目? (Y/n) 
复制代码


  • 可以看到,系统在最后有一个提醒,是否要部署该项目,此时可以输入y,直接进行项目的部署,稍等片刻,可以看到部署结果:


  fc-deploy-test:     region:   cn-hangzhou    service:       name: fc-deploy-service    function:       name:       http-trigger-nodejs12      runtime:    nodejs12      handler:    index.handler      memorySize: 128      timeout:    60    url:       system_url:    https://1583208943291465.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/fc-deploy-service/http-trigger-nodejs12/      custom_domain:         -           domain: http://http-trigger-nodejs12.fc-deploy-service.1583208943291465.cn-hangzhou.fc.devsapp.net    triggers:       -         type: http        name: httpTrigger
复制代码


此时可以打开domain返回给我们的域名,进行测试。

人工智能:目标检测
  • 初始化一个已有的人工智能目标检测项目:s init devsapp/image-prediction-app,初始化过程中可能会出现填写项目名称以及选择密钥的过程:

  • 项目名称可以是:image-prediction-app

  • 密钥可以选择我们上文中创建过的:alibaba-access

  • 例如:


  $ s init devsapp/image-prediction-app    🚀 Serverless Awesome: https://github.com/Serverless-Devs/package-awesome    ? Please input your project name (init dir) image-prediction-app  ✔ file decompression completed  ? please select credential alias alibaba-access         ___   __   __  _______  _______  _______       |   | |  |_|  ||   _   ||       ||       |      |   | |       ||  |_|  ||    ___||    ___|      |   | |       ||       ||   | __ |   |___       |   | |       ||       ||   ||  ||    ___|      |   | | ||_|| ||   _   ||   |_| ||   |___       |___| |_|   |_||__| |__||_______||_______|                                                  Welcome to the image-prediction-app application       This application requires to open these services:            FC : https://fc.console.aliyun.com/       This application can help you quickly deploy the image-prediction-app project.       The application uses FC component:https://github.com/devsapp/fc       The application homepage: https://github.com/devsapp/image-prediction-app      🏄‍ Thanks for using Serverless-Devs  👉 You could [cd /Users/jiangyu/start-application/image-prediction-app] 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
复制代码


  • 进入项目目录:cd image-prediction-app

  • 通过deploy命令进行项目的部署:


  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        imageAi:     region: cn-hangzhou    url:       custom_domain:         -           domain: http://server.ai-cv-image-prediction.1583208943291465.cn-hangzhou.fc.devsapp.net
复制代码


  • 此时可以打开系统分配的测试域名,并上传一张图片进行测试:

传统框架:基于 Django 的博客项目
  • 初始化一个已有的基于 Django 的博客项目:s init django-blog,初始化过程中可能会出现填写项目名称以及选择密钥的过程:

  • 项目名称可以是:django-blog

  • 密钥可以选择我们上文中创建过的:alibaba-access

  • 例如:


  $ s init django-blog    🚀 Serverless Awesome: https://github.com/Serverless-Devs/package-awesome    ? Please input your project name (init dir) django-blog  ✔ file decompression completed  ? please select credential alias alibaba-access         ______       ___  _______  __    _  _______  _______  _______  ___      _______  _______       |      |     |   ||   _   ||  |  | ||       ||       ||  _    ||   |    |       ||       |      |  _    |    |   ||  |_|  ||   |_| ||    ___||   _   || |_|   ||   |    |   _   ||    ___|      | | |   |    |   ||       ||       ||   | __ |  | |  ||       ||   |    |  | |  ||   | __       | |_|   | ___|   ||       ||  _    ||   ||  ||  |_|  ||  _   | |   |___ |  |_|  ||   ||  |      |       ||       ||   _   || | |   ||   |_| ||       || |_|   ||       ||       ||   |_| |      |______| |_______||__| |__||_|  |__||_______||_______||_______||_______||_______||_______|                                                  Welcome to the django-blog application       This application requires to open these services:            FC : https://fc.console.aliyun.com/       This application can help you quickly deploy the django-blog project.       The application uses Django component:https://github.com/devsapp/django       The application homepage: https://github.com/devsapp/django-blog              * Python 3.7 is recommended;       * If the version is greater than Python 3.7:           * Operation error: ImportError: cannot import name 'metadata' from 'importlib', you can refer to: https://stackoverflow.com/questions/59216175/importerror-cannot-import-name-metadata-from-importlib       * Default information:          * Admin:/admin          * Default Admin Username: blog          * Default Admin Password: myblog12345!                  🏄‍ Thanks for using Serverless-Devs  👉 You could [cd /Users/jiangyu/django-blog] 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
复制代码


  • 进入项目目录:cd django-blog

  • 通过deploy命令进行项目的部署:


  Tips for next step  ======================  * Invoke remote function: s invoke  ✔ Try container acceleration  djangoBlog:     region:        cn-shenzhen    serviceName:   serverless-devs-django    functionName:  django    customDomains:       - http://django.serverless-devs-django.1583208943291465.cn-shenzhen.fc.devsapp.net
复制代码


  • 此时可以打开系统分配的测试域名,并上传一张图片进行测试:

功能上手体验

测试项目创建


通过s init命令创建一个 Python 语言的 Hello World 项目,在引导的过程中,可能会出现填写项目名称以及选择密钥的过程:


  • 项目名称可以是:start-fc-http-python3

  • 密钥可以选择我们上文中创建过的:alibaba-access

  • 例如:


$ s init devsapp/start-fc-http-python3
? 🚀 Serverless Awesome: https://github.com/Serverless-Devs/package-awesome
? Please input your project name (init dir) start-fc-http-python3✔ file decompression completed? please select credential alias alibaba-access
...
🏄‍ Thanks for using Serverless-Devs👉 You could [cd /Users/jiangyu/demo/test/start-fc-http-python3] 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
? 是否立即部署该项目? (Y/n)
复制代码


此时,可以先不进行项目的部署,即选择n即可。接下来,可以通过cd等命令进入项目(例如:cd start-fc-http-python3)。

功能体验

部署 deploy


为了便于后续的体验,可以对默认的s.yaml文件进行修改,增加自动化日志配置的能力:logConfig: auto,完整的项目 Yaml 如下:


edition: 1.0.0          #  命令行YAML规范版本,遵循语义化版本(Semantic Versioning)规范name: fcDeployApp       #  项目名称access: "default"       #  秘钥别名
services: fc-deploy-test: # 服务名称 component: devsapp/fc # 组件名称 props: # 组件的属性值 region: cn-hangzhou service: name: fc-deploy-service description: 'demo for fc-deploy component' logConfig: auto function: name: http-trigger-py36 description: this is a test runtime: python3 codeUri: ./code handler: index.handler memorySize: 128 timeout: 60 triggers: - name: httpTrigger type: http config: authType: anonymous methods: - GET customDomains: - domainName: auto protocol: HTTP routeConfigs: - path: /* methods: - GET
复制代码


保存并退出编辑之后,可以执行s deploy直接进行项目的部署,稍等片刻,即可看到部署结果:


fc-deploy-test:   region:   cn-hangzhou  service:     name: fc-deploy-service  function:     name:       http-trigger-py36    runtime:    python3    handler:    index.handler    memorySize: 128    timeout:    60  url:     system_url:    https://1583208943291465.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/fc-deploy-service/http-trigger-py36/    custom_domain:       -         domain: http://http-trigger-py36.fc-deploy-service.1583208943291465.cn-hangzhou.fc.devsapp.net  triggers:     -       type: http      name: httpTrigger
复制代码


调用相关


本地调用


由于该项目是一个 HTTP 函数,所以可以使用s local start进行本地调用的测试(如果是其他 Event 函数,可以考虑用 s local invoke)。


   url: http://localhost:7665/2016-08-15/proxy/fc-deploy-service/http-trigger-py36/  methods: GET  authType: anonymous
Tips for more action: Start with customDomain method: [s local start auto] Debug with customDomain method: [s local start -d 3000 auto]
复制代码


此时,可以根据系统返回的url参数,在浏览器中打开http://localhost:7665/2016-08-15/proxy/fc-deploy-service/http-trigger-py36/,进行 HTTP 函数的本地测试。


远程调用


在当前项目下,直接使用 s invoke 即可实现线上函数的调用/触发:


Request url: https://1583208943291465.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/fc-deploy-service/http-trigger-py36/
========= FC invoke Logs begin =========FC Invoke Start RequestId: eb9cf022-297e-4a27-b3bf-ad304f6e04c9FC Invoke End RequestId: eb9cf022-297e-4a27-b3bf-ad304f6e04c9
Duration: 2.64 ms, Billed Duration: 3 ms, Memory Size: 128 MB, Max Memory Used: 10.77 MB========= FC invoke Logs end =========
FC Invoke Result[code: ${resp.code}]:Hello world!
复制代码


可观测性


日志查看


在当前项目下,直接使用 s logs 命令,可以进行日志查看,也可以通过 s logs -t 进入到 tail 模式:



FunctionCompute python3 runtime inited.

FC Invoke Start RequestId: eb9cf022-297e-4a27-b3bf-ad304f6e04c9FC Invoke End RequestId: eb9cf022-297e-4a27-b3bf-ad304f6e04c9
复制代码


指标查看


在当前项目,直接执行 s metrics 命令,可以进行指标的查看:


[2021-06-07T12:20:06.661] [INFO ] [FC-METRICS] - 请用浏览器访问Uri地址进行查看: http://localhost:3000
复制代码



发布于: 2021 年 12 月 15 日阅读数: 18
用户头像

刘宇

关注

阿里云Serverless云布道师 2020.01.04 加入

阿里云Serverless产品经理,国防科大在读博士,《Serverless架构》、《Serverless实践》、《人人都能学会的Serverless架构》等书籍作者,Serverless Devs发起人,Anycodes在线编程负责人。

评论

发布
暂无评论
Serverless Devs 新手引导