Skip to content

Apigateway Quickstart

Event handler and router for Amazon APIGateway REST & GraphQL, allowing you to take advantage of procesing all api requests in one lambda.

Features

  • Configurable router based on 3 common routing patterns
  • Built-in request validation using standard OpenAPI schema
  • Easily validate request in modular and declarative way without any additional code
  • Able to easily extend with custom middleware at both app and per-endpoint levels
  • Support for CORS, binary and Gzip compression

Installation

1
2
3
$ pip install acai_aws
# pipenv install acai_aws
# poetry add acai_aws

Minimal Setup

After installation, create a handler file and configure the AWS lambda to use that file as its handler. For the full set of configurations, please review the full list of configurations found router setup page.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
from acai_aws.apigateway.router import Router

router = Router(
    base_path='your-service/v1',
    handlers='api/handlers',
    schema='api/openapi.yml'
)
router.auto_load()

def handle(event, context):
    return router.route(event, context)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
functions:
    apigateway-handler:
        handler: api/handlers/router.handle
        events:
            - http:
                path: /
                method: ANY
            - http:
                path: /{proxy+}
                method: ANY    
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
~~ Directory ~~                     ~~ Route ~~
===================================================================
πŸ“¦api/                              |          
β”‚---πŸ“‚handlers                      |           
    β”‚---πŸ“œrouter.py                 |
    β”‚---πŸ“œorg.py                    | /org    
    β”‚---πŸ“‚grower                    |
        β”‚---πŸ“œ__init__.py           | /grower
        β”‚---πŸ“œ_grower_id.py         | /grower/{grower_id}
    β”‚---πŸ“‚farm                      |
        β”‚---πŸ“œ__init__.py           | /farm
        β”‚---πŸ“‚_farm_id              |
            β”‚---πŸ“œ__init__.py       | /farm/{farm_id}
            β”‚---πŸ“‚field             |
                β”‚---πŸ“œ__init__.py   | /farm/{farm_id}/field
                β”‚---πŸ“œ_field_id.py  | /farm/{farm_id}/field/{field_id}