官术网_书友最值得收藏!

How to do it...

  1. Create the project from the following template:
$ sls create --template-url https://github.com/danteinc/js-cloud-native-cookbook/tree/master/ch2/materialized-view-dynamodb --path cncb-materialized-view-dynamodb
  1. Navigate to the cncb-materialized-view-dynamodb directory with cd cncb-materialized-view-dynamodb.
  2. Review the file named serverless.yml with the following content:
service: cncb-materialized-view-dynamodb

provider:
name: aws
runtime: nodejs8.10
iamRoleStatements:
...
environment:
TABLE_NAME:
Ref: Table

functions:
listener:
handler: handler.listener
events:
- stream:
type: kinesis
arn: ${cf:cncb-event-stream-${opt:stage}.streamArn}
...
query:
handler: handler.query

resources:
Resources:
Table:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${opt:stage}-${self:service}-things
AttributeDefinitions:
...
KeySchema:
- AttributeName: id
KeyType: HASH
...
  1. Review the file named handler.js with the following content:
module.exports.listener = (event, context, cb) => {
_(event.Records)
.map(recordToEvent)
.filter(forThingCreated)
.map(toThing)
.flatMap(put)
.collect()
.toCallback(cb);
};

...
const forThingCreated = e => e.type === 'thing-created';

const toThing = event => ({
id: event.thing.new.id,
name: event.thing.new.name,
description: event.thing.new.description,
asOf: event.timestamp,
});

const put = thing => {
const params = {
TableName: process.env.TABLE_NAME,
Item: thing,
};

const db = new aws.DynamoDB.DocumentClient();
return _(db.put(params).promise());
};

module.exports.query = (id, context, callback) => {
const params = {
TableName: process.env.TABLE_NAME,
Key: {
id: id,
},
};

const db = new aws.DynamoDB.DocumentClient();
db.get(params, callback);
};
  1. Install the dependencies with npm install.
  2. Run the tests with npm test -- -s $MY_STAGE.
  3. Review the contents generated in the .serverless directory.
  4. Deploy the stack:
$ npm run dp:lcl -- -s $MY_STAGE

> cncb-materialized-view-dynamodb@1.0.0 dp:lcl <path-to-your-workspace>/cncb-materialized-view-dynamodb
> sls deploy -r us-east-1 "-s" "john"

Serverless: Packaging service...
...
Serverless: Stack update finished...
...
functions:
listener: cncb-materialized-view-dynamodb-john-listener
query: cncb-materialized-view-dynamodb-john-query
  1. Review the stack, functions, and table in the AWS Console.
  2. Publish an event from a separate Terminal with the following commands:
$ cd <path-to-your-workspace>/cncb-event-stream
$ sls invoke -r us-east-1 -f publish -s $MY_STAGE -d '{"type":"thing-created","thing":{"new":{"name":"thing two","id":"22222222-2222-2222-2222-222222222222"}}}'
{
"ShardId": "shardId-000000000000",
"SequenceNumber": "49583553996455686705785668952916415462701426537440215042"
}
  1. Take a look at the listener function logs:
$ sls logs -f listener -r us-east-1 -s $MY_STAGE
START ...
2018-04-17 00:54:48 ... event: {"Records":[...]}
2018-04-17 00:54:48 ... {"id":"39070dc13de0eb76548506a977d4134c","type":"thing-created","timestamp":1523939340000,"tags":{"region":"us-east-1"},"thing":{"new":{"name":"thing two","id":"22222222-2222-2222-2222-222222222222"}}}
2018-04-17 00:54:48 ... params: {"TableName":"john-cncb-materialized-view-dynamodb-things","Item":{"id":"22222222-2222-2222-2222-222222222222","name":"thing two","asOf":1523939340000}}
END ...
REPORT ... Duration: 306.17 ms Billed Duration: 400 ms ... Max Memory Used: 36 MB
  1. Invoke the query command:
$ sls invoke -r us-east-1 -f query -s $MY_STAGE -d 22222222-2222-2222-2222-222222222222
{
"Item": {
"id": "22222222-2222-2222-2222-222222222222",
"name": "thing two",
"asOf": 1523939340000
}
}
  1. Remove the stack once you are finished with npm run rm:lcl -- -s $MY_STAGE.
主站蜘蛛池模板: 弋阳县| 崇左市| 丰顺县| 南和县| 商南县| 恭城| 土默特右旗| 澄江县| 陆川县| 山东省| 平凉市| 昭平县| 洪泽县| 屯门区| 读书| 盐亭县| 繁昌县| 江北区| 来安县| 彰化市| 邵阳市| 竹北市| 图们市| 信宜市| 酒泉市| 绥德县| 读书| 黄浦区| 东阿县| 淮滨县| 罗甸县| 白河县| 南宫市| 建瓯市| 珠海市| 田林县| 河源市| 平舆县| 利津县| 金华市| 公主岭市|