Introduction
Welcome π to this blog. Did you build a Flask web project but it is running in localhost
or 127.0.0.1
and you want to learn about hosting it in AWS? Then this blog is for you. By the end of your blog, you will be able to know how to deploy your Flask web application to the AWS EC2 instance.
Let's get started
Prerequisites
These are some of the prerequisites for this blog
- Amazon AWS account
- Your favourite web browser.
- A
Flask
web application project repository. (Don't worry if you don't have a project repository feel free to use my project repository, and βοΈ it if you find it useful)
Create an new EC2 instance
Log in to your AWS account. And create a new EC2 instance. If you have any problem while creating the EC2 instance then I recommend you to read my this blog. This blog contains all the things you need to know.
I have created an instance with the name flask-web-app feel free to name it anything you want, OS images will be ubuntu and please remember to choose Free tier eligible selection of the specification if you are looking for the free tiers
.
Connect to AWS EC2 instance
You need to connect to the EC2 instance if you need to work with it, there are different methods present for connecting to the instance. You can go forward with any of the given methods. I am going forward with the SSH
method. Feel free to choose any methods you like the most.
Configuring the instance
-
We first need to update the package of the instance.
$ sudo apt-get update
Installing the requirements
-
Install the
pip
$ sudo apt install python3-pip
-
Install the
Flask
$ pip install -U Flask
-
Install the venv
$ sudo apt install python3.10-venv
Configure the project
-
Clone the Flask repository
$ git clone https://github.com/ankur0904/blog-flask-todo.git
-
change the directory (cd) into the Flask project
$ cd blog-flask-todo
-
Create the Python virtual environment & activate it
python3 -m venv venv $ source venv/bin/activate
-
Installing the dependency via the requirements.txt file
$ pip install -r requirements.txt
Everything is done
- One last step, run the project
bash $ flask run --debug --host=0.0.0.0 --port=5000
Let's discuss the arguments:--debug
-> for running the project in debugging mode--host
-> Host address where our project will run--port
-> the port where our project will run
After executing the commands you will notice that your terminal will look like this -
But if you notice when you go to the URL like pulic-IPv4:5000
(e.g., http://xx.xx.xxx.xxx:5000/) provided then you will get nothing.
You can get the Public IPv4 by selecting the EC2 instance in Insatances page and looking for the IPv4.
The page continues to load. Why???
Because our security group doesn't allow external traffic from the internet. If we want this functionality we need to make changes in the security groups.
Configuring the security groups
- Select the current EC2 instance go to the security section and click on the clickable security group link.
- Then click on edit inbound rules,
- Add the new rule, with custom TCP, port range 5000 web application running on this PORT & 0.0.0.0/0. Now you are good to go & save the rules.
Tada
If you refresh the page. Then you will get this hosted web application.
Remember to stop your EC2 instance.
Please remember to stop your EC2 instance.
Hire me: ankursingh91002@gmail.com
LinkedIn
Twitter
Top comments (0)