mirror of
https://github.com/serverless/serverless.git
synced 2026-01-25 15:07:39 +00:00
add external library Node.js example
This commit is contained in:
parent
fd561ed96a
commit
c9bc0b2e66
@ -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
|
||||
{
|
||||
|
||||
@ -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)
|
||||
@ -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`
|
||||
|
||||
@ -1,5 +0,0 @@
|
||||
{
|
||||
"key3": "value3",
|
||||
"key2": "value2",
|
||||
"key1": "value1"
|
||||
}
|
||||
@ -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);
|
||||
};
|
||||
|
||||
@ -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"
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user