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

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

provider:
name: aws
runtime: nodejs8.10
iamRoleStatements:
...

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

resources:
Resources:
IdentityPool:
Type: AWS::Cognito::IdentityPool
...

Outputs:
identityPoolId:
Value:
Ref: IdentityPool
identityPoolName:
Value:
Fn::GetAtt: [ IdentityPool, Name ]
  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,
identityId: event.thing.new.identityId, // the end user
asOf: event.timestamp,
});

const put = thing => {
const params = {
IdentityPoolId: process.env.IDENTITY_POOL_ID,
IdentityId: thing.identityId,
DatasetName: 'things',
};

const cognitosync = new aws.CognitoSync();

return _(
cognitosync.listRecords(params).promise()
.then(data => {
params.SyncSessionToken = data.SyncSessionToken;
params.RecordPatches = [{
Key: 'thing',
Value: JSON.stringify(thing),
Op: 'replace',
SyncCount: data.DatasetSyncCount,
}];

return cognitosync.updateRecords(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-materialized-view-cognito@1.0.0 dp:lcl <path-to-your-workspace>/cncb-materialized-view-cognito
> sls deploy -v -r us-east-1 "-s" "john"

Serverless: Packaging service...
...
Serverless: Stack update finished...
...
functions:
listener: cncb-materialized-view-cognito-john-listener

Stack Outputs
identityPoolName: IdentityPool_c0GbzyVSh3Ws
identityPoolId: us-east-1:3a07e120-f1d8-4c85-9c34-0f908f2a21a1
...
  1. Review the stack, function, and identity pool in the AWS Console.
  2. Update the file named index.html file with the identityPoolId from previous output.
  3. Open the file named index.html in a browser and copy the identity ID for use in the next step.
  1. 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 five","id":"55555555-5555-5555-5555-555555555555", "identityId":"<identityId from previous step>"}}}'

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

START ...
2018-04-19 00:18:42 ... {"type":"thing-created","thing":{"new":{"name":"thing five","id":"55555555-5555-5555-5555-555555555555","identityId":"us-east-1:ee319396-fec4-424d-aa19-71ee751624d1"}},"id":"bda76e80-4388-11e8-a845-5902692b9264","partitionKey":"c9d4e9e5-d33f-4907-9a7a-af03710fa50f","timestamp":1524111521129,"tags":{"region":"us-east-1"}}
2018-04-19 00:18:42 ... params: {"IdentityPoolId":"us-east-1:3a07e120-f1d8-4c85-9c34-0f908f2a21a1","IdentityId":"us-east-1:ee319396-fec4-424d-aa19-71ee751624d1","DatasetName":"things"}
2018-04-19 00:18:43 ... {"Records":[{"Key":"thing","Value":"{\"id\":\"55555555-5555-5555-5555-555555555555\",\"name\":\"thing five\",\"asOf\":1524111521129,\"identityId\":\"us-east-1:ee319396-fec4-424d-aa19-71ee751624d1\"}","SyncCount":1,"LastModifiedDate":"2018-04-19T04:18:42.978Z","LastModifiedBy":"123456789012","DeviceLastModifiedDate":"2018-04-19T04:18:42.978Z"}]}
END ...
REPORT ... Duration: 340.94 ms Billed Duration: 400 ms ... Max Memory Used: 33 MB
  1. Open the file named index.html in a browser and press the Synchronize button to retrieve the data from the materialized view:

  1. Remove the stack once you have finished with npm run rm:lcl -- -s $MY_STAGE.
主站蜘蛛池模板: 左云县| 赤壁市| 清河县| 交城县| 西城区| 宝坻区| 平谷区| 临武县| 江门市| 阿尔山市| 赤城县| 贺兰县| 湖南省| 穆棱市| 通辽市| 弥渡县| 金山区| 青海省| 集安市| 苍溪县| 丰县| 汉阴县| 叶城县| 扎赉特旗| 临邑县| 山东省| 沅江市| 平山县| 达拉特旗| 湘阴县| 县级市| 城固县| 甘洛县| 乐昌市| 梁山县| 金门县| 海盐县| 顺义区| 台北市| 化州市| 库伦旗|