This is the API codebase of the To Do App.
- Python (see
.python-versionfor the current version in use) - Django and Django REST Framework
- Pytest
There are two options for setting up and running the development environment locally:
- Python virtual environment (venv)
- Docker container
Install Python (check the version in .python-version)
Go to to-do-api/todo_api/ and create the virtual environment, open it and install dependencies:
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt.venv could be substituted with any path, if you wish to create the virual env elsewhere
You need to have Docker installed so that you can run docker commands.
Go to to-do-api/ directory, where the Dockerfile is, build the Docker image, create and run the container, and run the database migrations, with these commands:
docker build -t todo-api .
docker run -d --name todo-api -p 8000:8000 todo-api
docker exec -it todo-api python manage.py migrateIt's also recommended to create the Django super user for accessing the admin dashboard:
docker exec -it todo-api python manage.py createsuperuserYou will be asked to give a username, email (optional) and password (twice). After creating the super user, you can access the Django admin dashboard in https://localhost:8000/admin/login .
All commands you need to run inside the container should be prefixed with:
docker exec -it todo-apiand you can enter inside the container with this command:
docker exec -it todo-api bashand exit by running exit.
If you use VSCode, open a new terminal, and virtual environment should open automatically.
Run this command to start the development server:
python manage.py runserverTo use a regular terminal, go to the root directory of this project, open the virtual environment and start the server:
source .venv/bin/activate
python manage.py runserverThe project uses Pytest unit tests.
To run the tests, run the following command:
pytestThe project uses a SQLite3 database.
To open it, you need to install SQLite3: see SQLite homepage
Linux installation:
sudo apt install sqlite3If you have SQLite3 installed, run the dbshell command in the virtual env:
python manage.py dbshellSome commands to navigate the DB shell:
.tables # view the list of tables
.help # view the help fileIf you make changes to the data models, you must run a database migration to update the database according to those changes:
python manage.py makemigrations # create the migration files based on the code changes
python manage.py migrate # migrate the database to the latest migrationsYou can view the current state of the migrations by running this:
python manage.py showmigrationsInstall AWS CLI v2
Create IAM user main-admin with the following permissions policies:
- AdministratorAccess
Create an access key and a secret access key for main-admin.
Configure credentials and set the keys and choose a default region (e.g. us-east-1) to the AWS CLI configuration:
aws configureReplace the <region> and <account_id> with the region of your choice and the account id of the AWS account id.
aws ecr create-repository --repository-name todo-api
aws ecr get-login-password --region <region> | docker login --username AWS --password-stdin <account_id>.dkr.ecr.<region>.amazonaws.com
docker build -t todo-api .
docker tag todo-api:latest <account_id>.dkr.ecr.<region>.amazonaws.com/todo-api:latest
docker push <account_id>.dkr.ecr.<region>.amazonaws.com/todo-api:latestDeploy resources with CloudFormation:
aws cloudformation deploy \
--profile main-admin \
--template-file iac.yaml \
--stack-name todo-api-stack \
--capabilities CAPABILITY_NAMED_IAMLook up the EC2 security group named todo-api-sg and a subnet id and insert them in the placeholders <sg_id> and <subnet_id> below, and then run the command:
aws ecs run-task \
--cluster todo-api-cluster \
--launch-type FARGATE \
--task-definition todo-api-task \
--network-configuration "awsvpcConfiguration={subnets=[<subnet_id>],securityGroups=[<sg_id>],assignPublicIp=ENABLED}" \
--overrides '{"containerOverrides":[{"name":"todo-api","command":["python","manage.py","migrate"]}]}'Now the task has a public IP through which you can access the admin dashboard:
http://<PUBLIC_IP>:8000/admin/login
# Delete the CloudFormation Stack
aws cloudformation delete-stack --profile main-admin --stack-name todo-api-stack
# Delete ECR repo
aws ecr delete-repository --profile main-admin --repository todo-api --force