Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

An adaptor for serving WSGI applications using AWS Lambda and API Gateway

Project Description

Python Adaptor for Serving WSGI Applications with AWS Lambda and API Gateway

Sippy Cup is an extremely minimalistic Python adaptor that allows WSGI applications to be served using using AWS API Gateway and AWS Lambda proxy integration.

Sippy Cup converts the input format sent to an AWS Lambda function by API Gateway into a WSGI environment that is used to run a the application. The application’s response is then converted to a format that can be understood by API Gateway.

When the WSGI environment is created, some additional values from the event sent to AWS Lambda from API Gateway are also added. See the demo app below for how to access these.

  • apigateway: True
  • apigateway.stageVariables: API Gateway stage variables
  • apigateway.requestContext: The event request context

Getting started

Installation

Because you will eventually need to create a deployment package, it is highly recommended that you use a virtualenv when using Sippy Cup.

pip install sippycup

Create a simple application

lambda_function.py provides a demo application

# lambda_function.py

from flask import Flask, Response, request, jsonify
from sippycup import sippycup

app = Flask(__name__)

@app.route('/hello/', methods=['GET', 'POST'])
@app.route('/hello/<string:name>', methods=['GET', 'POST'])
def hello_world(name='World'):
    return Response('Hello, {0}!'.format(name), mimetype='text/plain')


@app.route('/')
def index():
    # return the additional WSGI environment variables that SippyCup
    # provides
    return jsonify({
        'requestContext': request.environ['apigateway.requestContext'],
        'stageVariables': request.environ['apigateway.stageVariables']
    })


def lambda_handler(event, context):
    return sippycup(app, event, context)


if __name__ == '__main__':
    app.run()

You will need to create a deployment package and use that to create a new AWS Lambda function.

Finally, set up an API Gateway proxy resource with the lambda proxy integration. It is recommended to create resources on both ‘/’ and ‘/SOMEPREFIX’ unless you don’t need the ‘/’ route.

A note about URL generation and redirection

In order to properly execute things like URL generation and redirection, WSGI applications use an environment variable called SCRIPT_NAME. By default, this works as one might expect. However, if your API is mapped to a custom domain using base path mapping, you need to tell the application about this. This is a common problem with more traditional setups as well (e.g. applications behind a reverse proxy).

The solution in Sippy Cup is to set a stage variable called SIPPYCUP_SCRIPT_NAME_BASE, which will be used by Sippy Cup to properly construct SCRIPT_NAME so that things work as expected.

e.g. If you are mapping your ‘production’ stage to api.mydomain.com using the default configuration in ApiGateway, you should set the ‘SIPPYCUP_SCRIPT_NAME_BASE’ stage variable for that stage to ‘/’.

Release History

Release History

This version
History Node

0.5.3

History Node

0.5.2

History Node

0.5.1

History Node

0.5.0

History Node

0.4.0

History Node

0.3.0

History Node

0.2.0

History Node

0.1.1

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
sippycup-0.5.3.tar.gz (4.9 kB) Copy SHA256 Checksum SHA256 Source Aug 27, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting