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

How to do it...

  1. Create two projects from the following template:
$ sls create --template-url https://github.com/danteinc/js-cloud-native-cookbook/tree/master/ch2/bi-directional-sync --path cncb-1-bi-directional-sync

$ sls create --template-url https://github.com/danteinc/js-cloud-native-cookbook/tree/master/ch2/bi-directional-sync --path cncb-2-bi-directional-sync
  1. Review the file named serverless.yml with the following content in each project:
service: cncb-1-bi-directional-sync

provider:
name: aws
runtime: nodejs8.10
iamRoleStatements:
...
environment:
SERVERLESS_PROJECT: ${self:service}
...

functions:
command:
handler: handler.command
trigger:
handler: handler.trigger
events:
- stream:
type: dynamodb
...
listener:
handler: handler.listener
events:
- stream:
type: kinesis
...
query:
handler: handler.query

resources:
Resources:
Table:
...
  1. Review the file named handler.js with the following content:
module.exports.command = (request, context, callback) => {
const thing = {
id: uuid.v4(),
latch: 'open',
...request,
};

...
db.put(params, callback);
};

module.exports.trigger = (event, context, cb) => {
_(event.Records)
.filter(forLatchOpen)
.map(toEvent)
.flatMap(publish)
.collect()
.toCallback(cb);
};

const forLatchOpen = e => e.dynamodb.NewImage.latch.S === 'open';

const toEvent = record => ({
id: record.eventID,
...
tags: {
region: record.awsRegion,
source: process.env.SERVERLESS_PROJECT
},
thing: ...,
});

...

module.exports.listener = (event, context, cb) => {
_(event.Records)
.map(recordToEvent)
.filter(forSourceNotSelf)
.filter(forThingCrud)
.map(toThing)
.flatMap(put)
.collect()
.toCallback(cb);
};

...
const forSourceNotSelf = e => e.tags.source != process.env.SERVERLESS_PROJECT;
...

const toThing = event => ({
id: event.thing.new.id,
...
latch: 'closed',
});

...
  1. Navigate to the cncb-1-bi-directional-sync directory withcd cncb-1-bi-directional-sync.
  2. Install the dependencies with npm install.
  3. Run the tests with npm test -- -s $MY_STAGE.
  4. Review the contents generated in the .serverless directory.
  5. Deploy the stack:
$ npm run dp:lcl -- -s $MY_STAGE

> cncb-1-bi-directional-sync@1.0.0 dp:lcl <path-to-your-workspace>/cncb-1-bi-directional-sync
> sls deploy -r us-east-1 "-s" "john"

Serverless: Packaging service...
...
Serverless: Stack update finished...
...
functions:
command: cncb-1-bi-directional-sync-john-command
trigger: cncb-1-bi-directional-sync-john-trigger
listener: cncb-1-bi-directional-sync-john-listener
query: cncb-1-bi-directional-sync-john-query
  1. Review the stack, functions, and table in the AWS Console.
  2. Navigate to the cncb-2-bi-directional-sync directory with cd cncb-2-bi-directional-sync.
  3. Repeat steps 5-9 for the cncb-2-bi-directional-sync project.
  4. Navigate back to the cncb-1-bi-directional-sync directory with cd cncb-1-bi-directional-sync.
  5. Invoke the command function to save data to the first service:
$ sls invoke -r us-east-1 -f command -s $MY_STAGE -d '{"id":"77777777-7777-7777-7777-777777777777","name":"thing seven"}'
  1. Take a look at the logs for the command and trigger functions: 
$ sls logs -f command -r us-east-1 -s $MY_STAGE

START ...
2018-04-24 02:02:11 ... event: {"id":"77777777-7777-7777-7777-777777777777","name":"thing seven"}
2018-04-24 02:02:11 ... params: {"TableName":"john-cncb-1-bi-directional-sync-things","Item":{"id":"77777777-7777-7777-7777-777777777777","latch":"open","name":"thing seven"}}
END ...
REPORT ... Duration: 146.90 ms Billed Duration: 200 ms ... Max Memory Used: 40 MB

$ sls logs -f trigger -r us-east-1 -s $MY_STAGE
START ...
2018-04-24 02:02:13 ... event: {"Records":[{"eventID":"494ec22686941c0d5ff56dee86df47dd","eventName":"INSERT",...,"Keys":{"id":{"S":"77777777-7777-7777-7777-777777777777"}},"NewImage":{"name":{"S":"thing seven"},"id":{"S":"77777777-7777-7777-7777-777777777777"},"latch":{"S":"open"}},...},...}]}
2018-04-24 02:02:13 ... {"id":"494ec22686941c0d5ff56dee86df47dd","type":"thing-created",...,"tags":{"region":"us-east-1","source":"cncb-1-bi-directional-sync"},"thing":{"new":{"name":"thing seven","id":"77777777-7777-7777-7777-777777777777","latch":"open"}}}
...
END ...
REPORT ... Duration: 140.20 ms Billed Duration: 200 ms ... Max Memory Used: 35 MB
  1. Navigate to the cncb-2-bi-directional-sync directory with cd cncb-2-bi-directional-sync
  2. Take a look at the logs for the listener and trigger functions: 
$ sls logs -f listener -r us-east-1 -s $MY_STAGE

$ sls logs -f trigger -r us-east-1 -s $MY_STAGE
  1. Invoke the query function to retrieve the synchronized data to the second service:
$ sls invoke -r us-east-1 -f query -s $MY_STAGE -d 77777777-7777-7777-7777-777777777777
  1. Remove both stacks once you have finished with npm run rm:lcl -- -s $MY_STAGE.
主站蜘蛛池模板: 隆安县| 越西县| 临洮县| 白城市| 乐业县| 嘉鱼县| 酉阳| 纳雍县| 榕江县| 常德市| 炎陵县| 井陉县| 长武县| 滦南县| 四平市| 花莲市| 乐都县| 柳林县| 沂南县| 阳谷县| 化德县| 布尔津县| 徐水县| 瑞安市| 儋州市| 花垣县| 芒康县| 江山市| 青浦区| 新泰市| 武隆县| 山阴县| 景洪市| 阜新市| 宜丰县| 阳曲县| 九台市| 辽阳市| 清镇市| 黔江区| 黄骅市|