通过 Serverless Devs 快速创建函数计算应用(一)
2022-03-07
218
简介:
通过 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获取页面 获取AccountId :

- 打开 获取密钥页面 获取密钥信息 :

执行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
返回给我们的域名,进行测试。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。