How to run flask in EC2 instance?

ELAKIA VM
featurepreneur
Published in
6 min readJul 31, 2022

In this article, we will see how to run flask code in the EC2 instance. If you want to know about how to create an EC2 instance you can see this article.

After launching your instance you will land on this page:

Then click the check box near the instance name and then you will see enabling of Connect button, after that click that Connect button then will end up on this page:

To go to the terminal and in there you to go to the location where you have saved your pem-key, then copy-paste the chmod command and run it in the terminal and then copy the example command paste in your terminal:

After pasting the example you get something like this:

Type yes and press enter then you will end like this:

After this, you have to install all the required things for setting up the code and this will be like a brand new computer so you have to set up from the beginning by installing pip, python and flask by creating an environment. Then will get the code from GitHub and clone the repo. But the thing is if the repo is in public can just use HTTPS for cloning the repo, if not, then will have to set up ssh in your instance.

Setting up of SSH key

For setting SSH in an instance, follow the commands below for the set-up:

ssh-keygen -t rsa -b 4096 -C "email@gmail.com"

eval "$(ssh-agent -s)"

ssh-add ~/.ssh/id_rsa

cat ~/.ssh/id_rsa.pub

Change your email address and run the first command, then using the cat command you get the content and copy-paste it and add it to your SSH key in GitHub.

Copy the content and paste into the SSH key in your Github. Go to your Github page and go to the settings.

Then after going to the setting page click SSH and GPG keys there:

Then click on the New SSH key

then copy and in the key box and give a name to the ssh key, then click Add SHH key button.

Then it asks for the GitHub password conformation go ahead and give your password and enter.

Then this is done for the ssh key set up. After that, we will have the code part where you have code in your repositories go there and clone the repo to your instance.

Here is a basic flask application code.

from flask import Flaskapp = Flask(__name__)@app.route('/')def hello_world():    return 'hello world'if __name__== "__main__":     app.run(        host    = "0.0.0.0",        debug   = True,        port    = 3848    )

This is just the basic code of Flask and this is for demo purposes you can or you can use your own code. In the repo, you make have a requirement.txt file.

GitHub link: https://github.com/elakiavm/basic-flask

After this, we have to set up a virtual environment here I have used Miniconda you can have anything Anaconda, virtual box or anything.

Setting up of Miniconda

Follow the commands for setting up Miniconda :

cd /tmpwget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.shchmod +x Miniconda3-latest-Linux-x86_64.sh

Copy-paste the command line by line and run it in your terminal. After copying the last command and pasting it continue pressing enter until you see the question asking for yes/no.

Then type yes after that given enter once, after that you will get like this

Press only one enter, At last, you will get something like this

After that please close the terminal and open it again, there will see that the base is activated so deactivate the base by using the following command

conda config --set auto_activate_base false

After that also close your terminal and open it now you cannot see the base. Then we have to create a conda environment that follows the commands below.

Run this command:
conda create -n py38 -y python=3.8

To activate conda:
conda activate py38

To deactivate:
conda deactivate

Now we have created the conda environment, activate the conda variable and run the flask application

Before that pip install the requirements.txt, If pip is not present then install pip

sudo apt install python3-pip

Or

python3 --versioncurl -O https://bootstrap.pypa.io/get-pip.pypython3 get-pip.py --user

Then after that activate conda py38 and run the installing the requirement.txt file

pip install -r requirement.txt

After that run your application and then add your port number to the security group in the instance

Go to the security group and change the inbound rules by adding the flask port.

Then click the save port button.

Then for checking whether your application is running get the public IP address of the instance and add your port number run in the browser.

Go to the browser first put the IP and followed by the port number

For terminating the instance go to the instance state and click the terminate instance button.

Conclusion

If your repo is public then you can clone using HTTPS or if not use SSH to clone the repo for that setup SSH in the instance and add that ssh to your Github. Clone the code and set up the conda environment for running the code. Change the port in the security group according to the port used in your application.

--

--