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

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.
主站蜘蛛池模板: 平定县| 阿合奇县| 澎湖县| 长岛县| 凤阳县| 太康县| 尉氏县| 沙雅县| 遂溪县| 长武县| 论坛| 西乌珠穆沁旗| 越西县| 即墨市| 信阳市| 遂宁市| 郴州市| 赞皇县| 漳浦县| 扎兰屯市| 北京市| 石棉县| 永靖县| 吉林省| 商城县| 广河县| 衡阳县| 五家渠市| 涟源市| 天气| 门源| 武陟县| 滦南县| 宁化县| 荆门市| 连山| 岳西县| 淳安县| 皮山县| 上饶市| 镇雄县|