上一节中,我们部署了API Gateway并使用它的URL进行验证测试。能不能在本地启动API服务进行测试呢?答案是肯定的
使用sam local start-api命令,可以在本地启动一台API Gateway实例以进行HTTP request/response测试。
在项目目录下运行:
sam local start-api
运行后,SAM会查找template.yaml中Event字段的Api/HttpApi字义,并将其映射到HTTP路径下。默认跑在本地3000端口:

启动新的命令行终端,访问该地址,得到预期的返回结果:

在start-api的控制台上,能够实时查看到对应的日志:

hello-world/app.js文件中定义了接口返回的内容:message: 'hello world',  将其更新:

此时如果想测试更新的效果,需要重新build,再启动api-server:
sam build
sam local start-api
此时访问本地接口,得到更新后的测试结果:
kongpingfan:~/environment $ curl -w '\n' http://127.0.0.1:3000/hello
{"message":"hello world updated"}
参考: