aws-lambda-proxy


Nameaws-lambda-proxy JSON
Version 1.0.0 PyPI version JSON
download
home_pageNone
SummarySimple AWS Lambda proxy to handle API Gateway request
upload_time2025-07-29 02:45:12
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

[![Packaging status](https://badge.fury.io/py/aws-lambda-proxy.svg)](https://badge.fury.io/py/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

APP = API(name="app")

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

With POST request

```python
from aws_lambda_proxy import API

APP = API(name="app")

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

## Binary body

```python
from aws_lambda_proxy import API

APP = API(name="app")

@APP.route('/test', methods=['POST']e)
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 ('OK', 'plain/text', f"regular {regularuser}")

@APP.get("/app/<regex([A-Z]+):capitaluser>")
def print_user(capitaluser):
    return ('OK', 'plain/text', 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 ('OK', 'plain/text', f"regular {user}")

@APP.get("/app/<regex([A-Z]+):user>")
def print_user(user):
    return ('OK', 'plain/text', 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
APP = API(app_name="app")

@APP.get('/test/tests/<id>', cors=True, cache_control="public,max-age=3600")
def print_id(id):
   return ('OK', 'plain/text', 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

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 ('OK', 'image/jpeg', f.read())
```

## Compression

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

```python
from aws_lambda_proxy import API

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 ('OK', 'image/jpeg', 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

APP = API(name="app")

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

## URL schema and request parameters

QueryString parameters are passed as function's options.

```python
from aws_lambda_proxy import API

APP = API(name="app")

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

requests:

```bash
$ curl /000001
   0001

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

## Multiple Routes

```python
from aws_lambda_proxy import API
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 ('OK', 'plain/text', 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

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 ('OK', 'plain/text', 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

APP = API(name="app")

@APP.route('/test/<int:id>', methods=['GET'], cors=True)
def print_id(id: int, num: float = 0.2) -> Tuple(str, str, str):
    return ('OK', 'plain/text', 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

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 ("OK", "text/html", html)


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

# Examples

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


# Contribution & Devellopement

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/82/46/6bd033aef00fa3c3d4ae73d28ed274b5d2ed39fb04a7f64d08b625500f04/aws_lambda_proxy-1.0.0.tar.gz",
    "platform": null,
    "description": "# aws-lambda-proxy\n\n[![Packaging status](https://badge.fury.io/py/aws-lambda-proxy.svg)](https://badge.fury.io/py/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\n\nAPP = API(name=\"app\")\n\n@APP.route('/test/tests/<id>', methods=['GET'], cors=True)\ndef print_id(id):\n    return ('OK', 'plain/text', id)\n```\n\nWith POST request\n\n```python\nfrom aws_lambda_proxy import API\n\nAPP = API(name=\"app\")\n\n@APP.route('/test/tests/<id>', methods=['POST'], cors=True)\ndef print_id(id, body):\n    return ('OK', 'plain/text', 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']e)\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 ('OK', 'plain/text', f\"regular {regularuser}\")\n\n@APP.get(\"/app/<regex([A-Z]+):capitaluser>\")\ndef print_user(capitaluser):\n    return ('OK', 'plain/text', 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 ('OK', 'plain/text', f\"regular {user}\")\n\n@APP.get(\"/app/<regex([A-Z]+):user>\")\ndef print_user(user):\n    return ('OK', 'plain/text', 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\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 ('OK', 'plain/text', 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\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 ('OK', 'image/jpeg', 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\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 ('OK', 'image/jpeg', 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\n\nAPP = API(name=\"app\")\n\n@APP.get('/test/tests/<id>', cors=True, token=True)\ndef print_id(id):\n    return ('OK', 'plain/text', 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\n\nAPP = API(name=\"app\")\n\n@APP.get('/<id>', cors=True)\ndef print_id(id, name=None):\n    return ('OK', 'plain/text', 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\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 ('OK', 'plain/text', 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\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 ('OK', 'plain/text', 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\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) -> Tuple(str, str, str):\n    return ('OK', 'plain/text', 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\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 (\"OK\", \"text/html\", html)\n\n\n@api.get(\"/yo\", cors=True)\ndef yo():\n    return (\"OK\", \"text/plain\", \"YOOOOO\")\n```\n\n# Examples\n\n-  https://github.com/aws-lambda-proxy/tree/main/example\n\n\n# Contribution & Devellopement\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.0.0",
    "project_urls": null,
    "split_keywords": [
        "aws",
        " lambda",
        " apigateway",
        " proxy"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "c41693880c1d5a3d1f04b08cd3ce8ac7b2a7af62caa9c053a473099d0fe3abcc",
                "md5": "eab72529db10152aa57a1f20b4e2fe6e",
                "sha256": "352b8ff84f3ba6972868bbb3e618515e3b2852558555668d7fbbcac782bfafed"
            },
            "downloads": -1,
            "filename": "aws_lambda_proxy-1.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "eab72529db10152aa57a1f20b4e2fe6e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 12582,
            "upload_time": "2025-07-29T02:45:11",
            "upload_time_iso_8601": "2025-07-29T02:45:11.899192Z",
            "url": "https://files.pythonhosted.org/packages/c4/16/93880c1d5a3d1f04b08cd3ce8ac7b2a7af62caa9c053a473099d0fe3abcc/aws_lambda_proxy-1.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "82466bd033aef00fa3c3d4ae73d28ed274b5d2ed39fb04a7f64d08b625500f04",
                "md5": "406fb1528c993f081d48cfaa70f3db96",
                "sha256": "53edef5927423a47d2f81e00bd32d778754c622985fefc71d92f887bb52cc209"
            },
            "downloads": -1,
            "filename": "aws_lambda_proxy-1.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "406fb1528c993f081d48cfaa70f3db96",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 20142,
            "upload_time": "2025-07-29T02:45:12",
            "upload_time_iso_8601": "2025-07-29T02:45:12.937292Z",
            "url": "https://files.pythonhosted.org/packages/82/46/6bd033aef00fa3c3d4ae73d28ed274b5d2ed39fb04a7f64d08b625500f04/aws_lambda_proxy-1.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-07-29 02:45:12",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "aws-lambda-proxy"
}
        
Elapsed time: 1.44633s