- JavaScript Cloud Native Development Cookbook
- John Gilbert
- 309字
- 2021-07-16 18:03:25
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/ch1/create-function --path cncb-create-function
- Navigate to the cncb-create-function directory with cd cncb-create-function.
- Review the file named serverless.yml with the following content:
service: cncb-create-function
provider:
name: aws
runtime: nodejs8.10
environment:
V1: value1
functions:
hello:
handler: handler.hello
- Review the file named handler.js with the following content:
module.exports.hello = (event, context, callback) => {
console.log('event: %j', event);
console.log('context: %j', context);
console.log('env: %j', process.env);
callback(null, 'success');
};
- Install the dependencies with npm install.
- Run the tests with npm test.
- Review the contents generated in the .serverless directory.
- Deploy the stack:
$ npm run dp:lcl -- -s $MY_STAGE
> cncb-create-function@1.0.0 dp:lcl <path-to-your-workspace>/cncb-create-function
> sls deploy -r us-east-1 "-s" "john"
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
.....
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (881 B)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
.................
Serverless: Stack update finished...
Service Information
service: cncb-create-function
stage: john
region: us-east-1
stack: cncb-create-function-john
api keys:
None
endpoints:
None
functions:
hello: cncb-create-function-john-hello
- Review the stack and function in the AWS Console.
- Invoke the function with the following command:
$ sls invoke -r us-east-1 -f hello -s $MY_STAGE -d '{"hello":"world"}'
"success"
- Review the function metrics in the AWS Console:

- Review the function logs in the AWS Console:

- Take a look at the logs locally:
$ sls logs -f hello -r us-east-1 -s $MY_STAGE
START RequestId: ... Version: $LATEST
2018-03-24 15:48:45 ... event: {"hello":"world"}
2018-03-24 15:48:45 ... context: {"functionName":"cncb-create-function-john-hello","memoryLimitInMB":"1024", ...}
2018-03-24 15:48:45 ... env: {"V1":"value1","TZ":":UTC","AWS_REGION":"us-east-1", "AWS_ACCESS_KEY_ID":"...", ...}
END RequestId: ...
REPORT ... Duration: 3.64 ms Billed Duration: 100 ms ... Max Memory Used: 20 MB
- Remove the stack once you have finished with npm run rm:lcl -- -s $MY_STAGE.
推薦閱讀
- 真空鍍膜
- 一本書(shū)讀懂5G技術(shù)
- 雙/多基地雷達(dá)目標(biāo)探測(cè)與識(shí)別
- 教你檢修液晶顯示器
- 通信系統(tǒng)仿真設(shè)計(jì)與應(yīng)用
- PLC控制技術(shù)(西門(mén)子S7-200)
- 上門(mén)速查快修國(guó)產(chǎn)新型彩色電視機(jī)500例
- 交換機(jī)/路由器及其配置
- 太陽(yáng)能光伏組件生產(chǎn)制造工程技術(shù)
- 如影隨形:無(wú)處不在的無(wú)線電波
- 光接入網(wǎng)實(shí)用技術(shù)
- 電子工程師必備——元器件應(yīng)用寶典
- 物聯(lián)網(wǎng)-射頻識(shí)別(RFID)核心技術(shù)教程
- 小基站(Small Cell)無(wú)線網(wǎng)絡(luò)規(guī)劃與設(shè)計(jì)
- cdma2000 1x EV-DO系統(tǒng)、接口與無(wú)線網(wǎng)絡(luò)優(yōu)化