Skip to content

Commit

Permalink
Initial working plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
logandk committed Oct 13, 2016
1 parent 6e283f5 commit fa083cb
Show file tree
Hide file tree
Showing 8 changed files with 602 additions and 0 deletions.
40 changes: 40 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Logs
logs
*.log
npm-debug.log

# Runtime data
pids
*.pid
*.seed
dist

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

#IDE Stuff
**/.idea

#OS STUFF
.DS_Store
.tmp

#SERVERLESS STUFF
admin.env
.env
166 changes: 166 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
# Serverless WSGI

[![serverless](http://public.serverless.com/badges/v3.svg)](http://www.serverless.com)

A Serverless v1.0 plugin to build your deploy Python WSGI applications using Serverless. Compatible
WSGI application frameworks include Flask, Django and Pyramids - for a complete list, see:
[http://wsgi.readthedocs.io/en/latest/frameworks.html](http://wsgi.readthedocs.io/en/latest/frameworks.html).

### Features

* Transparently converts API Gateway requests to and from standard WSGI requests
* Supports anything you'd expect from WSGI such as redirects, cookies, file uploads etc.
* Automatically downloads Python packages that you specify in `requirements.txt` and deploys them along with your application
* Convenient `wsgi serve` command for serving your application locally during development


## Install

```
npm install serverless-wsgi
```

Add the plugin to your `serverless.yml` file and set the WSGI application:

```yaml
plugins:
- serverless-wsgi
```
## Flask configuration example
This example assumes that you have intialized your application as `app` inside `api.py`.

```
project
├── api.py
├── requirements.txt
└── serverless.yml
```
### api.py
A regular Flask application.
```python
from flask import Flask
app = Flask(__name__)
@app.route("/cats")
def cats():
return "Cats"
@app.route("/dogs/<id>")
def dog(id):
return "Dog"
```

### serverless.yml

Load the plugin and set the `custom.wsgi.app` configuration in `serverless.yml` to the
module path of your Flask application.

All functions that will use WSGI need to have `wsgi.handler` set as the Lambda handler and
use the `lambda-proxy` integration for API Gateway.

```yaml
plugins:
- serverless-wsgi

functions:
api:
handler: wsgi.handler
events:
- http:
path: cats
method: get
integration: lambda-proxy
- http:
path: dogs/{id}
method: get
integration: lambda-proxy

custom:
wsgi:
app: api.app
```
### requirements.txt
Add Flask to the application bundle.
```
Flask==0.11.1
requests==2.11.1

```


## Deployment

Simply run the serverless deploy command as usual:

```
$ sls deploy
Serverless: Packaging Python WSGI handler...
Serverless: Packaging required Python packages...
Serverless: Packaging service...
Serverless: Removing old service versions...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading service .zip file to S3...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
..........
Serverless: Stack update finished...
```


## Other frameworks

Set `custom.wsgi.app` in `serverless.yml` according to your WSGI callable:

* For Pyramid, use [make_wsgi_app](http://docs.pylonsproject.org/projects/pyramid/en/latest/api/config.html#pyramid.config.Configurator.make_wsgi_app) to intialize the callable
* Django is configured for WSGI by default, set the callable to `<project_name>.wsgi.application`. See [https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/](https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/) for more information.


## Usage

### Automatic requirement packaging

You'll need to include any packages that your application uses in the bundle
that's deployed to AWS Lambda. This plugin helps you out by doing this automatically,
as long as you specify your required packages in a `requirements.txt` file in the root
of your Serverless service path:

```
Flask==0.11.1
```

For more information, see [https://pip.readthedocs.io/en/1.1/requirements.html](https://pip.readthedocs.io/en/1.1/requirements.html).

### Local server

For convenience, a `sls wsgi serve` is provided to run your WSGI application
locally. This command requires the `werkzeug` Python package to be installed,
and acts as a simple wrapper for starting werkzeug's built-in HTTP server.

By default, the server will start on port 5000.

```
$ sls wsgi serve
* Running on http://localhost:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
```

Configure the port using the `-p` parameter:

```
$ sls wsgi serve -p 8000
* Running on http://localhost:8000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
```
121 changes: 121 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/* jshint ignore:start */
'use strict';

const BbPromise = require('bluebird');
const _ = require('lodash');
const path = require('path');
const fse = require('fs-extra');
const child_process = require('child_process');

BbPromise.promisifyAll(fse);

class ServerlessWSGI {
validate() {
if (this.serverless.service.custom && this.serverless.service.custom.wsgi && this.serverless.service.custom.wsgi.app) {
this.wsgiApp = this.serverless.service.custom.wsgi.app;
} else {
throw new this.serverless.classes.Error(
'Missing WSGI app, please specify custom.wsgi.app. For instance, if you have a Flask application "app" in "api.py", set the Serverless custom.wsgi.app configuration option to: api.app');
}
};

packWsgiHandler() {
this.serverless.cli.log('Packaging Python WSGI handler...');

return BbPromise.all([
fse.copyAsync(
path.resolve(__dirname, 'wsgi.py'),
path.join(this.serverless.config.servicePath, 'wsgi.py')),
fse.writeFileAsync(
path.join(this.serverless.config.servicePath, '.wsgi_app'),
this.wsgiApp)
]);
};

packRequirements() {
const requirementsFile = path.join(this.serverless.config.servicePath, 'requirements.txt');

if (!fse.existsSync(requirementsFile)) {
return BbPromise.resolve();
}

this.serverless.cli.log('Packaging required Python packages...');

return new BbPromise((resolve, reject) => {
const res = child_process.spawnSync('python', [
path.resolve(__dirname, 'requirements.py'),
path.resolve(__dirname, 'requirements.txt'),
requirementsFile,
path.join(this.serverless.config.servicePath, '.requirements')
]);
if (res.error) {
return reject(res.error);
}
if (res.status != 0) {
return reject(res.stderr);
}
resolve();
});
};

cleanup() {
const artifacts = ['wsgi.py', '.wsgi_app', '.requirements'];

return BbPromise.all(_.map(artifacts, (artifact) =>
fse.removeAsync(path.join(this.serverless.config.servicePath, artifact))));;
};

serve() {
const port = this.options.port || 5000;

return new BbPromise((resolve, reject) => {
child_process.spawnSync('python', [
path.resolve(__dirname, 'serve.py'),
this.serverless.config.servicePath,
this.wsgiApp,
port
], { stdio: 'inherit' });
resolve();
});
};

constructor(serverless, options) {
this.serverless = serverless;
this.options = options;

this.commands = {
wsgi: {
commands: {
serve: {
usage: 'Serve the WSGI application locally.',
lifecycleEvents: [
'serve',
],
options: {
port: {
usage: 'The local server port, defaults to 5000.',
shortcut: 'p',
},
},
},
},
},
};

this.hooks = {
'before:deploy:createDeploymentArtifacts': () => BbPromise.bind(this)
.then(this.validate)
.then(this.packWsgiHandler)
.then(this.packRequirements),

'after:deploy:createDeploymentArtifacts': () => BbPromise.bind(this)
.then(this.cleanup),

'wsgi:serve:serve': () => BbPromise.bind(this)
.then(this.validate)
.then(this.serve)
};
}
}

module.exports = ServerlessWSGI;
48 changes: 48 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "serverless-wsgi",
"version": "1.0.0",
"engines": {
"node": ">=4.0"
},
"description": "Serverless WSGI Plugin",
"author": "logan.dk",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/logandk/serverless-wsgi"
},
"keywords": [
"serverless",
"wsgi",
"flask",
"serverless framework plugin",
"serverless applications",
"serverless plugins",
"api gateway",
"lambda",
"aws",
"aws lambda",
"amazon",
"amazon web services",
"serverless.com"
],
"files": [
"index.js",
"LICENSE",
"package.json",
"README.md",
"requirements.py",
"requirements.txt",
"serve.py",
"wsgi.py"
],
"main": "index.js",
"bin": {},
"scripts": {},
"devDependencies": {},
"dependencies": {
"bluebird": "^3.0.6",
"fs-extra": "^0.26.7",
"lodash": "^4.13.1"
}
}
Loading

0 comments on commit fa083cb

Please sign in to comment.