前言
aws和阿里云等服务商都提供云函数服务。基本构架大同小异。所以出现了一些工具来抹平并方便管理serverless服务,比如github.com/serverless/… 和 docs.serverless-devs.com/serverless-…
一、CICD部署aws的lambda
方法有两种:
- 用aws的code pipeline
- 用github actions安装severless命令
下面描述一下方法2.
Step1:创建IAM用户
- 因为需要多个资源包括lambda,cloudformation等,因此可以简单的选择 AdministratorAccess
- 当然具体的可以选择,如下权限
Step2:配置serverless.yml
- provider表示aws和阿里云等,创建的是application
- functions,对应的是云函数,包括入口文件和最终链接等
- plugin表示部署的时候可以用插件处理代码,比如webpack(需要提供webpack.config)
- layers表示chrome pptr需要单独处理,防止代码体积过大
Step3:配置package.json
- 设置node version 12,其他版本不行,会报错
- 局部安装serverless(没有全局安装)
Step4: 配置action.yml
- 安装pkgs,因为需要serverless cli
- 运行sls来deploy,需要配置credentials
- 不能用serverless/github-actions, 因为这个actions版本有问题,会报错,因此选择原始的sls命令来部署
- 如果全局安装serverless,则不用yarn install和构建,因为只要部署到lambda,会触发plugin,自动根据webpack来构建(而不是发生在cicd阶段)
备注
- 错误1
FUNCTION_ERROR_INIT_FAILURE plainly means there's something wrong with the function's handler/code that i'm trying to deploy, w/c is why provisioned lambdas can't start up/initialize.
The way to resolve this, is to test w/o provisioned concurrency option first. Once you are able to push your lambda, error(s) will surely flow into your CW logs. The best way though, is to test your lambda locally(using serverless-offline plugin or serverless invoke), if it works properly. You can also package your app, and invoke it with serverless cli to detect issues on packaging.
代码无法启动,先修改代码,不然concurrency设置会报错,因为无法启动。
【完了】