Skip to content
This repository has been archived by the owner on Aug 6, 2024. It is now read-only.

Maua-Dev/battlesnake_lucascalcada

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

25 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

battlesnake_fastapi_template 🐍

This is a Battlesnake template written in Python using the FastAPI framework and deployed in AWS Lambda using Mangum.

Introduction and Objectives ⁉

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.

Example

How to use πŸ€”

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.

Installation πŸ‘©β€πŸ’»

Create virtual ambient in python (only first time)

Windows
python -m venv venv
Linux
virtualenv -p python3.9 venv

Activate the venv

Windows:
venv\Scripts\activate
Linux:
source venv/bin/activate

Install the requirements

pip install -r requirements-dev.txt
pip install -r requirements.txt

Run the tests

pytest

Run the server local

uvicorn src.app.main:app

The Challenge 🐍

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 files πŸ“

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 πŸ›£

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.

Atention 🚨

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;

Deploy πŸš€

FastApi AWS drawio

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).

Action Tab CD 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.

Outputs

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.

Lambda Console Cloudwatch Logs

After finishing your project, you can delete it from our backend using our CD.

AwsDestroy

Useful tools πŸ› 

Thanks πŸ‘’πŸΏ

We hope you like and enjoy it! Thanks!

Contributors πŸ’°πŸ€πŸ’°

This project was developed to use inside Dev. Community MauΓ‘, but feel free to help!.

Contact us πŸ“ž

If you have any questions, feel free to contact us! You can find us in our Discord server.

Releases

No releases published

Packages

No packages published