参照下面的链接,实现的是云上的开发。
链接: Lambda-DynamoDB + API Gateway + Serverless Framework 的简单实例.
API Gateway; 本地使用 serverless-offline 插件模拟
DynamoDB ; 本地使用 serverless-dynamodb-local 插件安装 DynamoDB Local
[vagrant@localhost sampleSls]$ sudo npm install --save-dev serverless-offline
[vagrant@localhost sampleSls]$ sudo npm install --save-dev serverless-dynamodb-local
$ vi serverless.yml
# service: 内定义追加
plugins:
- serverless-dynamodb-local
- serverless-offline
[vagrant@localhost sampleSls]$ sls dynamodb install
运行上面的命令,画面未显示任何结果
serverless-dynamodb-local 的 0.2.31 有·bug,指定版本 0.2.30 重新安装
[vagrant@localhost sampleSls]$ sudo npm uninstall serverless-dynamodb-local
[vagrant@localhost sampleSls]$ sudo npm install serverless-dynamodb-local@0.2.30
Installation complete!
测试表创建时,追加record
$ mkdir db
$ vi db/db.json
[
{
"id": "004",
"name": "test04FormLocal"
}
]
api-gateway的端口设置为 4000 (测试用,任意)
custom:
serverless-offline:
port: 4000
dynamodb:
start:
port: 8000
inMemory: true
migrate: true
seed: true
seed:
development:
sources:
- table: sls-user
sources: [./db/db.json]
[vagrant@localhost sampleSls]$ sls dynamodb start
[vagrant@localhost sampleSls]$ sls dynamodb start
events.js:183
throw er; // Unhandled 'error' event
^
Error: spawn java ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:190:19)
at onErrorNT (internal/child_process.js:362:16)
at _combinedTickCallback (internal/process/next_tick.js:139:11)
at process._tickDomainCallback (internal/process/next_tick.js:219:9)
java 没有安装
[vagrant@localhost sampleSls]$ java --version
-bash: java: command not found
需要java环境
[vagrant@localhost sampleSls]$ sudo yum install java-1.8.0-openjdk
[vagrant@localhost sampleSls]$ sudo yum install java-1.8.0-openjdk-devel
path设置
[vagrant@localhost sampleSls]$ dirname $(readlink $(readlink $(which java)))
[vagrant@localhost sampleSls]$ export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.191.b12-1.el7_6.x86_64
export PATH=$PATH:$JAVA_HOME/bin
[vagrant@localhost sampleSls]$ java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
[vagrant@localhost sampleSls]$ sls dynamodb start
Dynamodb Local Started, Visit: http://localhost:8000/shell
[vagrant@localhost sampleSls]$ sls offline
Serverless: Starting Offline: dev/ap-northeast-1.
Serverless: Routes for hello:
Serverless: (none)
Serverless: Routes for listUser:
Serverless: GET /user
Serverless: Offline listening on http://localhost:4000
[vagrant@localhost ~]$ curl http://localhost:4000/user
{"users":[{"name":"test04FormLocal","id":"004"}]}
[vagrant@localhost sampleSls]$ sls remove -v