add external library Node.js example

This commit is contained in:
Maciej Winnicki 2016-10-12 11:13:56 +02:00
parent fd561ed96a
commit c9bc0b2e66
No known key found for this signature in database
GPG Key ID: 950BD3CA7EDF6008
7 changed files with 33 additions and 48 deletions

View File

@ -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
{

View File

@ -1,10 +0,0 @@
<!--
title: Hello World AWS Lambda Python Example
menuText: Hello World Python Example
description: Create a simple Python powered Lambda function on amazon web services
layout: Doc
-->
# Hello World in Python
[See installation guide](/docs/01-guide/01-installing-serverless.md)

View File

@ -1,13 +1,13 @@
<!--
title: Hello World AWS Lambda Node Example
menuText: Hello World Node Example
title: Using external libraries in Node.js service
menuText: External libraries in Node.js service
description: Create a nodeJS Lambda function on amazon web services
layout: Doc
-->
# 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`

View File

@ -1,5 +0,0 @@
{
"key3": "value3",
"key2": "value2",
"key1": "value1"
}

View File

@ -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);
};

View File

@ -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"
}

View File

@ -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