DEV Community

Cover image for From Localhost to the Cloud: A Guide to deploy Flask web application on AWS EC2
Ankur Singh
Ankur Singh

Posted on • Updated on

From Localhost to the Cloud: A Guide to deploy Flask web application on AWS EC2

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.

Create an new EC2 instance-img

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.

Connect to AWS EC2 instance

Configuring the instance

  1. We first need to update the package of the instance.

    $ sudo apt-get update
    
  2. 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

  1. Clone the Flask repository

    $ git clone https://github.com/ankur0904/blog-flask-todo.git
    
  2. change the directory (cd) into the Flask project

    $ cd blog-flask-todo
    
  3. Create the Python virtual environment & activate it

    python3 -m venv venv
    $ source venv/bin/activate
    
  4. Installing the dependency via the requirements.txt file

    $ pip install -r requirements.txt
    

Everything is done

  1. 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 -

configure-project-image

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.

configure-security-group-first-image

  • Then click on edit inbound rules,

configure-security-group-second-image

  • 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.

configure-security-group-third-image

Tada

If you refresh the page. Then you will get this hosted web application.

hosted-web-application-image

Remember to stop your EC2 instance.

Please remember to stop your EC2 instance.

Hire me: ankursingh91002@gmail.com
LinkedIn
Twitter

Top comments (0)