Skip to content

Make library compatible with both AWS REST and HTTP API gateways #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,4 @@ ENV/
.ropeproject

.vscode/
http_api_vs_rest_api/
12 changes: 12 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
flask = "*"

[dev-packages]

[requires]
python_version = "3.9"
136 changes: 136 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 11 additions & 67 deletions README.rst
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
flask-lambda
flask-aws-lambda
============

Python module to make Flask compatible with AWS Lambda for creating RESTful applications.
Python module to make Flask compatible with AWS Lambda for creating RESTful applications. Compatible with both REST and HTTP API gateways.

Installation
------------

::

pip install flask-lambda
pip install flask-aws-lambda

Usage
-----
Expand All @@ -19,9 +19,12 @@ This module works pretty much just like Flask. This allows you to run and develo

Here is an example of what ``my_python_file.py`` would look like::

from flask_lambda import FlaskLambda
import json
from flask import request
from flask_aws_lambda import FlaskAwsLambda

app = FlaskLambda(__name__)

app = FlaskAwsLambda(__name__)


@app.route('/foo', methods=['GET', 'POST'])
Expand All @@ -37,10 +40,10 @@ Here is an example of what ``my_python_file.py`` would look like::
{'Content-Type': 'application/json'}
)


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


Flask-RESTful
-------------

Expand All @@ -54,70 +57,11 @@ Configure your API Gateway with a ``{proxy+}`` resource with an ``ANY`` method.
Deploying
---------

Consider using `python-mu <https://github.com/sivel/mu>`_.
Consider using `AWS Serverless Application Model (AWS SAM) <https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html>`_.

Lambda Test Event
-----------------

If you wish to use the "Test" functionality in Lambda for your function, you will need a "API Gateway AWS Proxy" event. Below is an event to test the above sample application::

{
"body": "{\"test\":\"body\"}",
"resource": "/{proxy+}",
"requestContext": {
"resourceId": "123456",
"apiId": "1234567890",
"resourcePath": "/{proxy+}",
"httpMethod": "POST",
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
"accountId": "123456789012",
"identity": {
"apiKey": null,
"userArn": null,
"cognitoAuthenticationType": null,
"caller": null,
"userAgent": "Custom User Agent String",
"user": null,
"cognitoIdentityPoolId": null,
"cognitoIdentityId": null,
"cognitoAuthenticationProvider": null,
"sourceIp": "127.0.0.1",
"accountId": null
},
"stage": "prod"
},
"queryStringParameters": {
"foo": "bar"
},
"headers": {
"Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)",
"Accept-Language": "en-US,en;q=0.8",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Mobile-Viewer": "false",
"X-Forwarded-For": "127.0.0.1, 127.0.0.2",
"CloudFront-Viewer-Country": "US",
"Accept": "application/json",
"Upgrade-Insecure-Requests": "1",
"X-Forwarded-Port": "443",
"Host": "1234567890.execute-api.us-east-1.amazonaws.com",
"X-Forwarded-Proto": "https",
"X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==",
"CloudFront-Is-Tablet-Viewer": "false",
"Cache-Control": "max-age=0",
"User-Agent": "Custom User Agent String",
"CloudFront-Forwarded-Proto": "https",
"Accept-Encoding": "gzip, deflate, sdch",
"Content-Type": "application/json"
},
"pathParameters": {
"proxy": "foo"
},
"httpMethod": "POST",
"stageVariables": {
"baz": "qux"
},
"path": "/foo"
}
If you wish to use the "Test" functionality in Lambda for your function, you will need a "API Gateway AWS Proxy" event. Check the event JSON objects in the events folder.

To update your test event, click "Actions" -> "Configure test event".
38 changes: 38 additions & 0 deletions events/http_api_event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"version": "2.0",
"routeKey": "GET /<path:proxy>",
"rawPath": "/templates/contracts",
"rawQueryString": "",
"cookies": [],
"headers": {
"Host": "localhost:5000",
"User-Agent": "insomnia/2021.6.0",
"Content-Type": "application/json",
"Authorization": "",
"Accept": "*/*",
"Content-Length": "16",
"X-Forwarded-Proto": "http",
"X-Forwarded-Port": "5000"
},
"queryStringParameters": {},
"requestContext": {
"accountId": "123456789012",
"apiId": "1234567890",
"http": {
"method": "GET",
"path": "/templates/contracts",
"protocol": "HTTP/1.1",
"sourceIp": "127.0.0.1",
"userAgent": "Custom User Agent String"
},
"requestId": "4d160553-b400-4d10-8f14-5338b8936458",
"routeKey": "GET /<path:proxy>",
"stage": null
},
"body": "{\"foo\":\"bar\"}",
"pathParameters": {
"proxy": "templates/contracts"
},
"stageVariables": null,
"isBase64Encoded": false
}
63 changes: 63 additions & 0 deletions events/rest_api_event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"body": "{\"message\": \"hello world\"}",
"resource": "/hello",
"path": "/hello",
"httpMethod": "GET",
"isBase64Encoded": false,
"queryStringParameters": {
"foo": "bar"
},
"pathParameters": {
"proxy": "/path/to/resource"
},
"stageVariables": {
"baz": "qux"
},
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, sdch",
"Accept-Language": "en-US,en;q=0.8",
"Cache-Control": "max-age=0",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-Country": "US",
"Host": "1234567890.execute-api.us-east-1.amazonaws.com",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Custom User Agent String",
"Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==",
"X-Forwarded-For": "127.0.0.1, 127.0.0.2",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
},
"requestContext": {
"accountId": "123456789012",
"resourceId": "123456",
"stage": "prod",
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
"requestTime": "09/Apr/2015:12:34:56 +0000",
"requestTimeEpoch": 1428582896000,
"identity": {
"cognitoIdentityPoolId": null,
"accountId": null,
"cognitoIdentityId": null,
"caller": null,
"accessKey": null,
"sourceIp": "127.0.0.1",
"cognitoAuthenticationType": null,
"cognitoAuthenticationProvider": null,
"userArn": null,
"userAgent": "Custom User Agent String",
"user": null
},
"path": "/prod/hello",
"resourcePath": "/hello",
"httpMethod": "POST",
"apiId": "1234567890",
"protocol": "HTTP/1.1"
}
}

Loading