Running locally

alt text

Create a Serverless function using slspress. In this tutorial, we are gonna use serverless-offline to create and run Serverless offline.

 

Sample

First, create a new project and generate a new package.json file for it, running the following commands:

mkdir hello-world-offline
cd hello-world-offline
npm init

 

Install the dependencies needed for the project.

npm install -S slspress
npm install -D serverless-offline

 

Let's create a handler.js file.

const { create, jsonMiddleware } = require('slspress');

const handler = create();

handler.on('handle')
    .middleware(jsonMiddleware)
    .get('/hello-world', (req, res) => {
        return res.ok('hello-world');
    });

module.exports = handler.export();

 

Let's create a serverless.yml file.

service: hello-world-offline

provider:
  name: aws
  runtime: nodejs8.10
  region: eu-west-1
  profile: default
  memorySize: 256 # optional, in MB, default is 1024
  stage: dev

plugins:
  - serverless-offline

functions:
  hello-world:
    handler: handler.handle # required, handler set in AWS Lambda
    name: "${self:service}-${self:provider.stage}" # optional, Deployed Lambda name
    description: "slspress offline example"
    events:
      - http:
          path: hello-world
          method: get
          cors: true

 

Run a local version of the 'hello-world' Serverless function.

sls offline start

To stop the local debugging, just hit (ctrl+c).

 

Once you are happy with the local debugging, you can deploy your code to AWS.

sls deploy -v

 

Debugging

Serverless offline plugin will respond to the overall framework settings and output additional information to the console in debug mode. In order to do this you will have to set the SLS_DEBUG environmental variable.

SLS_DEBUG=* sls offline start