Skip to content

Instantly share code, notes, and snippets.

@knowsuchagency
Created June 8, 2017 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knowsuchagency/63ecf6baa103e9ee87525a863d38271e to your computer and use it in GitHub Desktop.
Save knowsuchagency/63ecf6baa103e9ee87525a863d38271e to your computer and use it in GitHub Desktop.
Pyramid zappa recipe (Python 3.6)
from pyramid.config import Configurator
from configparser import ConfigParser # This will be different in Python 2.7
from functools import partial
import os
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
config = Configurator(settings=settings)
config.include('pyramid_jinja2')
config.include('.models')
config.include('.routes')
config.scan()
return config.make_wsgi_app()
def zappa(config_uri, event, context, **vars):
"""
Uses the settings in the configuration uri to bootstrap a wsgi application
through pyramid.
Zappa then uses that wsgi application
to create a handler function for use with aws lambda.
Event and context information are passed to the handler function which uses
our wsgi application to return a response.
:param config_uri: string pointing to paste deploy config file
:param event: aws event
:param context: aws context
:param vars: parameters that will be passed to the configuration file
:return: response
"""
config = ConfigParser()
config.read(config_uri)
settings = dict(config.items('app:main', vars=vars))
wsgi_app = main(None, **settings)
return wsgi_app(event, context)
zappa_dev = partial(zappa,
'development.ini',
dbusername=os.environ.get('dbusername'),
dbpassword=os.environ.get('dbpassword')
)
zappa_prod = partial(zappa,
'production.ini',
dbusername=os.environ.get('dbusername'),
dbpassword=os.environ.get('dbpassword')
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment