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

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/micro-event-store --path cncb-micro-event-store
  1. Navigate to the cncb-micro-event-store directory with cd cncb-micro-event-store.
  2. Review the file named serverless.yml with the following content:
service: cncb-micro-event-store

provider:
name: aws
runtime: nodejs8.10
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:PutItem
- dynamodb:Query
Resource:
Fn::GetAtt: [ Table, Arn ]
environment:
TABLE_NAME:
Ref: Table

functions:
listener:
handler: handler.listener
events:
- stream:
type: kinesis
...
trigger:
handler: handler.trigger
events:
- stream:
type: dynamodb
arn:
Fn::GetAtt: [ Table, StreamArn ]
batchSize: 100
startingPosition: TRIM_HORIZON

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

...
const byType = event => event.type.matches(/thing-.+/);

const put = event => {
const params = {
TableName: process.env.TABLE_NAME,
Item: {
partitionKey: event.partitionKey,
eventId: event.id,
event: event,
}
};

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

module.exports.trigger = (event, context, cb) => {
_(event.Records)
.flatMap(getMicroEventStore)
.tap(events => console.log('events: %j', events))
.collect().toCallback(cb);
};

const getMicroEventStore = (record) => {
const params = {
TableName: process.env.TABLE_NAME,
KeyConditionExpression: '#partitionKey = :partitionKey',
ExpressionAttributeNames: {
'#partitionKey': 'partitionKey'
},
ExpressionAttributeValues: {
':partitionKey': record.dynamodb.Keys.partitionKey.S
}
};

const db = new aws.DynamoDB.DocumentClient();
return _(db.query(params).promise());
}
  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-micro-event-store@1.0.0 dp:lcl <path-to-your-workspace>/cncb-micro-event-store
> sls deploy -v -r us-east-1 "-s" "john"

Serverless: Packaging service...
...
Serverless: Stack update finished...
...
functions:
listener: cncb-micro-event-store-john-listener
trigger: cncb-micro-event-store-john-trigger
...
  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-updated","partitionKey":"11111111-1111-1111-1111-111111111111","thing":{"new":{"name":"thing one","id":"11111111-1111-1111-1111-111111111111"}}}'

{
"ShardId": "shardId-000000000000",
"SequenceNumber": "49583553996455686705785668952922460091805481438885707778"
}
  1. Take a look at the logs for the listener function:
$ sls logs -f listener -r us-east-1 -s $MY_STAGE

START ...
2018-04-18 01:18:55... {"type":"thing-updated","partitionKey":"11111111-1111-1111-1111-111111111111","thing":{"new":{"name":"thing one","id":"11111111-1111-1111-1111-111111111111"}},"id":"fcc03460-42c7-11e8-8756-f75e650b2731","timestamp":1524028734118,"tags":{"region":"us-east-1"}}
2018-04-18 01:18:55.394 (-04:00) b42aaa92-8a9a-418d-8e22-ecc54e9966f6 params: {"TableName":"john-cncb-micro-event-store-events","Item":{"partitionKey":"11111111-1111-1111-1111-111111111111","eventId":"fcc03460-42c7-11e8-8756-f75e650b2731","event":{"type":"thing-updated","partitionKey":"11111111-1111-1111-1111-111111111111","thing":{"new":{"name":"thing one","id":"11111111-1111-1111-1111-111111111111"}},"id":"fcc03460-42c7-11e8-8756-f75e650b2731","timestamp":1524028734118,"tags":{"region":"us-east-1"}}}}
END ...
REPORT ... Duration: 149.24 ms Billed Duration: 200 ms ... Max Memory Used: 35 MB

  1. Take a look at the logs for the trigger function: 
$ sls logs -f trigger -r us-east-1 -s $MY_STAGE

START ...
2018-04-18 01:18:56 ... event: {"Records":[{"eventID":"b8dbee2d9f49ee05609a7e930ac204e7","eventName":"INSERT",...,"Keys":{"eventId":{"S":"fcc03460-42c7-11e8-8756-f75e650b2731"},"partitionKey":{"S":"11111111-1111-1111-1111-111111111111"}},...}]}
2018-04-18 01:18:56 ... params: {"TableName":"john-cncb-micro-event-store-events",...,"ExpressionAttributeValues":{":partitionKey":"11111111-1111-1111-1111-111111111111"}}
2018-04-18 01:18:56 ... events: {"Items":[{"eventId":"2a1f5290-42c0-11e8-a06b-33908b837f8c","partitionKey":"11111111-1111-1111-1111-111111111111","event":{"id":"2a1f5290-42c0-11e8-a06b-33908b837f8c","type":"thing-submitted","partitionKey":"11111111-1111-1111-1111-111111111111","thing":{"name":"thing one","kind":"other","id":"11111111-1111-1111-1111-111111111111"},"timestamp":1524025374265,"tags":{"region":"us-east-1","kind":"other"}}},{"eventId":"fcc03460-42c7-11e8-8756-f75e650b2731","partitionKey":"11111111-1111-1111-1111-111111111111","event":{"id":"fcc03460-42c7-11e8-8756-f75e650b2731","type":"thing-updated","partitionKey":"11111111-1111-1111-1111-111111111111","thing":{"new":{"name":"thing one","id":"11111111-1111-1111-1111-111111111111"}},"timestamp":1524028734118,"tags":{"region":"us-east-1"}}}],"Count":2,"ScannedCount":2}
END ...
REPORT ... Duration: 70.88 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 42 MB
  1. Remove the stack once you have finished with npm run rm:lcl -- -s $MY_STAGE.
主站蜘蛛池模板: 中方县| 武冈市| 宁阳县| 余干县| 宁强县| 泰顺县| 田东县| 正宁县| 临邑县| 上高县| 长汀县| 合肥市| 蒙城县| 文成县| 抚顺市| 米泉市| 东港市| 雷山县| 柳州市| 灵石县| 华阴市| 汨罗市| 嘉义市| 乌恰县| 汉源县| 南宫市| 清镇市| 江门市| 石门县| 漳平市| 广东省| 镇坪县| 牡丹江市| 鄂托克旗| 尼玛县| 辉南县| 嵩明县| 宽甸| 南溪县| 迁安市| 镇赉县|