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

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

provider:
name: aws
runtime: nodejs8.10
iamRoleStatements:
- Effect: Allow
Action:
- kinesis:PutRecord
Resource: ${cf:cncb-event-stream-${opt:stage}.streamArn}

functions:
submit:
handler: handler.submit
environment:
STREAM_NAME: ${cf:cncb-event-stream-${opt:stage}.streamName}
  1. Review the file named handler.js with the following content:
module.exports.submit = (thing, context, callback) => {
thing.id = thing.id || uuid.v4();

const event = {
type: 'thing-submitted',
id: uuid.v1(),
partitionKey: thing.id,
timestamp: Date.now(),
tags: {
region: process.env.AWS_REGION,
kind: thing.kind,
},
thing: thing,
};

const params = {
StreamName: process.env.STREAM_NAME,
PartitionKey: event.partitionKey,
Data: Buffer.from(JSON.stringify(event)),
};

const kinesis = new aws.Kinesis();

kinesis.putRecord(params, (err, resp) => {
callback(err, event);
});
};
  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-event-first@1.0.0 dp:lcl <path-to-your-workspace>/cncb-event-first
> sls deploy -v -r us-east-1 "-s" "john"

Serverless: Packaging service...
...
Serverless: Stack update finished...
...
functions:
submit: cncb-event-first-john-submit
...
  1. Review the stack and function in the AWS Console.
  2. Invoke the submit function with the following command:
$ sls invoke -f submit -r us-east-1 -s $MY_STAGE -d '{"id":"11111111-1111-1111-1111-111111111111","name":"thing one","kind":"other"}'

{
"type": "thing-submitted",
"id": "2a1f5290-42c0-11e8-a06b-33908b837f8c",
"partitionKey": "11111111-1111-1111-1111-111111111111",
"timestamp": 1524025374265,
"tags": {
"region": "us-east-1",
"kind": "other"
},
"thing": {
"id": "11111111-1111-1111-1111-111111111111",
"name": "thing one",
"kind": "other"
}
}
  1. Take a look at the logs:
$ sls logs -f submit -r us-east-1 -s $MY_STAGE

START ...
2018-04-18 00:22:54 ... params: {"StreamName":"john-cncb-event-stream-s1","PartitionKey":"11111111-1111-1111-1111-111111111111","Data":{"type":"Buffer","data":[...]}}
2018-04-18 00:22:54 ... response: {"ShardId":"shardId-000000000000","SequenceNumber":"4958...2466"}
END ...
REPORT ... Duration: 381.21 ms Billed Duration: 400 ms ... Max Memory Used: 34 MB
  1. Remove the stack once you have finished with npm run rm:lcl -- -s $MY_STAGE.
主站蜘蛛池模板: 商水县| 呼玛县| 乌鲁木齐市| 垣曲县| 阿尔山市| 西乌珠穆沁旗| 万山特区| 石阡县| 聂荣县| 筠连县| 连平县| 文登市| 阳原县| 榕江县| 凉城县| 比如县| 河津市| 米易县| 疏附县| 瓦房店市| 崇信县| 唐山市| 治县。| 大理市| 延安市| 曲沃县| 竹溪县| 南阳市| 江永县| 东兴市| 合川市| 石渠县| 子长县| 邯郸县| 伊吾县| 玛纳斯县| 安龙县| 铅山县| 合肥市| 海兴县| 翁源县|