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

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

provider:
name: aws
runtime: nodejs8.10
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:PutItem
Resource:
Fn::GetAtt: [ Table, Arn ]
- Effect: Allow
Action:
- kinesis:PutRecord
Resource: ${cf:cncb-event-stream-${opt:stage}.streamArn}

functions:
command:
handler: handler.command
environment:
TABLE_NAME:
Ref: Table
trigger:
handler: handler.trigger
events:
- stream:
type: dynamodb
arn:
Fn::GetAtt: [ Table, StreamArn ]
...
environment:
STREAM_NAME: ${cf:cncb-event-stream-${opt:stage}.streamName}

resources:
Resources:
Table:
Type: AWS::DynamoDB::Table
Properties:
TableName: ${opt:stage}-${self:service}-things
AttributeDefinitions:
...
KeySchema:
- AttributeName: id
KeyType: HASH
...
StreamSpecification:
StreamViewType: NEW_AND_OLD_IMAGES
  1. Review the file named handler.js with the following content:
module.exports.command = (request, context, callback) => {
const thing = {
id: uuid.v4(),
...request,
};

const params = {
TableName: process.env.TABLE_NAME,
Item: thing,
};

const db = new aws.DynamoDB.DocumentClient();
db.put(params, callback);
};

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

const toEvent = record => ({
id: record.eventID,
type: `thing-${EVENT_NAME_MAPPING[record.eventName]}`,
timestamp: record.dynamodb.ApproximateCreationDateTime * 1000,
partitionKey: record.dynamodb.Keys.id.S,
tags: {
region: record.awsRegion,
},
thing: {
old: record.dynamodb.OldImage ?
aws.DynamoDB.Converter.unmarshall(record.dynamodb.OldImage) :
undefined,
new: record.dynamodb.NewImage ?
aws.DynamoDB.Converter.unmarshall(record.dynamodb.NewImage) :
undefined,
},
});

const EVENT_NAME_MAPPING = {
INSERT: 'created',
MODIFY: 'updated',
REMOVE: 'deleted',
};

const publish = event => {
const params = {
StreamName: process.env.STREAM_NAME,
PartitionKey: event.partitionKey,
Data: Buffer.from(JSON.stringify(event)),
};

const kinesis = new aws.Kinesis();
return _(kinesis.putRecord(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-db-first-dynamodb@1.0.0 dp:lcl <path-to-your-workspace>/cncb-db-first-dynamodb
> sls deploy -r us-east-1 "-s" "john"

Serverless: Packaging service...
...
Serverless: Stack update finished...
...
functions:
command: cncb-db-first-dynamodb-john-command
trigger: cncb-db-first-dynamodb-john-trigger
  1. Review the stack, functions, and table in the AWS Console.
  2. Invoke the function with the following command:
$ sls invoke -r us-east-1 -f command -s $MY_STAGE -d '{"name":"thing one"}'
  1. Take a look at the command function logs:
$ sls logs -f command -r us-east-1 -s $MY_STAGE
START ...
2018-04-17 00:29:13 ... request: {"name":"thing one"}
2018-04-17 00:29:13 ... params: {"TableName":"john-cncb-db-first-dynamodb-things","Item":{"id":"4297c253-f512-443d-baaf-65f0a36aaaa3","name":"thing one"}}
END ...
REPORT ... Duration: 136.99 ms Billed Duration: 200 ms Memory Size: 1024 MB Max Memory Used: 35 MB
  1. Take a look at the trigger function logs:
$ sls logs -f trigger -r us-east-1 -s $MY_STAGE
START ...
2018-04-17 00:29:15 ... event: {"Records":[{"eventID":"39070dc13de0eb76548506a977d4134c","eventName":"INSERT",...,"dynamodb":{"ApproximateCreationDateTime":1523939340,"Keys":{"id":{"S":"4297c253-f512-443d-baaf-65f0a36aaaa3"}},"NewImage":{"name":{"S":"thing one"},"id":{"S":"4297c253-f512-443d-baaf-65f0a36aaaa3"}},"SequenceNumber":"100000000006513931753",...},...}]}
2018-04-17 00:29:15 ... {"id":"39070dc13de0eb76548506a977d4134c","type":"thing-created","timestamp":1523939340000,"partitionKey":"4297c253-f512-443d-baaf-65f0a36aaaa3","tags":{"region":"us-east-1"},"thing":{"new":{"name":"thing one","id":"4297c253-f512-443d-baaf-65f0a36aaaa3"}}}
2018-04-17 00:29:15 ... params: {"StreamName":"john-cncb-event-stream-s1","PartitionKey":"4297c253-f512-443d-baaf-65f0a36aaaa3","Data":{"type":"Buffer","data":[...]}}
2018-04-17 00:29:15 ... {"ShardId":"shardId-000000000000","SequenceNumber":"4958...3778"}
END ...
REPORT ... Duration: 326.99 ms Billed Duration: 400 ms ... Max Memory Used: 40 MB
  1. Review the events collected in the data lake bucket.
  2. Remove the stack once you have finished with npm run rm:lcl -- -s $MY_STAGE.
主站蜘蛛池模板: 登封市| 高碑店市| 玛曲县| 武邑县| 黄大仙区| 安达市| 华安县| 东乌| 泸州市| 化隆| 民乐县| 平潭县| 康定县| 泉州市| 高碑店市| 东方市| 阿克| 长沙县| 都安| 大港区| 德格县| 平谷区| 莱西市| 正安县| 波密县| 秭归县| 平顺县| 蚌埠市| 得荣县| 郧西县| 南平市| 桓台县| 荃湾区| 莆田市| 睢宁县| 阿拉善盟| 成安县| 璧山县| 锡林浩特市| 海盐县| 离岛区|