From c9bc0b2e666953f46c500b63cbe71a562dc14151 Mon Sep 17 00:00:00 2001 From: Maciej Winnicki Date: Wed, 12 Oct 2016 11:13:56 +0200 Subject: [PATCH] add external library Node.js example --- .../aws/examples/hello-world/node/README.md | 2 +- .../aws/examples/hello-world/python/README.md | 10 ------- .../using-external-libraries/node/README.md | 27 ++++++++++--------- .../using-external-libraries/node/event.json | 5 ---- .../using-external-libraries/node/handler.js | 26 +++++++++--------- .../node/package.json | 7 ++--- .../node/serverless.yml | 4 +-- 7 files changed, 33 insertions(+), 48 deletions(-) delete mode 100644 docs/02-providers/aws/examples/hello-world/python/README.md delete mode 100644 docs/02-providers/aws/examples/using-external-libraries/node/event.json diff --git a/docs/02-providers/aws/examples/hello-world/node/README.md b/docs/02-providers/aws/examples/hello-world/node/README.md index 5c4c1896a..6d5420506 100644 --- a/docs/02-providers/aws/examples/hello-world/node/README.md +++ b/docs/02-providers/aws/examples/hello-world/node/README.md @@ -19,7 +19,7 @@ Make sure serverless is installed. [See installation guide](/docs/01-guide/01-in `-f` is shorthand for `--function` -In your terminal window you should be the response from AWS Lambda +In your terminal window you should see the response from AWS Lambda ```bash { diff --git a/docs/02-providers/aws/examples/hello-world/python/README.md b/docs/02-providers/aws/examples/hello-world/python/README.md deleted file mode 100644 index dbd58fd38..000000000 --- a/docs/02-providers/aws/examples/hello-world/python/README.md +++ /dev/null @@ -1,10 +0,0 @@ - - -# Hello World in Python - -[See installation guide](/docs/01-guide/01-installing-serverless.md) diff --git a/docs/02-providers/aws/examples/using-external-libraries/node/README.md b/docs/02-providers/aws/examples/using-external-libraries/node/README.md index 444011fac..fdb510168 100644 --- a/docs/02-providers/aws/examples/using-external-libraries/node/README.md +++ b/docs/02-providers/aws/examples/using-external-libraries/node/README.md @@ -1,13 +1,13 @@ -# Using External libraries in Node +# Using external libraries in Node.js service -Make sure serverless is installed. [See installation guide](/docs/01-guide/01-installing-serverless.md) +Make sure `serverless` is installed. [See installation guide](/docs/01-guide/01-installing-serverless.md) ## 1. Install dependencies @@ -15,22 +15,25 @@ For this example we are going to install the `faker` module from npm. `npm install faker --save` -## 2. Install the faker module in your `handler.js` file +## 2. Use the faker module in your `handler.js` file Inside of `handler.js` require your module. `const faker = require('faker');` -## 1. Deploy +## 3. Deploy -`serverless deploy` or `sls deploy`. +`serverless deploy` -`sls` is shorthand for the serverless CLI command +## 4. Invoke -Alternatively, you can run `npm run deploy` and deploy via NPM script defined in the `package.json` file +`serverless invoke -f helloRandomName` -## 2. Invoke +In your terminal window you should see the response from AWS Lambda -`serverless invoke --function helloRandomName` or `sls invoke -f helloRandomName` +```bash +{ + "message": "Hello Floyd" +} +``` -`-f` is shorthand for `--function` diff --git a/docs/02-providers/aws/examples/using-external-libraries/node/event.json b/docs/02-providers/aws/examples/using-external-libraries/node/event.json deleted file mode 100644 index 2ac50a459..000000000 --- a/docs/02-providers/aws/examples/using-external-libraries/node/event.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "key3": "value3", - "key2": "value2", - "key1": "value1" -} diff --git a/docs/02-providers/aws/examples/using-external-libraries/node/handler.js b/docs/02-providers/aws/examples/using-external-libraries/node/handler.js index 7d1309b25..bc9d601a3 100644 --- a/docs/02-providers/aws/examples/using-external-libraries/node/handler.js +++ b/docs/02-providers/aws/examples/using-external-libraries/node/handler.js @@ -1,13 +1,13 @@ -// 'use strict'; -// // Import faker module from node_modules -// const faker = require('faker'); -// -// // Your function handler -// module.exports.helloRandomNameHandler = function (event, context, callback) { -// const randomName = faker.name.firstName(); -// const message = { -// message: 'Hello ' + randomName, -// event: event -// }; -// callback(null, message); -// }; +'use strict'; + +// Import faker module from node_modules +const faker = require('faker'); + +module.exports.helloRandomName = function (event, context, callback) { + const name = faker.name.firstName(); + const message = { + message: `Hello ${name}`, + }; + + callback(null, message); +}; diff --git a/docs/02-providers/aws/examples/using-external-libraries/node/package.json b/docs/02-providers/aws/examples/using-external-libraries/node/package.json index 89667c7c2..a7cd0f3af 100644 --- a/docs/02-providers/aws/examples/using-external-libraries/node/package.json +++ b/docs/02-providers/aws/examples/using-external-libraries/node/package.json @@ -1,10 +1,7 @@ { - "name": "hello-world", - "description": "Serverless using external libraries example with node", + "name": "external-library", + "description": "Serverless using external libraries example with Node.js", "private": true, - "scripts": { - "deploy": "serverless deploy" - }, "dependencies": { "faker": "^3.1.0" } diff --git a/docs/02-providers/aws/examples/using-external-libraries/node/serverless.yml b/docs/02-providers/aws/examples/using-external-libraries/node/serverless.yml index ae8c48d3e..4bbb504b2 100644 --- a/docs/02-providers/aws/examples/using-external-libraries/node/serverless.yml +++ b/docs/02-providers/aws/examples/using-external-libraries/node/serverless.yml @@ -1,5 +1,5 @@ # Hello Random Name for AWS Lambda -service: hello-random-name # Service Name +service: external-lib # Service Name provider: name: aws @@ -7,4 +7,4 @@ provider: functions: helloRandomName: - handler: handler.helloRandomNameHandler + handler: handler.helloRandomName