Skip to main content

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

Project description

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

Background

https://ben.fogbutter.com/2016/11/09/introducing-sippy-cup.html

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.

Project details


Download files

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

Source Distribution

sippycup-0.4.0.tar.gz (3.7 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page