This is a microservice template written in Python using the FastAPI framework and deployed in AWS Lambda using Mangum.
The main objective is to provide a template for repositories that can be used as a starting point for new projects. This architecture is based on the Clean Architecture, and it was based in many other projects and books, articles that were mixed by the students of Mauá Institute of Technology, from the academic group Dev. Community Mauá.
First of all, you need to create a repo using issues from Devmaua setup, set the project_name as you prefer and project template as simple_fastapi_template and make sure it's public . Hit create issue and wait for the setup to finish.
After that you need to clone your new repo, create a virtual environment and install the requirements.
python -m venv venv
virtualenv -p python3.9 venv
venv\Scripts\activate
source venv/bin/activate
pip install -r requirements-dev.txt
pip install -r requirements.txt
pytest
uvicorn src.app.main:app
If you want to access the FAST API interface to interact with the API, you can access the following URL:
http://localhost:8000/docs
In order to deploy your microservice in AWS Lambda, you need to follow some rules:
- The routes must be created using FastAPI decorators;
- Don't use complete import, only relative ones. (eg: from .move_function import move);
- ALWAYS test your code before pushing it to the repo. You can use pytest to test your code;
- Don't forget to create your own tests;
- Make sure there is a __init__.py file each directory, otherwise it's not a Python package;
- Every file should be inside the app folder;
After pushing your code to the repo, it will trigger an action to deploy your code in AWS Lambda. You can find the action in the .github/workflows/aws_cd.yml file.
In the first time you push your code, the action will create a new stack in AWS CloudFormation. After that, every time you push your code, the action will update the stack with the new code.
In the Actions tab you can see the status of the deploy, and if it was successful or not. If it was successful, you can find the URL of your API in the outputs tab of the action (in the final part of the "Deploy with CDK" step).
There you can find your API URL, an user and password to access the AWS Console and view the logs of the lambda function to debug it.
To login in the AWS Console, click in the link name "console" on the output, and then click in "Sign in to a different account". There you need to put the account id and the user and password from the outputs tab. On your login you are required to change your password, DON'T FORGET THE NEW ONE. After that you can click in the link to lambda console, and click monitor to find the logs.
After finishing your project, you can delete it from our backend using our CD.
We hope you like and enjoy it! Thanks!
This project was developed to use inside Dev. Community Mauá, but feel free to help!.
- Bruno Vilardi - Brvilardi 👷♂️
- Hector Guerrini - hectorguerrini 🧙♂️
- João Branco - JoaoVitorBranco 😎
- Luigi Trevisan - LuigiTrevisan 🔙
- Vitor Soller - VgsStudio 🌞
If you have any questions, feel free to contact us! You can find us in our Discord server.