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

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

provider:
name: aws
runtime: nodejs8.10
iamRoleStatements:
...
environment:
DOMAIN_ENDPOINT:
Fn::GetAtt: [ Domain, DomainEndpoint ]

...

functions:
listener:
handler: handler.listener
events:
- stream:
type: kinesis
arn: ${cf:cncb-event-stream-${opt:stage}.streamArn}
...
search:
handler: handler.search

resources:
Resources:
Domain:
Type: AWS::Elasticsearch::Domain
Properties:
...

Outputs:
DomainName:
Value:
Ref: Domain
DomainEndpoint:
Value:
Fn::GetAtt: [ Domain, DomainEndpoint ]
  1. Review the file named handler.js with the following content:
const client = require('elasticsearch').Client({
hosts: [`https://${process.env.DOMAIN_ENDPOINT}`],
connectionClass: require('http-aws-es'),
log: 'trace',
});

module.exports.listener = (event, context, cb) => {
_(event.Records)
.map(recordToEvent)
.filter(forThingCreated)
.map(toThing)
.flatMap(index)
.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 index = thing => {
const params = {
index: 'things',
type: 'thing',
id: thing.id,
body: thing,
};

return _(client.index(params));
};

module.exports.search = (query, context, callback) => {
const params = {
index: 'things',
q: query,
};

client.search(params, callback);
};
  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:
Deploying an Elasticsearch domain can take upwards of 1-20 minutes.
$ npm run dp:lcl -- -s $MY_STAGE

> cncb-materialized-view-es@1.0.0 dp:lcl <path-to-your-workspace>/cncb-materialized-view-es
> sls deploy -v -r us-east-1 "-s" "john"

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

Stack Outputs
...
DomainEndpoint: search-cncb-ma-domain-gw419rzj26hz-p2g37av7sdlltosbqhag3qhwnq.us-east-1.es.amazonaws.com
DomainName: cncb-ma-domain-gw419rzj26hz
...
  1. Review the stack, function, and Elasticsearch domain in 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 four","id":"44444444-4444-4444-4444-444444444444"}}}'

{
"ShardId": "shardId-000000000000",
"SequenceNumber": "49583655996852917476267785004768832452002332571160543234"
}
  1. Take a look at the listener function logs:
$ sls logs -f listener -r us-east-1 -s $MY_STAGE

START ...
2018-04-19 01:54:33 ... {"type":"thing-created","thing":{"new":{"name":"thing four","id":"44444444-4444-4444-4444-444444444444"}},"id":"0e1a68c0-4395-11e8-b455-8144cebc5972","partitionKey":"8082a69c-00ee-4388-9697-c590c523c061","timestamp":1524116810060,"tags":{"region":"us-east-1"}}
2018-04-19 01:54:33 ... params: {"index":"things","type":"thing","id":"44444444-4444-4444-4444-444444444444","body":{"id":"44444444-4444-4444-4444-444444444444","name":"thing four","asOf":1524116810060}}
2018-04-19 01:54:33 ... {"_index":"things","_type":"thing","_id":"44444444-4444-4444-4444-444444444444","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
END ...
REPORT ... Duration: 31.00 ms Billed Duration: 100 ms ... Max Memory Used: 42 MB

  1. Invoke the search command:
$ sls invoke -r us-east-1 -f search -s $MY_STAGE -d four

{
...
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "things",
"_type": "thing",
"_id": "44444444-4444-4444-4444-444444444444",
"_score": 0.2876821,
"_source": {
"id": "44444444-4444-4444-4444-444444444444",
"name": "thing four",
"asOf": 1524116810060
}
}
]
}
}
  1. Remove the stack once you have finished with npm run rm:lcl -- -s $MY_STAGE.
主站蜘蛛池模板: 涿州市| 独山县| 巢湖市| 南投县| 泸溪县| 密山市| SHOW| 宜兴市| 周宁县| 鄱阳县| 百色市| 万山特区| 砚山县| 遂宁市| 津市市| 慈利县| 土默特左旗| 利津县| 慈利县| 扬中市| 通河县| 衡南县| 南康市| 潞西市| 洛扎县| 聂荣县| 荥经县| 石门县| 巴林左旗| 闸北区| 阜城县| 新沂市| 枣阳市| 阿拉尔市| 喀喇沁旗| 浪卡子县| 津南区| 商河县| 外汇| 成都市| 衡水市|