Serverless 应用的一个不方便之处:缺少一个本地的调试环境。在之前的那篇《Serverless 架构应用开发:使用 serverless-offline 在本地部署与调试》中, 我们提到了使用 serverless-offline
插件来在本地部署和调试。在本文中,我们将介绍 serverless-plugin-simulate
插件来解决相似的问题。
serverless-plugin-simulate
是一个的概念证明,用来尝试使用 docker 镜像复制 Amazon API Gateway 来运行lambda。
它可以支持以下的功能:
那么,让我们来试试使用 serverless-plugin-simulate
插件来模拟 Lambda 环境。
注意
:由于这里需要使用 Docker,建议读者先行安装 Docker。
然后,让我们来创建 Serverless 应用:
serverless create --template aws-nodejs --path simulate-lambda
接着,就可以安装 serverless-plugin-simulate 插件了
yarn add --dev serverless-plugin-simulate
然后,添加到 serverless.yml
中:
plugins:
- serverless-plugin-simulate
custom:
simulate:
services: docker-compose.yml
上面的配置中依赖于 docker-compose.yml
文件,创建、然后输入:
version: '2'
保存,并退出。
紧接着,运行:docker pull lambci/lambda
。
$ docker pull lambci/lambda
Using default tag: latest
latest: Pulling from lambci/lambda
5aed7bd8313c: Pull complete
d60049111ce7: Pull complete
7791f7ad5cf2: Pull complete
Digest: sha256:4d511dfc1a264ccc69081ceb00116dd0bea380080ad1e89c2f48752f6c4670df
Status: Downloaded newer image for lambci/lambda:latest
它将从服务端下载 lambci 的 lambda 镜像。
然后运行 simlaute
$ sls simulate apigateway -p 5000
Serverless: Starting mock services.
Serverless:
Serverless: [GET /undefined] => λ:hello
Serverless: Invoke URL: http://localhost:5000
Serverless: HTTP Event Not Found: Try checking your serverless.yml
发现少了一个入口,于是在 serverless.yml
中添加了路径
于是:
functions:
hello:
handler: handler.hello
events:
- http:
method: get
path: hello
接着再次运行,然后访问:http://localhost:5000/hello
$ npm start
> simulate@1.0.0 start /Users/fdhuang/learing/serverless-guide/simulate
> sls simulate apigateway -p 5000
Serverless: Starting mock services.
Serverless:
Serverless: [GET /hello] => λ:hello
Serverless: Invoke URL: http://localhost:5000
Serverless: Creating event
Serverless: Invoking hello
Serverless: Invoking function handler.hello
START RequestId: 18b5b89b-7118-1344-7f1e-a3b49c5c26d4 Version: $LATEST
END RequestId: 18b5b89b-7118-1344-7f1e-a3b49c5c26d4
REPORT RequestId: 18b5b89b-7118-1344-7f1e-a3b49c5c26d4 Duration: 6.27 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 28 MB
{"statusCode":200,"body":"{\"message\":\"Go Serverless v1.0! Your function executed successfully!\"}
Serverless: Mapping response
GET /hello 200 1518.527 ms - 1802
Serverless: HTTP Event Not Found: Try checking your serverless.yml
GET /favicon.ico 403 1.084 ms - 23
HTTP Event Not Found: Try checking your serverless.yml
就可以得到类似于生产环境的 Lambda 函数的结果。
围观我的Github Idea墙, 也许,你会遇到心仪的项目