- JavaScript Cloud Native Development Cookbook
- John Gilbert
- 408字
- 2021-07-16 18:03:34
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/data-lake-es --path cncb-data-lake-es
- Navigate to the cncb-data-lake-es directory with cd cncb-data-lake-es.
- Review the file named serverless.yml with the following content:
service: cncb-data-lake-es
provider:
name: aws
runtime: nodejs8.10
plugins:
- elasticsearch
functions:
transformer:
handler: handler.transform
timeout: 120
resources:
Resources:
Domain:
Type: AWS::Elasticsearch::Domain
Properties:
...
DeliveryStream:
Type: AWS::KinesisFirehose::DeliveryStream
Properties:
DeliveryStreamType: KinesisStreamAsSource
KinesisStreamSourceConfiguration:
KinesisStreamARN: ${cf:cncb-event-stream-${opt:stage}.streamArn}
...
ElasticsearchDestinationConfiguration:
DomainARN:
Fn::GetAtt: [ Domain, DomainArn ]
IndexName: events
IndexRotationPeriod: OneDay
TypeName: event
BufferingHints:
IntervalInSeconds: 60
SizeInMBs: 50
RetryOptions:
DurationInSeconds: 60
...
ProcessingConfiguration: ${file(includes.yml):ProcessingConfiguration}
Bucket:
Type: AWS::S3::Bucket
...
Outputs:
...
DomainEndpoint:
Value:
Fn::GetAtt: [ Domain, DomainEndpoint ]
KibanaEndpoint:
Value:
Fn::Join:
- ''
- - Fn::GetAtt: [ Domain, DomainEndpoint ]
- '/_plugin/kibana'
...
- Review the file named handler.js with the following content:
exports.transform = (event, context, callback) => {
const output = event.records.map((record, i) => {
// store all available data
const uow = {
event: JSON.parse((Buffer.from(record.data, 'base64')).toString('utf8')),
kinesisRecordMetadata: record.kinesisRecordMetadata,
firehoseRecordMetadata: {
deliveryStreamArn: event.deliveryStreamArn,
region: event.region,
invocationId: event.invocationId,
recordId: record.recordId,
approximateArrivalTimestamp: record.approximateArrivalTimestamp,
}
};
return {
recordId: record.recordId,
result: 'Ok',
data: Buffer.from(JSON.stringify(uow), 'utf-8').toString('base64'),
};
});
callback(null, { records: output });
};
- 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:
Deploying an Elasticsearch domain can take upwards of 20 minutes.
$ npm run dp:lcl -- -s $MY_STAGE
> cncb-data-lake-es@1.0.0 dp:lcl <path-to-your-workspace>/cncb-data-lake-es
> sls deploy -v -r us-east-1 "-s" "john"
Serverless: Packaging service...
...
Serverless: Stack update finished...
...
functions:
transformer: cncb-data-lake-es-john-transformer
Stack Outputs
DeliveryStream: cncb-data-lake-es-john-DeliveryStream-1ME9ZI78H3347
DomainEndpoint: search-cncb-da-domain-5qx46izjweyq-oehy3i3euztbnog4juse3cmrs4.us-east-1.es.amazonaws.com
DeliveryStreamArn: arn:aws:firehose:us-east-1:123456789012:deliverystream/cncb-data-lake-es-john-DeliveryStream-1ME9ZI78H3347
KibanaEndpoint: search-cncb-da-domain-5qx46izjweyq-oehy3i3euztbnog4juse3cmrs4.us-east-1.es.amazonaws.com/_plugin/kibana
DomainArn: arn:aws:es:us-east-1:123456789012:domain/cncb-da-domain-5qx46izjweyq
...
- Review the stack, function, and Elasticsearch domain in 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"}'
{
"ShardId": "shardId-000000000000",
"SequenceNumber": "49583655996852917476267785049074754815059037929823272962"
}
Allow the Firehose buffer time to process, as the buffer interval is 60 seconds.
- Take a look at the transformer function logs:
$ sls logs -f transformer -r us-east-1 -s $MY_STAGE
- Open Kibana using the preceding KibanaEndpoint output with protocol https.
- Select the Management menu, set the index pattern to events-*, and press the Next step button.
- Select timestamp as the Time Filter field name, and press Create Index pattern.
- Select the Discover menu to view the current events in the index.
- Remove the stack once you are finished with npm run rm:lcl -- -s $MY_STAGE.