- JavaScript Cloud Native Development Cookbook
- John Gilbert
- 407字
- 2021-07-16 18:03:33
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/materialized-view-cognito --path cncb-materialized-view-cognito
- Navigate to the cncb-materialized-view-cognito directory with cd cncb-materialized-view-cognito.
- 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 ]
- 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()
})
);
};
- 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-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
...
- Review the stack, function, and identity pool in the AWS Console.
-
Update the file named index.html file with the identityPoolId from previous output.
-
Open the file named index.html in a browser and copy the identity ID for use in the next step.
- 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"
}
- 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
Open the file named index.html in a browser and press the Synchronize button to retrieve the data from the materialized view:

- Remove the stack once you have finished with npm run rm:lcl -- -s $MY_STAGE.