使用Serverless构建无服务器应用
借助 Vagrant 虚拟环境构建无服务器应用,实现客户端的deploy
应用示意图
客户端通过http访问 lambda 函数;
serverless实现客户端的 deploy
使用技术和工具
Vagrant--------------虚拟环境作成
VirtualBox-----------vagrant环境管理工具
AWS------------------API Gateway;Lambda;DynamoDB; AWS CLI(客户端的权限配置)
Serverless Framework----无服务应用开发的第三方framework
Node.js--------------构建 lambda 函数
虚拟环境建立
Vagrant的虚拟环境建立,参照: Vagrant-环境搭建.
NodeJs环境相关安装
NodeJs环境建立,参照: NodeJs环境相关安装.
配置AWS-CLI
利用 Serverless Framework deploy应用时,需要aws的认证信息,可以利用Serverless Framework的配置命令来配置,也可以使用AWS-CLI进行配置。
在这里使用AWS-CLI进行配置,将来如果使用EB-CLI时,也就不用配置了。
参照: AWS-CLI.
安装 Serverless Framework
[vagrant@localhost vagrant]$ pwd
/vagrant
[vagrant@localhost vagrant]$ sudo npm install -g serverless
[vagrant@localhost vagrant]$ sls -v
1.36.3
[vagrant@localhost vagrant]$ sls
Commands
* You can run commands with "serverless" or the shortcut "sls"
* Pass "--verbose" to this command to get in-depth plugin info
* Pass "--no-color" to disable CLI colors
* Pass "--help" after any <command> for contextual help
Framework
* Documentation: https://serverless.com/framework/docs/
config ........................ Configure Serverless
config credentials ............ Configures a new provider profile for the Serverless Framework
create ........................ Create new Serverless service
install ....................... Install a Serverless service from GitHub or a plugin from the Serverless registry
package ....................... Packages a Serverless service
deploy ........................ Deploy a Serverless service
deploy function ............... Deploy a single function from the service
deploy list ................... List deployed version of your Serverless Service
deploy list functions ......... List all the deployed functions and their versions
invoke ........................ Invoke a deployed function
invoke local .................. Invoke function locally
info .......................... Display information about the service
logs .......................... Output the logs of a deployed