aws-lambda-proxy


Nameaws-lambda-proxy JSON
Version 1.1.0 PyPI version JSON
download
home_pageNone
SummarySimple AWS Lambda proxy to handle API Gateway request
upload_time2025-07-31 02:50:50
maintainerNone
docs_urlNone
authorNone
requires_python>=3.10
licenseBSD
keywords aws lambda apigateway proxy
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # aws-lambda-proxy

![GitHub License](https://img.shields.io/github/license/layertwo/aws-lambda-proxy)
[![Packaging status](https://img.shields.io/pypi/v/aws-lambda-proxy)](https://pypi.org/project/aws-lambda-proxy/)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/aws-lambda-proxy)
![Build status](https://img.shields.io/github/actions/workflow/status/layertwo/aws-lambda-proxy/python-package.yml?branch=main)
![PyPI - Downloads](https://img.shields.io/pypi/dm/aws-lambda-proxy)

Forked from https://github.com/vincentsarago/lambda-proxy/

A zero-requirement proxy linking AWS API Gateway `{proxy+}` requests and AWS Lambda.

<img width="600" alt="" src="https://user-images.githubusercontent.com/10407788/58742966-6ff50480-83f7-11e9-81f7-3ba7aa2310bb.png">

## Install

```bash
$ pip install -U pip
$ pip install aws-lambda-proxy
```

Or install from source:

```bash
$ git clone https://github.com/layertwo/aws-lambda-proxy.git
$ cd aws-lambda-proxy
$ pip install -U pip
$ pip install -e .
```

# Usage

Lambda proxy is designed to work well with both API Gateway's REST API and the
newer and cheaper HTTP API. If you have issues using with the HTTP API, please
open an issue.

With GET request

```python
from aws_lambda_proxy import API, Response, StatusCode

APP = API(name="app")

@APP.route('/test/tests/<id>', methods=['GET'], cors=True)
def print_id(id):
    return Response(status_code=StatusCode.OK, content_type='plain/text', body=id)
```

With POST request

```python
from aws_lambda_proxy import API, Response, StatusCode

APP = API(name="app")

@APP.route('/test/tests/<id>', methods=['POST'], cors=True)
def print_id(id, body):
    return Response(status_code=StatusCode.OK, content_type='plain/text', body=id)
```

## Binary body

```python
from aws_lambda_proxy import API

APP = API(name="app")

@APP.route('/test', methods=['POST'])
def print_id(body):
    body = json.loads(body)
```

# Routes

Route schema is simmilar to the one used in [Flask](http://flask.pocoo.org/docs/1.0/api/#url-route-registrations)

> Variable parts in the route can be specified with angular brackets `/user/<username>`. By default a variable part in the URL accepts any string without a slash however a different converter can be specified as well by using `<converter:name>`.

Converters:
- `int`: integer
- `string`: string
- `float`: float number
- `uuid`: UUID

example:
- `/app/<user>/<id>` (`user` and `id` are variables)
- `/app/<string:value>/<float:num>` (`value` will be a string, while `num` will be a float)

## Regex
You can also add regex parameters descriptions using special converter `regex()`

example:
```python
@APP.get("/app/<regex([a-z]+):regularuser>")
def print_user(regularuser):
    return Response(status_code=StatusCode.OK, content_type='plain/text', body=f"regular {regularuser}")

@APP.get("/app/<regex([A-Z]+):capitaluser>")
def print_user(capitaluser):
    return Response(status_code=StatusCode.OK, content_type='plain/text', body=f"CAPITAL {capitaluser}")
```

#### Warning

when using **regex()** you must use different variable names or the route might not show up in the documentation.

```python
@APP.get("/app/<regex([a-z]+):user>")
def print_user(user):
    return Response(status_code=StatusCode.OK, content_type='plain/text', body=f"regular {user}")

@APP.get("/app/<regex([A-Z]+):user>")
def print_user(user):
    return Response(status_code=StatusCode.OK, content_type='plain/text', body=f"CAPITAL {user}")
```
This app will work but the documentation will only show the second route because in `openapi.json`, route names will be `/app/{user}` for both routes.

# Route Options

- **path**: the URL rule as string
- **methods**: list of HTTP methods allowed, default: ["GET"]
- **cors**: allow CORS, default: `False`
- **token**: set `access_token` validation
- **payload_compression_method**: Enable and select an output body compression
- **binary_b64encode**: base64 encode the output body (API Gateway)
- **ttl**: Cache Control setting (Time to Live) 
- **cache_control**: Cache Control setting
- **description**: route description (for documentation)
- **tag**: list of tags (for documentation)

## Cache Control

Add a Cache Control header with a Time to Live (TTL) in seconds.

```python
from aws_lambda_proxy import API, Response, StatusCode
APP = API(app_name="app")

@APP.get('/test/tests/<id>', cors=True, cache_control="public,max-age=3600")
def print_id(id):
   return Response(status_code=StatusCode.OK, content_type='plain/text', body=id)
```

Note: If function returns other then "OK", Cache-Control will be set to `no-cache`

## Binary responses

When working with binary on API-Gateway we must return a base64 encoded string

```python
from aws_lambda_proxy import API, Response, StatusCode

APP = API(name="app")

@APP.get('/test/tests/<filename>.jpg', cors=True, binary_b64encode=True)
def print_id(filename):
    with open(f"{filename}.jpg", "rb") as f:
        return Response(status_code=StatusCode.OK, content_type='image/jpeg', body=f.read())
```

## Compression

Enable compression if "Accept-Encoding" if found in headers.

```python
from aws_lambda_proxy import API, Response, StatusCode

APP = API(name="app")

@APP.get(
   '/test/tests/<filename>.jpg',
   cors=True,
   binary_b64encode=True,
   payload_compression_method="gzip"
)
def print_id(filename):
    with open(f"{filename}.jpg", "rb") as f:
       return Response(status_code=StatusCode.OK, content_type='image/jpeg', body=f.read())
```

## Simple Auth token

Lambda-proxy provide a simple token validation system.

-  a "TOKEN" variable must be set in the environment
-  each request must provide a "access_token" params (e.g curl
   http://myurl/test/tests/myid?access_token=blabla)

```python
from aws_lambda_proxy import API, Response, StatusCode

APP = API(name="app")

@APP.get('/test/tests/<id>', cors=True, token=True)
def print_id(id):
    return Response(status_code=StatusCode.OK, content_type='plain/text', body=id)
```

## URL schema and request parameters

QueryString parameters are passed as function's options.

```python
from aws_lambda_proxy import API, Response, StatusCode

APP = API(name="app")

@APP.get('/<id>', cors=True)
def print_id(id, name=None):
    return Response(status_code=StatusCode.OK, content_type='plain/text', body=f"{id}{name}")
```

requests:

```bash
$ curl /000001
   0001

$ curl /000001?name=layertwo
   0001layertwo
```

## Multiple Routes

```python
from aws_lambda_proxy import API, Response, StatusCode
APP = API(name="app")

@APP.get('/<id>', cors=True)
@APP.get('/<id>/<int:number>', cors=True)
def print_id(id, number=None, name=None):
    return Response(status_code=StatusCode.OK, content_type='plain/text', body=f"{id}-{name}-{number}")
```
requests:

```bash

$ curl /000001
   0001--

$ curl /000001?name=layertwo
   0001-layertwo-

$ curl /000001/1?name=layertwo
   0001-layertwo-1
```

# Advanced features

## Context and Event passing

Pass event and context to the handler function.

```python
from aws_lambda_proxy import API, Response, StatusCode

APP = API(name="app")

@APP.get("/<id>", cors=True)
@APP.pass_event
@APP.pass_context
def print_id(ctx, evt, id):
    print(ctx)
    print(evt)
    return Response(status_code=StatusCode.OK, content_type='plain/text', body=f"{id}")
```

# Automatic OpenAPI documentation

By default the APP (`aws_lambda_proxy.API`) is provided with three (3) routes:
- `/openapi.json`: print OpenAPI JSON definition

- `/docs`: swagger html UI
![swagger](https://user-images.githubusercontent.com/10407788/58707335-9cbb0480-8382-11e9-927f-8d992cf2531a.jpg)

- `/redoc`: Redoc html UI
![redoc](https://user-images.githubusercontent.com/10407788/58707338-9dec3180-8382-11e9-8dec-18173e39258f.jpg)

**Function annotations**

To be able to render full and precise API documentation, aws_lambda_proxy uses python type hint and annotations [link](https://www.python.org/dev/peps/pep-3107/).

```python
from aws_lambda_proxy import API, Response, StatusCode 

APP = API(name="app")

@APP.route('/test/<int:id>', methods=['GET'], cors=True)
def print_id(id: int, num: float = 0.2) -> Response:
    return Response(status_code=StatusCode.OK, content_type='plain/text', body=id)
```

In the example above, our route `/test/<int:id>` define an input `id` to be a `INT`, while we also add this hint to the function `print_id` we also specify the type (and default) of the `num` option.

# Custom Domain and path mapping

Note: When using path mapping other than `root` (`/`), `/` route won't be available.

```python
from aws_lambda_proxy import API, Response, StatusCode 

api = API(name="api", debug=True)


# This route won't work when using path mapping
@api.get("/", cors=True)
# This route will work only if the path mapping is set to /api
@api.get("/api", cors=True)
def index():
    html = """<!DOCTYPE html>
    <html>
        <header><title>This is title</title></header>
        <body>
            Hello world
        </body>
    </html>"""
    return Response(status_code=StatusCode.OK, content_type="text/html", body=html)


@api.get("/yo", cors=True)
def yo():
    return Response(status_code=StatusCode.OK, content_type="text/plain", body="YOOOOO")
```

# Examples

-  https://github.com/layertwo/aws-lambda-proxy/tree/main/example


# Contribution & Development

Issues and pull requests are more than welcome.

**Dev install & Pull-Request**

```bash
$ git clone https://github.com/layertwo/aws-lambda-proxy.git
$ cd lambda-proxy
$ pip install -e .[dev]
```

### License

See [LICENSE.txt](/LICENSE.txt>).

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "aws-lambda-proxy",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": null,
    "keywords": "aws, lambda, apigateway, proxy",
    "author": null,
    "author_email": "Lucas Messenger <1335960+layertwo@users.noreply.github.com>",
    "download_url": "https://files.pythonhosted.org/packages/ee/7d/c8d1184752afbb163845cff5397d0c5e606b059f0e829d99a04a7f75164f/aws_lambda_proxy-1.1.0.tar.gz",
    "platform": null,
    "description": "# aws-lambda-proxy\n\n![GitHub License](https://img.shields.io/github/license/layertwo/aws-lambda-proxy)\n[![Packaging status](https://img.shields.io/pypi/v/aws-lambda-proxy)](https://pypi.org/project/aws-lambda-proxy/)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/aws-lambda-proxy)\n![Build status](https://img.shields.io/github/actions/workflow/status/layertwo/aws-lambda-proxy/python-package.yml?branch=main)\n![PyPI - Downloads](https://img.shields.io/pypi/dm/aws-lambda-proxy)\n\nForked from https://github.com/vincentsarago/lambda-proxy/\n\nA zero-requirement proxy linking AWS API Gateway `{proxy+}` requests and AWS Lambda.\n\n<img width=\"600\" alt=\"\" src=\"https://user-images.githubusercontent.com/10407788/58742966-6ff50480-83f7-11e9-81f7-3ba7aa2310bb.png\">\n\n## Install\n\n```bash\n$ pip install -U pip\n$ pip install aws-lambda-proxy\n```\n\nOr install from source:\n\n```bash\n$ git clone https://github.com/layertwo/aws-lambda-proxy.git\n$ cd aws-lambda-proxy\n$ pip install -U pip\n$ pip install -e .\n```\n\n# Usage\n\nLambda proxy is designed to work well with both API Gateway's REST API and the\nnewer and cheaper HTTP API. If you have issues using with the HTTP API, please\nopen an issue.\n\nWith GET request\n\n```python\nfrom aws_lambda_proxy import API, Response, StatusCode\n\nAPP = API(name=\"app\")\n\n@APP.route('/test/tests/<id>', methods=['GET'], cors=True)\ndef print_id(id):\n    return Response(status_code=StatusCode.OK, content_type='plain/text', body=id)\n```\n\nWith POST request\n\n```python\nfrom aws_lambda_proxy import API, Response, StatusCode\n\nAPP = API(name=\"app\")\n\n@APP.route('/test/tests/<id>', methods=['POST'], cors=True)\ndef print_id(id, body):\n    return Response(status_code=StatusCode.OK, content_type='plain/text', body=id)\n```\n\n## Binary body\n\n```python\nfrom aws_lambda_proxy import API\n\nAPP = API(name=\"app\")\n\n@APP.route('/test', methods=['POST'])\ndef print_id(body):\n    body = json.loads(body)\n```\n\n# Routes\n\nRoute schema is simmilar to the one used in [Flask](http://flask.pocoo.org/docs/1.0/api/#url-route-registrations)\n\n> Variable parts in the route can be specified with angular brackets `/user/<username>`. By default a variable part in the URL accepts any string without a slash however a different converter can be specified as well by using `<converter:name>`.\n\nConverters:\n- `int`: integer\n- `string`: string\n- `float`: float number\n- `uuid`: UUID\n\nexample:\n- `/app/<user>/<id>` (`user` and `id` are variables)\n- `/app/<string:value>/<float:num>` (`value` will be a string, while `num` will be a float)\n\n## Regex\nYou can also add regex parameters descriptions using special converter `regex()`\n\nexample:\n```python\n@APP.get(\"/app/<regex([a-z]+):regularuser>\")\ndef print_user(regularuser):\n    return Response(status_code=StatusCode.OK, content_type='plain/text', body=f\"regular {regularuser}\")\n\n@APP.get(\"/app/<regex([A-Z]+):capitaluser>\")\ndef print_user(capitaluser):\n    return Response(status_code=StatusCode.OK, content_type='plain/text', body=f\"CAPITAL {capitaluser}\")\n```\n\n#### Warning\n\nwhen using **regex()** you must use different variable names or the route might not show up in the documentation.\n\n```python\n@APP.get(\"/app/<regex([a-z]+):user>\")\ndef print_user(user):\n    return Response(status_code=StatusCode.OK, content_type='plain/text', body=f\"regular {user}\")\n\n@APP.get(\"/app/<regex([A-Z]+):user>\")\ndef print_user(user):\n    return Response(status_code=StatusCode.OK, content_type='plain/text', body=f\"CAPITAL {user}\")\n```\nThis app will work but the documentation will only show the second route because in `openapi.json`, route names will be `/app/{user}` for both routes.\n\n# Route Options\n\n- **path**: the URL rule as string\n- **methods**: list of HTTP methods allowed, default: [\"GET\"]\n- **cors**: allow CORS, default: `False`\n- **token**: set `access_token` validation\n- **payload_compression_method**: Enable and select an output body compression\n- **binary_b64encode**: base64 encode the output body (API Gateway)\n- **ttl**: Cache Control setting (Time to Live) \n- **cache_control**: Cache Control setting\n- **description**: route description (for documentation)\n- **tag**: list of tags (for documentation)\n\n## Cache Control\n\nAdd a Cache Control header with a Time to Live (TTL) in seconds.\n\n```python\nfrom aws_lambda_proxy import API, Response, StatusCode\nAPP = API(app_name=\"app\")\n\n@APP.get('/test/tests/<id>', cors=True, cache_control=\"public,max-age=3600\")\ndef print_id(id):\n   return Response(status_code=StatusCode.OK, content_type='plain/text', body=id)\n```\n\nNote: If function returns other then \"OK\", Cache-Control will be set to `no-cache`\n\n## Binary responses\n\nWhen working with binary on API-Gateway we must return a base64 encoded string\n\n```python\nfrom aws_lambda_proxy import API, Response, StatusCode\n\nAPP = API(name=\"app\")\n\n@APP.get('/test/tests/<filename>.jpg', cors=True, binary_b64encode=True)\ndef print_id(filename):\n    with open(f\"{filename}.jpg\", \"rb\") as f:\n        return Response(status_code=StatusCode.OK, content_type='image/jpeg', body=f.read())\n```\n\n## Compression\n\nEnable compression if \"Accept-Encoding\" if found in headers.\n\n```python\nfrom aws_lambda_proxy import API, Response, StatusCode\n\nAPP = API(name=\"app\")\n\n@APP.get(\n   '/test/tests/<filename>.jpg',\n   cors=True,\n   binary_b64encode=True,\n   payload_compression_method=\"gzip\"\n)\ndef print_id(filename):\n    with open(f\"{filename}.jpg\", \"rb\") as f:\n       return Response(status_code=StatusCode.OK, content_type='image/jpeg', body=f.read())\n```\n\n## Simple Auth token\n\nLambda-proxy provide a simple token validation system.\n\n-  a \"TOKEN\" variable must be set in the environment\n-  each request must provide a \"access_token\" params (e.g curl\n   http://myurl/test/tests/myid?access_token=blabla)\n\n```python\nfrom aws_lambda_proxy import API, Response, StatusCode\n\nAPP = API(name=\"app\")\n\n@APP.get('/test/tests/<id>', cors=True, token=True)\ndef print_id(id):\n    return Response(status_code=StatusCode.OK, content_type='plain/text', body=id)\n```\n\n## URL schema and request parameters\n\nQueryString parameters are passed as function's options.\n\n```python\nfrom aws_lambda_proxy import API, Response, StatusCode\n\nAPP = API(name=\"app\")\n\n@APP.get('/<id>', cors=True)\ndef print_id(id, name=None):\n    return Response(status_code=StatusCode.OK, content_type='plain/text', body=f\"{id}{name}\")\n```\n\nrequests:\n\n```bash\n$ curl /000001\n   0001\n\n$ curl /000001?name=layertwo\n   0001layertwo\n```\n\n## Multiple Routes\n\n```python\nfrom aws_lambda_proxy import API, Response, StatusCode\nAPP = API(name=\"app\")\n\n@APP.get('/<id>', cors=True)\n@APP.get('/<id>/<int:number>', cors=True)\ndef print_id(id, number=None, name=None):\n    return Response(status_code=StatusCode.OK, content_type='plain/text', body=f\"{id}-{name}-{number}\")\n```\nrequests:\n\n```bash\n\n$ curl /000001\n   0001--\n\n$ curl /000001?name=layertwo\n   0001-layertwo-\n\n$ curl /000001/1?name=layertwo\n   0001-layertwo-1\n```\n\n# Advanced features\n\n## Context and Event passing\n\nPass event and context to the handler function.\n\n```python\nfrom aws_lambda_proxy import API, Response, StatusCode\n\nAPP = API(name=\"app\")\n\n@APP.get(\"/<id>\", cors=True)\n@APP.pass_event\n@APP.pass_context\ndef print_id(ctx, evt, id):\n    print(ctx)\n    print(evt)\n    return Response(status_code=StatusCode.OK, content_type='plain/text', body=f\"{id}\")\n```\n\n# Automatic OpenAPI documentation\n\nBy default the APP (`aws_lambda_proxy.API`) is provided with three (3) routes:\n- `/openapi.json`: print OpenAPI JSON definition\n\n- `/docs`: swagger html UI\n![swagger](https://user-images.githubusercontent.com/10407788/58707335-9cbb0480-8382-11e9-927f-8d992cf2531a.jpg)\n\n- `/redoc`: Redoc html UI\n![redoc](https://user-images.githubusercontent.com/10407788/58707338-9dec3180-8382-11e9-8dec-18173e39258f.jpg)\n\n**Function annotations**\n\nTo be able to render full and precise API documentation, aws_lambda_proxy uses python type hint and annotations [link](https://www.python.org/dev/peps/pep-3107/).\n\n```python\nfrom aws_lambda_proxy import API, Response, StatusCode \n\nAPP = API(name=\"app\")\n\n@APP.route('/test/<int:id>', methods=['GET'], cors=True)\ndef print_id(id: int, num: float = 0.2) -> Response:\n    return Response(status_code=StatusCode.OK, content_type='plain/text', body=id)\n```\n\nIn the example above, our route `/test/<int:id>` define an input `id` to be a `INT`, while we also add this hint to the function `print_id` we also specify the type (and default) of the `num` option.\n\n# Custom Domain and path mapping\n\nNote: When using path mapping other than `root` (`/`), `/` route won't be available.\n\n```python\nfrom aws_lambda_proxy import API, Response, StatusCode \n\napi = API(name=\"api\", debug=True)\n\n\n# This route won't work when using path mapping\n@api.get(\"/\", cors=True)\n# This route will work only if the path mapping is set to /api\n@api.get(\"/api\", cors=True)\ndef index():\n    html = \"\"\"<!DOCTYPE html>\n    <html>\n        <header><title>This is title</title></header>\n        <body>\n            Hello world\n        </body>\n    </html>\"\"\"\n    return Response(status_code=StatusCode.OK, content_type=\"text/html\", body=html)\n\n\n@api.get(\"/yo\", cors=True)\ndef yo():\n    return Response(status_code=StatusCode.OK, content_type=\"text/plain\", body=\"YOOOOO\")\n```\n\n# Examples\n\n-  https://github.com/layertwo/aws-lambda-proxy/tree/main/example\n\n\n# Contribution & Development\n\nIssues and pull requests are more than welcome.\n\n**Dev install & Pull-Request**\n\n```bash\n$ git clone https://github.com/layertwo/aws-lambda-proxy.git\n$ cd lambda-proxy\n$ pip install -e .[dev]\n```\n\n### License\n\nSee [LICENSE.txt](/LICENSE.txt>).\n",
    "bugtrack_url": null,
    "license": "BSD",
    "summary": "Simple AWS Lambda proxy to handle API Gateway request",
    "version": "1.1.0",
    "project_urls": null,
    "split_keywords": [
        "aws",
        " lambda",
        " apigateway",
        " proxy"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "ed8c543d9b34ddb44133d11a027bb0276a397ff4ffbbea910e1fda3707505738",
                "md5": "e9babc77e455a47e38628e454ec131b8",
                "sha256": "2a973c9843e673c875e1bf86597b4720f7a6c13e427f36d594e7a7441a274368"
            },
            "downloads": -1,
            "filename": "aws_lambda_proxy-1.1.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e9babc77e455a47e38628e454ec131b8",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 13423,
            "upload_time": "2025-07-31T02:50:49",
            "upload_time_iso_8601": "2025-07-31T02:50:49.493214Z",
            "url": "https://files.pythonhosted.org/packages/ed/8c/543d9b34ddb44133d11a027bb0276a397ff4ffbbea910e1fda3707505738/aws_lambda_proxy-1.1.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "ee7dc8d1184752afbb163845cff5397d0c5e606b059f0e829d99a04a7f75164f",
                "md5": "4242fac9e083d27936dd42dfd1f8d871",
                "sha256": "19b80d832bd03151dfc0215b9d5c72f50c42653dc431cf57daeec38f9262ee3c"
            },
            "downloads": -1,
            "filename": "aws_lambda_proxy-1.1.0.tar.gz",
            "has_sig": false,
            "md5_digest": "4242fac9e083d27936dd42dfd1f8d871",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 20961,
            "upload_time": "2025-07-31T02:50:50",
            "upload_time_iso_8601": "2025-07-31T02:50:50.584999Z",
            "url": "https://files.pythonhosted.org/packages/ee/7d/c8d1184752afbb163845cff5397d0c5e606b059f0e829d99a04a7f75164f/aws_lambda_proxy-1.1.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-31 02:50:50",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "aws-lambda-proxy"
}
        
Elapsed time: 2.01898s