Skip to content
Configure serverless functions from swagger definition
JavaScript TypeScript
Branch: master
Clone or download
Latest commit 02d2ff5 Jan 20, 2017
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
test Mapper handles replacing http event Jan 11, 2017
.gitignore Initial Import Jan 9, 2017
.nvmrc Initial Import Jan 9, 2017
LICENSE.MD Add license Jan 9, 2017
README.md Update README.md Jan 19, 2017
generator.d.ts Rework in more functional approach, handle namespace Jan 11, 2017
generator.js Rework in more functional approach, handle namespace Jan 11, 2017
generator.ts Rework in more functional approach, handle namespace Jan 11, 2017
index.d.ts Release 1.0.3 Jan 11, 2017
index.js Release 1.0.3 Jan 11, 2017
index.ts Release 1.0.3 Jan 11, 2017
package.json Release 1.0.3 Jan 11, 2017
serverless.d.ts
serverless.js Initial Import Jan 9, 2017
serverless.ts Initial Import Jan 9, 2017
tsconfig.json Initial Import Jan 9, 2017
yarn.lock Initial Import Jan 9, 2017

README.md

Serverless Swagger

Maps swagger configuration to serverless handlers.

Installation

If you don't have a serverless project, create one now:

sls create -t aws-nodejs --path MyService
cd MyService

then

yarn init 

or

npm init

Inside your serverless project directory:

yarn add serverless_swagger

or

npm install serverless_swagger

Add the following to serverless.yml:

plugins:
  - serverless_swagger

Configuration

The plugin looks for a swagger.yml in your serverless project. You can provide a different name by specifying a custom variable:

custom:
  swagger_file: swagger.yml

Generation

A complete set of JS handlers and the appropriate serverless.yml configuration can be generated

sls swagger

Existing files should not be overwritten, meaning that the swagger can be updated and new functions generated without losing customisations.

Generated files can be output to a specified directory using the --output flag and a destination directory (which must exist).

sls swagger --output output

Mapping

At deploy, each function will be mapped to API Gateway events based on the swagger specification.

Functions are mapped by parsing the path and method in the swagger spec to a function name in serverless.yml.

For example, given the following serverless.yml and swagger.yml definitions

functions:
  getClientById:
    handler: getClientById/handler.main
  getClientByName:
    handler: getClientByName/handler.main
  paths:
    /client/{id}:
      get:
        summary: "Get client  id"      
    /blah
      get:
        summary: "Get blah"      

The getClientById function will be mapped to an API gateway event in the form:

events:
  - http:
      method: get
      path: /client/{id}

The getClientByName does not match any summary information and will not have an event

You can’t perform that action at this time.