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

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

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

functions:
trigger:
handler: handler.trigger
events:
- stream:
type: kinesis
arn:
Fn::GetAtt: [ CognitoStream, Arn ]
...
environment:
STREAM_NAME: ${cf:cncb-event-stream-${opt:stage}.streamName}

resources:
Resources:
CognitoStream:
Type: AWS::Kinesis::Stream
Properties:
ShardCount: 1

IdentityPool:
Type: AWS::Cognito::IdentityPool
Properties:
CognitoStreams:
StreamName:
Ref: CognitoStream
...

Outputs:
identityPoolId:
Value:
Ref: IdentityPool
identityPoolName:
Value:
Fn::GetAtt: [ IdentityPool, Name ]
  1. Review the file named handler.js with the following content:
module.exports.trigger = (event, context, cb) => {
_(event.Records)
.flatMap(recordToSync)
.map(toEvent)
.flatMap(publish)
.collect().toCallback(cb);
};

const recordToSync = r => {
const data = JSON.parse(Buffer.from(r.kinesis.data, 'base64'));
return _(data.kinesisSyncRecords.map(sync => ({
record: r,
data: data,
sync: sync,
thing: JSON.parse(sync.value)
})));
}

const toEvent = uow => ({
id: uuid.v1(),
type: `thing-created`,
timestamp: uow.sync.lastModifiedDate,
partitionKey: uow.thing.id,
tags: {
region: uow.record.awsRegion,
identityPoolId: uow.data.identityPoolId,
datasetName: uow.data.datasetName
},
thing: {
identityId: uow.data.identityId, // the end user
...uow.thing,
},
raw: uow.sync
});

...
  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-cognito@1.0.0 dp:lcl <path-to-your-workspace>/cncb-db-first-cognito
> sls deploy -v -r us-east-1 "-s" "john"

Serverless: Packaging service...
...
Serverless: Stack update finished...
...
functions:
trigger: cncb-db-first-cognito-john-trigger

Stack Outputs
identityPoolName: IdentityPool_P6awUWzjQH0y
identityPoolId: us-east-1:e51ba12c-75c2-4548-868d-2d023eb9398b
...
  1. Review the stack, function, and identity pool in the AWS Console.
  2. Update the file named ./index.html with the  identityPoolId from the previous output.
  1. Open the file named ./index.html in a browser, enter a name and description, and press Save and then Synchronize, as shown in the following screenshot:
  1. Take a look at the logs:
$ sls logs -f trigger -r us-east-1 -s $MY_STAGE

START ...
2018-04-19 00:50:15 ... {"id":"2714e290-438d-11e8-b3de-2bf7e0b964a2","type":"thing-created","timestamp":1524113413268,"partitionKey":"fd398c3b-8199-fd26-8c3c-156bb7ae8feb","tags":{"region":"us-east-1","identityPoolId":"us-east-1:e51ba12c-75c2-4548-868d-2d023eb9398b","datasetName":"things"},"thing":{"identityId":"us-east-1:28a2c685-2822-472e-b42a-f7bd1f02545a","id":"fd398c3b-8199-fd26-8c3c-156bb7ae8feb","name":"thing six","description":"the sixth thing"},"raw":{"key":"thing","value":"{\"id\":\"fd398c3b-8199-fd26-8c3c-156bb7ae8feb\",\"name\":\"thing six\",\"description\":\"the sixth thing\"}","syncCount":1,"lastModifiedDate":1524113413268,"deviceLastModifiedDate":1524113410528,"op":"replace"}}
2018-04-19 00:50:15 ... params: {"StreamName":"john-cncb-event-stream-s1","PartitionKey":"fd398c3b-8199-fd26-8c3c-156bb7ae8feb","Data":{"type":"Buffer","data":[...]}}
END ...
REPORT ... Duration: 217.22 ms Billed Duration: 300 ms ... Max Memory Used: 40 MB
  1. Remove the stack once you have finished with npm run rm:lcl -- -s $MY_STAGE
主站蜘蛛池模板: 斗六市| 寻甸| 庄浪县| 德化县| 敖汉旗| 罗甸县| 岳阳县| 彭泽县| 海林市| 女性| 内乡县| 屏东县| 青州市| 侯马市| 紫阳县| 仪征市| 阿坝县| 聊城市| 海城市| 鹤山市| 乌兰察布市| 德江县| 乌海市| 泸溪县| 金平| 长兴县| 丹江口市| 油尖旺区| 正定县| 孙吴县| 鹤壁市| 桃源县| 天门市| 申扎县| 纳雍县| 华容县| 柳河县| 镶黄旗| 外汇| 石河子市| 阜康市|