- JavaScript Cloud Native Development Cookbook
- John Gilbert
- 379字
- 2021-07-16 18:03:32
How to do it...
- 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
- Navigate to the cncb-materialized-view-s3 directory with cd cncb-materialized-view-s3.
- 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 ]
- 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());
};
- 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-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
...
- Review the stack, function, and bucket from 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 three","id":"33333333-3333-3333-3333-333333333333"}}}'
{
"ShardId": "shardId-000000000000",
"SequenceNumber": "49583553996455686705785668952918833314346020725338406914"
}
- 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
- 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"
}
- Use the console to delete the objects from the bucket before removing the stack.
- Remove the stack once you have finished with npm run rm:lcl -- -s $MY_STAGE.
推薦閱讀
- 高效變換器設(shè)計(jì)與應(yīng)用電路
- Serverless架構(gòu):無服務(wù)器應(yīng)用與AWS Lambda
- 未解之謎(下)
- Rust Standard Library Cookbook
- 通信線路工程設(shè)計(jì)、施工與維護(hù)(第2版)
- 快修巧修新型等離子和高清晰度電視機(jī)
- 從算法到電路:數(shù)字芯片算法的電路實(shí)現(xiàn)
- iOS應(yīng)用開發(fā)最佳實(shí)踐
- 精通Android 5 多媒體開發(fā)
- VoLTE端到端業(yè)務(wù)詳解
- 一個(gè)APP的誕生2.0:從零開始設(shè)計(jì)你的手機(jī)應(yīng)用
- 電子技術(shù)(第3版)
- 5G網(wǎng)絡(luò)全專業(yè)規(guī)劃設(shè)計(jì)寶典
- TD-SCDMA無線網(wǎng)絡(luò)創(chuàng)新技術(shù)與應(yīng)用
- Microduino實(shí)戰(zhàn)