/starter-serverless-nodejs

A Starter project for Serverless services with NodeJS 6.10 and Serverless Framework

Primary LanguageJavaScript

Serverless NodeJS Starter project

This project is intended to make you as fast as possible starting a new service project using the following technologies

  • Lodash 4.17.5
  • ESLint 4.18.2
  • Ajv 6.2.1 (with schema draft 0.7, previous not supported anymore)
    • Ajv Keywords 3.1.0
  • Bluebird 3.5.1
  • Intravenous 0.1.4-beta
  • Locale 0.1.0
  • NConf 0.10.0
  • Node UUID 1.4.8
  • Winston 2.4.0
  • Istanbul 0.4.5
  • Jasmine 3.1.0
    • Jasmine Core 3.1.0
    • Jasmine Expect 3.8.3
    • Jasmine Promises 0.4.1
    • Jasmine Spec Reporter 4.2.1
  • Serverless 1.26.1

Getting started

To start a new service project, you don't need the full repository history, just make an archive of this repo

git archive -o my-starter-project.zip --remote https://path/to/this/repo

Now you must cd into the folder and run

git init

Then edit package.json setting appropriate values for

  • name
  • displayName
  • contributors
  • repository
  • bugs
  • homepage

Once you've finished wit package.json, edit also this README.md file

Finally run

npm install

NOTE: you could experience some NPM error when running npm install. This is probably related to a mismatch between package-lock.json file and your npm version. If this occur, just delete package-lock.json and run npm install again.

Now everything is set up, you can create your repo first commit:

git add .
git commit -m "Initial project setup"

If you wish, you could also set a remote destination with

git remote add origin https://path/to/remote/repo.git
git push --all

Now you're done and can start adding your project' specific code.

And, of course, you can deploy this service with

serverless deploy --region <YOUR_REGION> --stage <YOUR_STAGE>

About Lambda Event mappings

This starter project comes with the assumption every service will be handled through Serverless and LambdaProxy is the preferred way of mapping services. To this reason, we provided a set of classes:

  • lambda-event.js
  • HTTPError
  • lambda-mapper.js
  • resources/json-schemas/principal.schema.json

lambda-event.js

LambdaEvent is an utility class that provides a set of functions to extract and validate data coming from a LambdaProxy Input integration. it allow to abstract away all the structure of the event and request only semantic objects for resources, uuids, principals and so on. LambdaEvent supports also response packaging into a LambdaProxy Output Event either as error or as a result.

Principals

This is an important note about Neosperience usage of principals. Read this before using LamdbaEvent functions, to fully understand their meaning. For your convenience, check on principal has been disabled in lamdba-mapper.js

AWS does not imply any particular structure for event principals. It just defines them as provided back from a lambda authorizer Regarding principals, at Neosperience, we make the assumption it is always a JSON. Using a JSON instead of a play string has many advantages, such as out-of-the-box format validation and semantic validation through JSON-Schema.

Whenever a principal is extracted from event using LambdaEvent methods, it is required to pass a JSON containing JSONSchema for this principal and validation is performad before extraction. LamdbaEvent utility class does not require any particular schema, but lambda-mapper.js uses the standard Neosperience Principal schema to validate request.

Entities

LamdbaEvent provides support for entity extraction from Lambda event and their validation using a custom schema