README
Serverless Offline Plugin
:sparkles: Announcement :sparkles:
Dear users and contributors,
Thank you for all your support. In the upcomming days my availability on this project should decline. This is why I'm looking for maintainers: anyone that pushed a successful PR and is willing to manage the upcomming ones is welcome to apply. To do so just comment on this issue.
Thanks you for your attention. :)
:wavy_dash::wavy_dash::wavy_dash:
This Serverless plugin emulates AWS λ and API Gateway on your local machine to speed up your development cycles. To do so, it starts an HTTP server that handles the request's lifecycle like APIG does and invokes your handlers.
Features:
- Nodejs λ only.
- Velocity templates support.
- Lazy loading of your files with require cache invalidation: no need for a reloading tool like Nodemon.
- And more: integrations, authorizers, proxies, timeouts, responseParameters, HTTPS, Babel runtime, CORS, etc...
This plugin is updated by its users, I just do maintenance and ensure that PRs are relevant to the community. In other words, if you find a bug or want a new feature, please help us by becoming one of the contributors :v: ! See the contributing section.
Documentation
- Installation
- Usage and command line options
- Usage with Babel
- Token authorizers
- Custom authorizers
- AWS API Gateway features
- Velocity nuances
- Debug process
- Scoped execution
- Simulation quality
- Credits and inspiration
- Contributing
- License
Installation
For Serverless v1 only. See this branch for 0.5.x versions.
First, add Serverless Offline to your project:
npm install serverless-offline --save-dev
Then inside your project's serverless.yml file add following entry to the plugins section: serverless-offline. If there is no plugin section you will need to add it to the file.
It should look something like this:
plugins:
- serverless-offline
You can check wether you have successfully installed the plugin by running the serverless command line:
serverless
the console should display Offline as one of the plugins now available in your Serverless project.
Usage and command line options
In your project root run:
serverless offline start or sls offline start.
to list all the options for the plugin run:
sls offline --help
All CLI options are optional:
--prefix -p Adds a prefix to every path, to send your requests to http://localhost:3000/[prefix]/[your_path] instead. E.g. -p dev
--location -l The root location of the handlers' files. Defaults to the current directory
--host -o Host name to listen on. Default: localhost
--port -P Port to listen on. Default: 3000
--stage -s The stage used to populate your templates. Default: the first stage found in your project.
--region -r The region used to populate your templates. Default: the first region for the first stage found.
--noTimeout -t Disables the timeout feature.
--noEnvironment Turns off loading of your environment variables from serverless.yml. Allows the usage of tools such as PM2 or docker-compose.
--resourceRoutes Turns on loading of your HTTP proxy settings from serverless.yml.
--dontPrintOutput Turns off logging of your lambda outputs in the terminal.
--httpsProtocol -H To enable HTTPS, specify directory (relative to your cwd, typically your project dir) for both cert.pem and key.pem files.
--skipCacheInvalidation -c Tells the plugin to skip require cache invalidation. A script reloading tool like Nodemon might then be needed.
--corsAllowOrigin Used as default Access-Control-Allow-Origin header value for responses. Delimit multiple values with commas. Default: '*'
--corsAllowHeaders Used as default Access-Control-Allow-Headers header value for responses. Delimit multiple values with commas. Default: 'accept,content-type,x-api-key'
--corsDisallowCredentials When provided, the default Access-Control-Allow-Credentials header value will be passed as 'false'. Default: true
--exec "<script>" When provided, a shell script is executed when the server starts up, and the server will shut domn after handling this command.
--noAuth Turns off all authorizers
Any of the CLI options can be added to your serverless.yml. For example:
custom:
serverless-offline:
httpsProtocol: "dev-certs"
port: 4000
Options passed on the command line override YAML options.
By default you can send your requests to http://localhost:3000/. Please note that:
- You'll need to restart the plugin if you modify your
serverless.ymlor any of the default velocity template files. - The event object passed to your λs has one extra key:
{ isOffline: true }. Also,process.env.IS_OFFLINEistrue. - When no Content-Type header is set on a request, API Gateway defaults to
application/json, and so does the plugin. But if you send anapplication/x-www-form-urlencodedor amultipart/form-databody with anapplication/json(or no) Content-Type, API Gateway won't parse your data (you'll get the ugly raw as input), whereas the plugin will answer 400 (malformed JSON). Please consider explicitly setting your requests' Content-Type and using separate templates.
Usage with Babel
You can use Offline with Serverless-runtime-babel.
To do so you need to install (at least) the es2015 preset in your project folder (npm i babel-preset-es2015 --save-dev).
~ Or ~
Your λ handlers can be required with babel-register.
To do so, in your serverless.yml file, set options to be passed to babel-register like this:
custom:
serverless-offline:
babelOptions:
presets: ["es2015", "stage-2"]
Here is the full list of babel-register options
Token Authorizers
As defined in the Serverless Documentation you can use API Keys as a simple authentication method.
Serverless-offline will emulate the behaviour of APIG and create a random token that's printed on the screen. With this token you can access your private methods adding x-api-key: generatedToken to your request header. All api keys will share the same token. To specify a custom token use the --apiKey cli option.
Custom authorizers
Only custom authorizers are supported. Custom authorizers are executed before a Lambda function is executed and return an Error or a Policy document.
The Custom authorizer is passed an event object as below:
{
"type": "TOKEN",
"authorizationToken": "<Incoming bearer token>",
"methodArn": "arn:aws:execute-api:<Region id>:<Account id>:<API id>/<Stage>/<Method>/<Resource path>"
}
The methodArn does not include the Account id or API id.
The plugin only supports retrieving Tokens from headers. You can configure the header as below:
"authorizer": {
"type": "TOKEN",
"identitySource": "method.request.header.Authorization", // or method.request.header.SomeOtherHeader
"authorizerResultTtlInSeconds": "0"
}
AWS API Gateway Features
Velocity Templates
You can supply response and request templates for each function. This is optional. To do so you will have to place function specific template files in the same directory as your function file and add the .req.vm extension to the template filename.
For example,
if your function is in code-file: helloworld.js,
your response template should be in file: helloworld.res.vm and your request template in file helloworld.req.vm.
CORS
If the endpoint config has CORS set to true, the plugin will use the CLI CORS options for the associated route. Otherwise, no CORS headers will be added.
Catch-all Path Variables
Set greedy paths like /store/{proxy+} that will intercept requests made to /store/list-products, /store/add-product, etc...
ANY method
Works out of the box.
Lambda and Lambda Proxy Integrations
Works out of the box. See examples in the manual_test directory.
HTTP Proxy
Serverless doc ~ AWS doc - AWS::ApiGateway::Method ~ AWS doc - AWS::ApiGateway::Resource
Example of enabling proxy:
custom:
serverless-offline:
resourceRoutes: true
or
YourCloudFormationMethodId:
Type: AWS::ApiGateway::Method
Properties:
......
Integration:
Type: HTTP_PROXY
Uri: 'https://s3-${self:custom.region}.amazonaws.com/${self:custom.yourBucketName}/{proxy}'
......
custom:
serverless-offline:
resourceRoutes:
YourCloudFormationMethodId:
Uri: 'http://localhost:3001/assets/{proxy}'
Response parameters
You can set your response's headers using ResponseParameters.
May not work properly. Please PR. (Difficulty: hard?)
Example response velocity template:
"responseParameters": {
"method.response.header.X-Powered-By": "Serverless", // a string
"method.response.header.Warning": "integration.response.body", // the whole response
"method.response.header.Location": "integration.response.body.some.key" // a pseudo JSON-path
},
Velocity nuances
Consider this requestTemplate for a POST endpoint:
"application/json": {
"payload": "$input.json('