From Localhost to the Cloud: A Guide to deploy Flask web application on AWS EC2
Tags: aws, tutorial, webdev, python
Published: 2024-01-31 18:38:54

## 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](https://github.com/ankur0904/blog-flask-todo), 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 ](https://dev.to/ankur0904/step-by-step-guide-to-setting-up-your-first-aws-ec2-instance-1ph5) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3zaecoknmm1tb0tnxq0p.png)

## 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 ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/78sh2gd49x3xp2ufq6vc.png)

## Configuring the instance

1. We first need to update the package of the instance.
```bash
$ sudo apt-get update
```

2. Installing the requirements

  - Install the `pip` 
     ```bash
     $ sudo apt install python3-pip
     ```

  - Install the `Flask`
     ```bash
     $ pip install -U Flask
     ```
  
  - Install the venv
    ```bash
    $ sudo apt install python3.10-venv
    ```


## Configure the project 

1. Clone the Flask repository
```bash
$ git clone https://github.com/ankur0904/blog-flask-todo.git
```

2. change the directory (cd) into the Flask project
```bash
$ cd blog-flask-todo
```

3. Create the Python virtual environment & activate it 
```bash
python3 -m venv venv
$ source venv/bin/activate
```

4. Installing the dependency via the requirements.txt file
```bash
$ pip install -r requirements.txt
```

**Everything is done** 
5. 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yypwjlf4d3isqf9ttwyv.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j4peexfavqt9xt4rfr0j.png)

- Then click on edit inbound rules, 

![configure-security-group-second-image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cko3irp14185y7zov7sg.png)

- 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y632nd0s6gzehdiq9zzh.png)

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

![hosted-web-application-image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/glcj69avkazbi930jldu.png)

## Remember to stop your EC2 instance.
Please remember to stop your EC2 instance.

Hire me: ankursingh91002@gmail.com 
            [LinkedIn](https://www.linkedin.com/in/ankur-singh-161458227/)
            [Twitter](https://twitter.com/ankursingh91002)