Skip to main content

モダンな AWS Lambda 用 API フレームワーク

Project description

lambapi

モダンな AWS Lambda 用 API フレームワーク

Python Version License

AWS Lambda で直感的でモダンな API を構築できる軽量フレームワーク。依存性注入システム、自動型変換、バリデーション、CORS サポートを標準提供。

✨ 主な特徴

  • 🚀 モダンな API - デコレータベースの直感的なルート定義
  • 💉 依存性注入 - Query, Path, Body, Authenticated による型安全なパラメータ取得
  • 🔄 自動型変換・バリデーション - データクラスと型ヒントによる自動処理
  • 🛡️ 豊富なバリデーション - 数値範囲、文字列長、正規表現などの制約チェック
  • 🌐 CORS サポート - プリフライトリクエストの自動処理
  • 🔐 認証システム - DynamoDB + JWT による完全な認証・認可機能
  • 🛡️ 構造化エラーハンドリング - 本番運用に適した統一エラーレスポンス
  • 📦 軽量設計 - 標準ライブラリベース、外部依存最小化
  • 🔒 完全な型安全 - mypy 対応の型ヒントで開発体験を向上

🚀 クイックスタート

インストール

# 基本インストール
pip install lambapi

# ローカル開発環境(uvicorn 付き)
pip install lambapi[dev]

# または uvicorn を個別にインストール
pip install lambapi uvicorn[standard]

基本的な使用例

from lambapi import API, create_lambda_handler, Query, Path, Body
from dataclasses import dataclass
from typing import Optional

@dataclass
class CreateUserRequest:
    name: str
    email: str
    age: Optional[int] = None

def create_app(event, context):
    app = API(event, context)

    @app.get("/")
    def hello():
        return {"message": "Hello, lambapi!"}

    @app.get("/search")
    def search(
        query: str = Query(..., description="検索クエリ", min_length=1),
        limit: int = Query(10, ge=1, le=100, description="結果数"),
        category: str = Query("all", description="カテゴリー")
    ):
        return {"query": query, "limit": limit, "category": category}

    @app.post("/users")
    def create_user(user_data: CreateUserRequest = Body(...)):
        return {
            "message": "ユーザーが作成されました", 
            "user": {"name": user_data.name, "email": user_data.email, "age": user_data.age}
        }

    @app.get("/users/{user_id}")
    def get_user(user_id: str = Path(..., description="ユーザー ID")):
        return {"user_id": user_id, "name": f"User {user_id}"}

    return app

lambda_handler = create_lambda_handler(create_app)

ローカル開発

lambapi は uvicorn を使用した高性能なローカル開発サーバーを提供します。AWS Lambda と API Gateway の環境を完全に再現し、本番環境と同等の動作を保証します。

# 新しいプロジェクトを作成
lambapi create my-api --template basic

# 高性能ローカルサーバーを起動(uvicorn + ホットリロード付き)
lambapi serve app

# カスタムポート・ホスト設定
lambapi serve app --host 0.0.0.0 --port 8080

# 詳細ログでデバッグ
lambapi serve app --debug --log-level debug

# API 動作確認
curl http://localhost:8000/
curl -X POST http://localhost:8000/users -H "Content-Type: application/json" -d '{"name":"test"}'

uvicorn 統合の利点:

  • 🚀 高性能 - 非同期 ASGI ベースで高速レスポンス
  • 🔄 ホットリロード - コード変更を即座に反映
  • 🌐 API Gateway 互換 - 本番環境と同等のリクエスト/レスポンス形式
  • 📊 詳細ログ - リクエスト詳細とエラー情報の表示
  • ⚙️ 豊富な設定 - ワーカー数、ログレベル等のカスタマイズ可能

📚 ドキュメント

完全なドキュメントは https://sskyh0208.github.io/lambapi/ で公開されています。

💡 なぜ lambapi?

従来の問題

# 従来の Lambda ハンドラー(煩雑)
def lambda_handler(event, context):
    method = event['httpMethod']
    path = event['path']
    query_params = event.get('queryStringParameters', {}) or {}
    
    # 手動でパラメータ取得・型変換・バリデーション
    try:
        limit = int(query_params.get('limit', 10))
        if limit <= 0 or limit > 100:
            return {
                'statusCode': 400,
                'body': json.dumps({'error': 'Invalid limit'})
            }
    except ValueError:
        return {
            'statusCode': 400, 
            'body': json.dumps({'error': 'Invalid limit format'})
        }
    
    # 複雑なルーティング...
    if method == 'GET' and path == '/users':
        # ビジネスロジック
        pass

    return {
        'statusCode': 200,
        'headers': {'Content-Type': 'application/json'},
        'body': json.dumps({'users': []})
    }

lambapi なら

# lambapi 版(シンプルで型安全)
@app.get("/users")
def get_users(limit: int = Query(10, ge=1, le=100, description="取得件数")):
    return {"users": [f"user-{i}" for i in range(limit)]}

削減できるコード: パラメータ取得、型変換、バリデーション、エラーハンドリングが自動化され、約 80% のボイラープレートコードを削減

🤝 コミュニティ

  • 📁 GitHub - ソースコード・ Issues ・ Discussions
  • 📦 PyPI - パッケージダウンロード
  • 📚 ドキュメント - 完全な使用ガイド

📄 ライセンス

MIT License - 詳細は 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

lambapi-0.2.9.tar.gz (95.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lambapi-0.2.9-py3-none-any.whl (49.5 kB view details)

Uploaded Python 3

File details

Details for the file lambapi-0.2.9.tar.gz.

File metadata

  • Download URL: lambapi-0.2.9.tar.gz
  • Upload date:
  • Size: 95.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.13.1 Linux/6.11.0-1018-azure

File hashes

Hashes for lambapi-0.2.9.tar.gz
Algorithm Hash digest
SHA256 383f9de97a931a29fdfadbf82eea0013513e6a09b1b76405c3ffacbb45fe6536
MD5 12d663a6a53d16b0671043efbc67dd60
BLAKE2b-256 3ed7d1394478cdee493bbd5e8643f90d0e367cb7439f0a29d44ca1338555ae35

See more details on using hashes here.

File details

Details for the file lambapi-0.2.9-py3-none-any.whl.

File metadata

  • Download URL: lambapi-0.2.9-py3-none-any.whl
  • Upload date:
  • Size: 49.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.13.1 Linux/6.11.0-1018-azure

File hashes

Hashes for lambapi-0.2.9-py3-none-any.whl
Algorithm Hash digest
SHA256 4687cf37c3f621e1517e2ec533e64d5d76a5a2f8b8a2bfe82ffdef11565b77ad
MD5 9ae159c64f4d498f8efbb3d1f2e63580
BLAKE2b-256 71f379540d63010c7ecabae5cf0b8159ed3d141e688a1c39cce9eb748bc84c30

See more details on using hashes here.

Supported by

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