Skip to content

Commit

Permalink
Expose AWS context object on request
Browse files Browse the repository at this point in the history
  • Loading branch information
jochenvdv committed Nov 9, 2018
1 parent 2ab4afb commit e79a577
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
5 changes: 3 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ Here is an example of what a Flask app using this library would look like:
if __name__ == '__main__':
app.run(debug=True)
You can access the original input event on the Flask request context:
You can access the original input event and context on the Flask request context:

.. code-block:: python
from flask import request
assert request.aws_event['input']['httpMethod'] == 'POST'
assert request.aws_event['input']['httpMethod'] == 'POST'
assert request.aws_context.get_remaining_time_in_millis() == 10_000
13 changes: 9 additions & 4 deletions flask_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
from flask import Request


__version__ = '0.1.1'
__version__ = '0.1.2'


def make_environ(event):
def make_environ(event, context):
environ = {}
# key might be there but set to None
headers = event.get('headers', {}) or {}
Expand Down Expand Up @@ -78,8 +78,9 @@ def make_environ(event):
environ['wsgi.run_once'] = True
environ['wsgi.multiprocess'] = False

# store AWS input event in WSGI environment
# store AWS input event and context in WSGI environment
environ['aws.event'] = event
environ['aws.context'] = context

return environ

Expand All @@ -89,6 +90,10 @@ class LambdaRequest(Request):
def aws_event(self):
return self.environ.get('aws.event')

@property
def aws_context(self):
return self.environ.get('aws.context')


class LambdaResponse:

Expand Down Expand Up @@ -119,7 +124,7 @@ def __call__(self, event, context):
response = LambdaResponse()

body = next(self.wsgi_app(
make_environ(event),
make_environ(event, context),
response.start_response
))

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setup(
name='flask-lambda-support',
version='0.1.1',
version='0.1.2',
description='Python 3.6+ module to make Flask compatible with AWS Lambda',
long_description=long_description,
keywords='flask aws amazon lambda',
Expand Down

0 comments on commit e79a577

Please sign in to comment.