- Create the project from the following template:
$ sls create --template-url https://github.com/danteinc/js-cloud-native-cookbook/tree/master/ch2/materialized-view-dynamodb --path cncb-materialized-view-dynamodb
- Navigate to the cncb-materialized-view-dynamodb directory with cd cncb-materialized-view-dynamodb.
- Review the file named serverless.yml with the following content:
service: cncb-materialized-view-dynamodb
provider:
name: aws
runtime: nodejs8.10
iamRoleStatements:
...
environment:
TABLE_NAME:
Ref: Table
functions:
listener:
handler: handler.listener
events:
- stream:
type: kinesis
arn: ${cf:cncb-event-stream-${opt:stage}.streamArn}
...
query:
handler: handler.query
resources:
Resources:
Table:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${opt:stage}-${self:service}-things
AttributeDefinitions:
...
KeySchema:
- AttributeName: id
KeyType: HASH
...
- 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,
asOf: event.timestamp,
});
const put = thing => {
const params = {
TableName: process.env.TABLE_NAME,
Item: thing,
};
const db = new aws.DynamoDB.DocumentClient();
return _(db.put(params).promise());
};
module.exports.query = (id, context, callback) => {
const params = {
TableName: process.env.TABLE_NAME,
Key: {
id: id,
},
};
const db = new aws.DynamoDB.DocumentClient();
db.get(params, callback);
};
- 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-dynamodb@1.0.0 dp:lcl <path-to-your-workspace>/cncb-materialized-view-dynamodb
> sls deploy -r us-east-1 "-s" "john"
Serverless: Packaging service...
...
Serverless: Stack update finished...
...
functions:
listener: cncb-materialized-view-dynamodb-john-listener
query: cncb-materialized-view-dynamodb-john-query
- 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-created","thing":{"new":{"name":"thing two","id":"22222222-2222-2222-2222-222222222222"}}}'
{
"ShardId": "shardId-000000000000",
"SequenceNumber": "49583553996455686705785668952916415462701426537440215042"
}
- Take a look at the listener function logs:
$ sls logs -f listener -r us-east-1 -s $MY_STAGE
START ...
2018-04-17 00:54:48 ... event: {"Records":[...]}
2018-04-17 00:54:48 ... {"id":"39070dc13de0eb76548506a977d4134c","type":"thing-created","timestamp":1523939340000,"tags":{"region":"us-east-1"},"thing":{"new":{"name":"thing two","id":"22222222-2222-2222-2222-222222222222"}}}
2018-04-17 00:54:48 ... params: {"TableName":"john-cncb-materialized-view-dynamodb-things","Item":{"id":"22222222-2222-2222-2222-222222222222","name":"thing two","asOf":1523939340000}}
END ...
REPORT ... Duration: 306.17 ms Billed Duration: 400 ms ... Max Memory Used: 36 MB
- Invoke the query command:
$ sls invoke -r us-east-1 -f query -s $MY_STAGE -d 22222222-2222-2222-2222-222222222222
{
"Item": {
"id": "22222222-2222-2222-2222-222222222222",
"name": "thing two",
"asOf": 1523939340000
}
}
- Remove the stack once you are finished with npm run rm:lcl -- -s $MY_STAGE.