watch the demo on youtube
note: this repository assumes your local dev environment is unix-based (macOS, WSL2, linux) with python3, git and docker desktop configured
-
build the docker containers:
docker compose build
-
run the containers in detached mode:
docker compose up -d
-
apply database migrations:
docker compose exec api alembic upgrade head
-
visit
http://localhost:8000/docs
to create and get movies -
stop the containers:
docker compose down
note: the application must be up and running before using the following commands. you will always want to create your database migrations locally so that it can be checked into source code and versioned with deployments.
-
create a new migration:
docker compose exec api alembic revision --autogenerate -m "your comment here"
-
apply migrations:
docker compose exec api alembic upgrade head
-
revert a migration:
docker compose exec api alembic downgrade -1
note: this section assumes you have the following cloud infrastructure provisioned:
- elastic container registry
- application load balancer
- application load balancer security group
- application load balancer target group
- postgresql relational database instance
- relational database security group
- elastic container task definition
- elastic container cluster
- elastic container service within cluster
- elastic container cluster ssh key
-
locate your
.pem
ssh key file that was downloaded when creating the ecs cluster -
open the file with a basic text editor (vim, textEdit, notepad++, etc.)
-
copy the contents of the file
-
in your terminal, navigate to the hidden ssh directory and create a new file with the same name as your ssh key file:
cd ~/.ssh # navigate to ssh directory vim your_key_name.pem
-
once you're in the vim editor, hit
i
on your keyboard to begin typing, you'll seeINSERT
in the bottom left corner of your editor. paste in the contents from the downloaded ssh key file. -
to save and quit, hit
esc
, followed by:wq
. The colon and wq should appear in the bottom left corner of your editor where you saw theINSERT
statement from earlier. Finally, hit theenter
and you should return to your terminal in the.ssh
directory. -
change the permissions on the file you just created to enable the file's usage when running ssh commands:
chmod 400 your_key_name.pem
-
finally, obtain a list of the ec2 instances (servers) that are available for your container to run on. if you followed my youtube tutorial, there should be 4 instances. clicking on each instance should present a
connect
button in the aws console. once you've clicked theconnect
button, navigate to theSSH client
tab to see the command forconnect[ing] to your instance using its Public DNS
. We'll modify this command so that we can call it outside of.ssh
directory, like if we wanted to use it in a script:ssh -i "~/.ssh/your_key_name.pem" [email protected]
-
run the modified ssh command for each of your instances. the first time you run the command, you will be prompted to accept the connection. type
yes
and hit enter. to exit the ssh connection in your ec2 instance usectrl
andc
at the same time on your keyboard.
note: this section assumes you have the AWS CLI v2 installed and configured and the following cloud infrastructure provisioned:
- elastic container registry
- application load balancer
- application load balancer security group
- application load balancer target group
- postgresql relational database instance
- relational database security group
- elastic container task definition
- elastic container cluster
- elastic container service within cluster
- elastic container cluster ssh key
if you have not deployed your container image to the elastic container registry, comment out line 23 of the deploy.sh script since you likely do not have an ecs service
-
visit
project/alembic.ini
and update line 53 w/ your production database URI (change this back after deploying so you don't commit your prod password to github):sqlalchemy.url = postgresql+asyncpg://db_username:db_password@rds_endpoint:5432/inital_db_name
-
visit
deploy.sh
and update the variables to match your credentials:# variables aws_region="us-east-1" aws_account_id="2893821284" aws_ecr_name="api" aws_cluster_name="api-cluster" aws_service_name="api-sv"
-
execute the script to build new images and update the ecs service:
bash deploy.sh
-
if you have database migrations to apply, you'll need to ssh into one of your ec2 instances:
ssh -i "~/.ssh/your_key_name.pem" [email protected]
-
then list the running containers on the server:
docker ps
-
if you don't see your fastapi container running, no worries. just quit the ssh session and try with one of the other instances you provisioned. you can also go into the aws console, find the running task in ecs, and find the linked ec2 instance it's running on.
-
once you see your container running on the server, copy the container id and bash into the container:
docker exec -it your_container_id bash
-
finally, run the migration command:
alembic upgrade head