GithubHelp home page GithubHelp logo

brettdh / serverless-wsgi Goto Github PK

View Code? Open in Web Editor NEW

This project forked from logandk/serverless-wsgi

0.0 2.0 0.0 78 KB

Serverless plugin to deploy WSGI applications (Flask/Django/Pyramid etc.) and bundle Python packages

License: MIT License

JavaScript 43.34% Python 56.66%

serverless-wsgi's Introduction

Serverless WSGI

npm package

serverless Build Status Coverage Status Dependency Status Dev Dependency Status

A Serverless v1.x plugin to build your deploy Python WSGI applications using Serverless. Compatible WSGI application frameworks include Flask, Django and Pyramid - for a complete list, see: 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 --save serverless-wsgi

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

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.

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 default lambda-proxy integration for API Gateway. This configuration example treats API Gateway as a transparent proxy, passing all requests directly to your Flask application, and letting the application handle errors, 404s etc.

service: example

provider:
  name: aws
  runtime: python2.7

plugins:
  - serverless-wsgi

functions:
  api:
    handler: wsgi.handler
    events:
      - http: ANY /
      - http: ANY {proxy+}

custom:
  wsgi:
    app: api.app

requirements.txt

Add Flask to the application bundle.

Flask==0.12.2

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:

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.12.2
requests==2.18.3

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

You can use the requirement packaging functionality of serverless-wsgi without the WSGI handler itself by including the plugin in your serverless.yml configuration, without specifying the custom.wsgi.app setting. This will omit the WSGI handler from the package, but include any requirements specified in requirements.txt.

If you do not include the WSGI handler, you'll need to add .requirements to the Python search path manually in your handler, before importing any packages:

import os
import sys
root = os.path.abspath(os.path.join(os.path.dirname(__file__)))
sys.path.insert(0, os.path.join(root, '.requirements'))

If you don't want to use automatic requirement packaging you can set custom.wsgi.packRequirements to false:

custom:
  wsgi:
    app: api.app
    packRequirements: false

Local server

For convenience, a sls wsgi serve command 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!

Explicit routes

If you'd like to be explicit about which routes and HTTP methods should pass through to your application, see the following example:

service: example

provider:
  name: aws
  runtime: python2.7

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

Thanks

Thanks to Zappa, which has been both the inspiration and source of several implementations that went into this project.

Thanks to chalice for the requirement packaging implementation.

serverless-wsgi's People

Contributors

logandk avatar lucasrcosta avatar iwitaly avatar brettdh avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.