Skip to main content

Build API with ASGI in AWS Lambda with API Gateway HTTP API or REST API, or with Function URL ✨

Project description

asgi-aws

Tests Lint and Format codecov Language Star Badge Pypi

Build API with ASGI in AWS Lambda with API Gateway HTTP API or REST API, or with Function URL ✨

Installation

pip install asgi_aws

Example

  • Create a file main.py with:
from asgi_aws import Asgi, AsgiService
from typing import Optional
from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"Hello": "World"}

@app.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
    return {"item_id": item_id, "q": q}

entry_point = Asgi.entry_point(app, AsgiService.aws)

Deploy it

  • Let's create for exampple a yaml file with the following content:
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31

Resources:
  ExFunctionUrlAPI:
    Type: AWS::Serverless::Function
    Properties:
      Runtime: python3.9
      CodeUri: src/
      Handler: main.entry_point
      MemorySize: 256
      Timeout: 30
      FunctionUrlConfig:
        AuthType: NONE
      Environment:
        Variables:
          AsgiService: AWS Lambda
  • Now, we can deploy the function with the following command:
# deploy HTTP API
sam build -t api.yaml --use-container
sam run deploy

Note: You can also deploy the function under Deployment for Rest API or with a Function URL.

Development 🚧

Setup environment 📦

You should create a virtual environment and activate it:

python -m venv venv/
source venv/bin/activate

And then install the development dependencies:

# Install Flit
pip install flit

# Install dependencies
flit install --symlink

Run tests 🌝

You can run all the tests with:

make test

Format the code 🍂

Execute the following command to apply pre-commit formatting:

make lint

License

This project is licensed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asgi_aws-1.0.0.tar.gz (13.7 kB view hashes)

Uploaded Source

Built Distribution

asgi_aws-1.0.0-py3-none-any.whl (7.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page