How To Deploy Flask APP On AWS EC2 Instance? (original) (raw)

Last Updated : 06 Jan, 2025

In this article, we will study how we can deploy our existing Flask application in AWS EC2. We will also see how to use the public IP of the EC2 instance to access the flask application. For this article, you should know about setting up EC2 in AWS. So, let's begin with the deployment.

Primary Terminologies:

Prerequisites:

Steps to Deploy Flask Application in AWS EC2 with Ubuntu Server

Step 1: Create an EC2 instance allowing HTTP traffic.

Ubuntu OS

configure ec2

Step 2: Connect to instance

ssh -i username@

Step 3: Install Python Setup the enviroment.

sudo apt install python3

Step 4: Install Flask

pip install flask

Screenshot-(227)

Step 5: Create simple flask application.

nano hello.py

from flask import Flask
app = Flask(name)

@app.route("/")
def hello():
return "Hello World!"

if name == "main":
app.run()

python hello.py

Server

Step 6: Test the application locally.

curl http://127.0.0.1:5000

**Step 7: Install and configure nginx as reverse proxy.

sudo apt install nginx

Nginx

Step 8: Configure Reverse proxy.

Proxy

server {
listen 80;
listen [::]:80;
server_name ;

location / {  
    proxy_pass http://127.0.0.1:5000;  
    include proxy_params;  
}  

}

sudo systemctl restart nginx
sudo systemctl status nginx

Nginx Status

Step 9: Test your application.

Verification

Home Page

Conclusion:

We have successfully deployed our flask application on EC2 instance in AWS. Deploying application on EC2 makes it easier for web applications deployment. EC2 instances can be configured for more secure and upgraded web application servers.

Troubleshooting