- JavaScript Cloud Native Development Cookbook
- John Gilbert
- 440字
- 2021-07-16 18:03:29
How to do it...
- 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
- Navigate to the cncb-micro-event-store directory with cd cncb-micro-event-store.
- 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
- 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());
}
- Install the dependencies with npm install.
- Run the tests with npm test -- -s $MY_STAGE.
- Review the contents generated in the .serverless directory.
- 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
...
- Review the stack, functions, and table in the AWS Console.
- 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"
}
- 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
- 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
- Remove the stack once you have finished with npm run rm:lcl -- -s $MY_STAGE.
推薦閱讀
- 常用元器件的識(shí)別與檢測(cè)
- 電子產(chǎn)品印制電路板制作技能演練
- 元器件易學(xué)通:常用器件分冊(cè)
- 面向5G的蜂窩物聯(lián)網(wǎng)(CIoT)規(guī)劃設(shè)計(jì)及應(yīng)用
- PLC控制技術(shù)(西門子S7-200)
- 電子技術(shù)(第3版)
- 通信原理教程
- 輕松跟我學(xué)Protel 99SE電路設(shè)計(jì)與制版
- 平板電視機(jī)故障檢修實(shí)例
- 數(shù)字平板電視機(jī)現(xiàn)場(chǎng)維修實(shí)錄
- 電子工程師必備——元器件應(yīng)用寶典
- 小基站(Small Cell)無(wú)線網(wǎng)絡(luò)規(guī)劃與設(shè)計(jì)
- 應(yīng)用商店核心用戶在線評(píng)論對(duì)消費(fèi)者更新決策的影響研究
- 現(xiàn)代通信工程制圖與概預(yù)算
- LTE網(wǎng)絡(luò)規(guī)劃設(shè)計(jì)手冊(cè)(“十二五”國(guó)家重點(diǎn)圖書出版規(guī)劃項(xiàng)目)