This is a Battlesnake template written in Python using the FastAPI framework and deployed in AWS Lambda using Mangum.
The main purpose of this project is to create a template for Battlesnake using FastAPI and Mangum. The biggest challenge is to understand how an API works and how to deploy it in AWS Lambda.
First of all, you need to create a repo using issues from Devmaua setup, set the project_name as "battlesnake_{your name}" and project template as battlesnake_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
The challenge is to create a Battlesnake using FastAPI and Mangum. The Battlesnake must be deployed in AWS Lambda. You can find the documentation for Battlesnake here.
The project is divided in 2 folders: src and tests. In src you can find the main.py file, which is the file that contains the FastAPI app and the routes. From there you can create your own routes and functions. The tests folder contains the tests for the project. You can create your own tests and run them using pytest.
The routes are created in main.py file. You can create your own routes and functions. The routes are created using FastAPI decorators, you can find the documentation here. Follow the rules from Battlesnake documentation to create your routes, they should look like this.
In order to deploy your Battlesnake 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. You can use this URL to create your Battlesnake in the Battlesnake website. You can find the documentation here. You can also find 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.
- Postman - API development environment
- FastAPI - Web framework
- Python3.9 - Python Documentation
- Battlesnake - Battlesnake Documentation
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.