{"id":27261507,"url":"https://github.com/shenoy-anurag/fastapi-aws-lambda-template","last_synced_at":"2026-04-05T21:02:39.276Z","repository":{"id":287141220,"uuid":"963710220","full_name":"shenoy-anurag/fastapi-aws-lambda-template","owner":"shenoy-anurag","description":"A FastAPI AWS Lambda Template for quickly deploying an app to AWS Lambda using AWS ECR. Using FastAPI, SQLModel, PostgreSQL, Docker, Mangum, MVC pattern and more.","archived":false,"fork":false,"pushed_at":"2025-04-10T07:31:34.000Z","size":69,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-11T05:39:53.732Z","etag":null,"topics":["aws","aws-lambda","backend","docker","fastapi","jwt","postgresql","python","sqlmodel","template","template-project"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shenoy-anurag.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-04-10T05:12:10.000Z","updated_at":"2025-04-10T07:43:51.000Z","dependencies_parsed_at":"2025-04-10T07:35:56.569Z","dependency_job_id":null,"html_url":"https://github.com/shenoy-anurag/fastapi-aws-lambda-template","commit_stats":null,"previous_names":["shenoy-anurag/fastapi-aws-lambda-template"],"tags_count":1,"template":true,"template_full_name":null,"purl":"pkg:github/shenoy-anurag/fastapi-aws-lambda-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shenoy-anurag%2Ffastapi-aws-lambda-template","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shenoy-anurag%2Ffastapi-aws-lambda-template/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shenoy-anurag%2Ffastapi-aws-lambda-template/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shenoy-anurag%2Ffastapi-aws-lambda-template/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shenoy-anurag","download_url":"https://codeload.github.com/shenoy-anurag/fastapi-aws-lambda-template/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shenoy-anurag%2Ffastapi-aws-lambda-template/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269807658,"owners_count":24478491,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-10T02:00:08.965Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","aws-lambda","backend","docker","fastapi","jwt","postgresql","python","sqlmodel","template","template-project"],"created_at":"2025-04-11T05:33:29.799Z","updated_at":"2025-12-30T21:48:07.307Z","avatar_url":"https://github.com/shenoy-anurag.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FastAPI AWS Lambda Template\nA FastAPI AWS Lambda Template for quickly deploying an app to AWS Lambda using AWS ECR.\n\nFollow my step-by-step blog post to deploy this app to AWS Lambda in half an hour.\n\nhttps://www.anuragshenoy.com/blog/fastapi-deployment-using-aws-lambda\n\n## What makes this template different?\n- **Use of Docker Compose for Lambda Deployment**\n  - No need for `.tar.gz` files; simplifies the deployment process.\n  - Supports deployment to AWS Lambda regardless of processor architecture (e.g., `arm64`/`aarch64` or `x86_64`).\n  - Tested using M1 Mac (arm64 architecture).\n- **Full-fledged FastAPI Application**\n  - Unlike most examples that focus on single-file, single-endpoint setups, this template supports building a complete backend for hobby or production projects on Lambda.\n- **Database Integration**\n  - Includes support for database connections, which is often missing in other templates.  \n  - Designed to handle Lambda's ephemeral nature by recommending transaction-pooler connection strings for external databases to avoid connection loss during cold starts.\n\n- **No Built-in Model Inference**\n  - A lot of examples show model inference on Lambda using FastAPI.\n  - Unless your model is tiny, say a regression model, decision tree or a boosting model, the startup time of Lambda defeats the purpose. Latency will be quite high unless it's warmed up. \n  - It makes more sense to use an external API for inference (e.g., OpenAI, Mistral, Anthropic, AWS Bedrock) for better performance and scalability.\n\n## Technology Stack and Features\n- ⚡ FastAPI for the Python backend API.\n  - 🧰 SQLModel for the Python SQL database interactions (ORM).\n  - 🔍 Pydantic, used by FastAPI, for the data validation and settings management.\n  - 💾 PostgreSQL as the SQL database.\n  - 🎟️ Mangum to handle AWS Lambda events.\n- 🐋 Docker Compose for development and production.\n- 🔒 Secure password hashing by default.\n- 🔑 JWT (JSON Web Token) authentication.\n- 🚢 Deployment instructions using Docker Compose.\n\n## Acknowledgements\n### FastAPI Fullstack Template \nhttps://github.com/fastapi/full-stack-fastapi-template\n\nThis repository has a heavily modified version of the Full Stack FastAPI Template.\n\n### Key Modifications\n- **MVC Design Pattern**: Improves code organization and scalability for larger projects.\n- **Mangum Integration**: Enables seamless handling of AWS Lambda events, making it easy to deploy FastAPI applications to Lambda.\n- **Custom Dockerfile**: Optimized for building images compatible with AWS Lambda's runtime environment.\n- **Enhanced Security**: Includes examples of protecting endpoints with user and super-user tokens, demonstrating how to secure APIs effectively.\n\n## How To Use It\n\nYou can just fork or clone this repository and use it as is.\n\n✨ It just works. ✨\n\nFollow my step-by-step blog post to deploy this app to AWS Lambda in half an hour. **After following steps 1 - 4 below**.\n\nhttps://www.anuragshenoy.com/blog/fastapi-deployment-using-aws-lambda\n\n### 1. Clone the Repository\n```bash\ngit clone https://github.com/shenoy-anurag/fastapi-aws-lambda-template.git\ncd fastapi-aws-lambda-template\n```\n\n### 2. Configuration\n- Rename the `.env.sample` file to `.env` and replace all variables with your setup.\n  - `mv .env.sample .env`\n  - Replace the placeholder variables in .env with your setup (e.g., database connection string, secret keys).\n- Tip: For the database, if using an external provider, use their transaction-pooler connection string. Long-standing connections are lost whenever lambda spins down.\n\n### 3. For local development\n#### Install dependencies\n- Change directory to backend `cd backend`.\n- If using uv: `uv lock`, then `uv sync`.\n- If using poetry: `poetry install`\n- If using pip: `pip install -r requirements.txt`\n#### Activate the environment\n- If using uv or pip: `source .venv/bin/activate`.\n- If using poetry: `poetry shell`\n#### Run the application locally\n`python main.py`\n\n### 4. Preparing dependencies for Lambda deployment\n- `backend/scripts/aws_lambda_prep.sh` contains the commands to generate your `requirements.txt` file for installing dependencies to the lambda python image. Use the appropriate command depending on whether you are using poetry or uv.\n\n### 5. Deploy to AWS Lambda\nFollow my step-by-step blog post to deploy this app to AWS Lambda in half an hour.\n\nhttps://www.anuragshenoy.com/blog/fastapi-deployment-using-aws-lambda\n\n### Miscellaneous\n#### Generating Secret Keys\nLearn how to generate secrets:\nhttps://www.anuragshenoy.com/blog/generate-secret-keys\n\nAlternatively:\n\n```python -c \"import secrets; print(secrets.token_urlsafe(32))\"```\n\n#### Adding Environment variables to Lambda\n1. Follow the blog post to create a Lambda function.\n2. Deploy the image to the function.\n3. Go to the **Configuration** tab of the Lambda function, and into the **Environment Variables** sidebar menu. Click \"Edit\" and add all the environment variables there.\n\nAlternatively, you can setup AWS Secret Manager (not covered in the blog post or in this readme).\n\n## License\nThe FastAPI AWS Lambda Template is licensed under the terms of the MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshenoy-anurag%2Ffastapi-aws-lambda-template","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshenoy-anurag%2Ffastapi-aws-lambda-template","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshenoy-anurag%2Ffastapi-aws-lambda-template/lists"}