How to update code of Python Flask App to AWS EC2 from local
Published in
1 min readNov 15, 2023
After updating your code locally, you need to deploy the new version to AWS EC2.
Here is the handbook for uploading the code for the first time:
Uploading updated code is similar, but easier.
First, get the requirements.txt file:
pip freeze > requirements.txt
Then, open the terminal, and update all the files except the ones you don’t want:
rsync -av --exclude='.git/' --exclude='.DS_Store' --exclude='venv_winjob' --exclude='__pycache__' --exclude='.gitattributes' --exclude='.gitignore' --exclude='.env' -e "ssh -i /Users/henrywu/MyDrive/98_Products/P01-Project-Alpha/03-keys/AWS_EC2_key_pair_helloworld.pem" /Users/henrywu/MyDrive/99_Coding/01-Github/winjob/ ec2-user@ec2-44-239-28-119.us-west-2.compute.amazonaws.com:/home/ec2-user/firstweb/
Remember, the above command is from your local end.
Next, login SSH and install requirements.txt:
ssh ec2helloworld
Delete the former venv
sudo rm -rf venv_winjob
Create a new one:
python3 -m venv venv_winjob
Install libraries:
source /home/ec2-user/firstweb/venv_winjob/bin/activate
pip install -r /home/ec2-user/firstweb/requirements.txt
Test:
cd ~/firstweb
gunicorn --workers 3 --bind 0.0.0.0:5000 app:app
If the web is available, then restart config files:
sudo systemctl restart firstweb.service