Streamline Your Flask Deployment Process on EC2 with These Simple Steps

Bagiyalakshmi
featurepreneur
Published in
3 min readMar 13, 2023

Before Starting with EC2 Deployment, you must have created an EC2 instance and it should be running status.

To create an EC2 instance refer: https://medium.com/featurepreneur/creating-an-ec2-instance-on-aws-everything-you-need-to-know-630c5eb92f1f

  1. Connect with the remote machine.

2. It’s time to install some basics to proceed

  • To install miniconda
cd /tmp
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
chmod +x Miniconda3-latest-Linux-x86_64.sh
./Miniconda3-latest-Linux-x86_64.sh

Once you’re done with the above steps, exit from EC2 and reconnect again. Then type in the following command:

conda config --set auto_activate_base false
  • Create conda environment
conda create -n py39 -y python=3.9
  • To activate and deactivate the environment, type in the commands below:
conda activate py38
conda deactivate
  • Install git and copy your ssh key and paste it in your GitHub account
sudo apt install git
ssh-keygen -t rsa -b 4096 -C "youremail@gmail.com"
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa
cat ~/.ssh/id_rsa.pub

Once you’re done with the above commands, copy the SSH key generated and add it to GitHub.

To do so, go to settings on GitHub, then you will find SSH and GPG keys on the left-hand side panel. Go into it and add in the SSH key generated and name it.

3. This is enough to deploy a simple-flask

4. git clone the repository (here we use: https://github.com/BagiyaLakshmi/simple-flask)

4. Navigate to the repo cloned, conda activate the environment, and install the requirements

pip install -r requirements.txt

5. Now, it’s time to work on the console

  • Go inside the EC2 console-> security -> click the security group -> edit inbound rule -> add the port in which the flask app is running.

6. Now in the terminal run

python app.py

7. open a browser, paste

<public_ip>:<port_number>

eg:

34.56.78.119:3011

The public IP of the instance is found in the EC2 instance summary in the console

8. To stop the flask app from the server, just kill the port

sudo kill -9 'sudo lsof -t -i:<port_number>'

eg : sudo kill -9 ‘sudo lsof -t -i:3011’

In conclusion, deploying a Flask application on EC2 can seem intimidating at first, but with the right guidance, it can be a straightforward and rewarding process. Amazon EC2 provides a scalable and reliable cloud computing environment that can easily host Flask applications. By following the steps outlined in this article, you can deploy your Flask application on EC2 and take advantage of the many benefits of cloud computing, including scalability, cost-effectiveness, and high availability.

Hope you enjoyed it!!

Happy learning!!

--

--

Bagiyalakshmi
featurepreneur

Learning something new everyday keeps me busy and refresh