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

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

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:
BUCKET_NAME:
Ref: Bucket

resources:
Resources:
Bucket:
Type: AWS::S3::Bucket

Outputs:
BucketName:
Value:
Ref: Bucket
BucketDomainName:
Value:
Fn::GetAtt: [ Bucket, DomainName ]
  1. 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 = {
Bucket: process.env.BUCKET_NAME,
Key: `things/${thing.id}`,
ACL: 'public-read',
ContentType: 'application/json',
CacheControl: 'max-age=300',
Body: JSON.stringify(thing),
};

const s3 = new aws.S3();
return _(s3.putObject(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-materialized-view-s3@1.0.0 dp:lcl <path-to-your-workspace>/cncb-materialized-view-s3
> sls deploy -r us-east-1 "-s" "john"

Serverless: Packaging service...
...
Serverless: Stack update finished...
...
functions:
listener: cncb-materialized-view-s3-john-listener

Stack Outputs
BucketName: cncb-materialized-view-s3-john-bucket-1pp3d4c2z99kt
BucketDomainName: cncb-materialized-view-s3-john-bucket-1pp3d4c2z99kt.s3.amazonaws.com
...
  1. Review the stack, function, and bucket from the AWS Console.
  2. 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 three","id":"33333333-3333-3333-3333-333333333333"}}}'
{
"ShardId": "shardId-000000000000",
"SequenceNumber": "49583553996455686705785668952918833314346020725338406914"
}
  1. Take a look at the logs:
$ sls logs -f listener -r us-east-1 -s $MY_STAGE
START ...
2018-04-17 22:49:20 ... event: {"Records":[...]}
2018-04-17 22:49:20 ... {"type":"thing-created","thing":{"new":{"name":"thing three","id":"33333333-3333-3333-3333-333333333333"}},"id":"16a7b930-42b3-11e8-8700-a918e007d88a","partitionKey":"3de89e9d-c48d-4255-84fc-6c1b7e3f8b90","timestamp":1524019758148,"tags":{"region":"us-east-1"}}
2018-04-17 22:49:20 ... params: {"Bucket":"cncb-materialized-view-s3-john-bucket-1pp3d4c2z99kt","Key":"things/33333333-3333-3333-3333-333333333333","ACL":"public-read","ContentType":"application/json","CacheControl":"max-age=300","Body":"{\"id\":\"33333333-3333-3333-3333-333333333333\",\"name\":\"thing three\",\"asOf\":1524019758148}"}
2018-04-17 22:49:20 ... {"ETag":"\"edfee997659a520994ed18b82255be2a\""}
END ...
REPORT ... Duration: 167.66 ms Billed Duration: 200 ms ... Max Memory Used: 36 MB
  1. Invoke the following command, after updating the bucket-suffix, to get the data from S3:
$ curl https://s3.amazonaws.com/cncb-materialized-view-s3-$MY_STAGE-bucket-<bucket-suffix>/things/33333333-3333-3333-3333-333333333333 | json_pp
{
"asOf" : 1524019758148,
"name" : "thing three",
"id" : "33333333-3333-3333-3333-333333333333"
}
  1. Use the console to delete the objects from the bucket before removing the stack.
  2. Remove the stack once you have finished with npm run rm:lcl -- -s $MY_STAGE.
主站蜘蛛池模板: 体育| 会理县| 聂荣县| 澎湖县| 新绛县| 济宁市| 营山县| 江达县| 神木县| 福清市| 遵义县| 南澳县| 修武县| 周至县| 凉城县| 井陉县| 铜川市| 蚌埠市| 科尔| 白沙| 广昌县| 尚志市| 洛南县| 翁牛特旗| 正定县| 南京市| 大埔区| 武陟县| 梅州市| 融水| 长岛县| 贡嘎县| 广西| 鹤岗市| 杨浦区| 电白县| 云南省| 靖州| 霍城县| 荃湾区| 永宁县|