Open In App

How To Deploy Flask APP On AWS EC2 Instance?

Last Updated : 01 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  • Flask: A micro web framework for Python used to build web applications. Flask is known for its simplicity and ease of use.
  • AWS (Amazon Web Services): A cloud computing platform that provides a wide range of services, including computing power, storage, and databases, allowing users to deploy and scale applications.
  • EC2 Instance: Elastic Compute Cloud (EC2) instances are virtual servers in the AWS cloud. They can be used to run applications and host websites.

Prerequisites:

  • An AWS account with EC2 access.
  • An EC2 instance allows HTTP traffic (port 80/443).
  • A Flask application that you want to deploy.

Steps to Deploy Flask Application in AWS EC2 with Ubuntu Server

Step 1: Create an EC2 instance allowing HTTP traffic.

  • First, we have to create an Amazon EC2 instance running any Linux distribution of your choice.
  • Under Launch Instance on AWS, give your instance a name and select Ubuntu Server as the OS.
  • Specify other options as per your choice.

Ubuntu OS

  • Make sure you allow HTTP traffic to the instance. You can also allow HTTPS if you want.

configure ec2

  • After specifying all options correctly click launch instance.

Step 2: Connect to instance

  • Once the instance is started successfully copy the public IP address assigned you will need it later. You may have assigned an elastic ip address which is also fine.
  • Connect to the instance using PuTTY or OpenSSH.
ssh -i <key-file-name> username@<public-ip-address>
  • Once you connect to the instance we can start next steps.

Step 3: Install Python Setup the enviroment.

  • Download and install Python and other Flask requirements according to your project. Ubuntu may have already installed Python. If it is not there install using the below command.
sudo apt install python3

Step 4: Install Flask

  • After installing Python install Flask using below command.
pip install flask

Screenshot-(227)

Step 5: Create simple flask application.

  • Now add your project to the instance or you can create a new flask project.
  • For this article we will setup a simple flask project. Create a hello.py file in your project folder in your favorite editor and paste a simple flask code as below.
nano hello.py
from flask import Flask
app = Flask(__name__)

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

if __name__ == "__main__":
app.run()
  • Save and exit the file. Then run the file to start flask server.
python hello.py
  • Send the server to background to keep it running.

Server

Step 6: Test the application locally.

  • Curl the server to view the output. You should see “Hello World” as response.
curl http://127.0.0.1:5000

Step 7: Install and configure nginx as reverse proxy.

  • Install the nginx server on instance using below command.
sudo apt install nginx
  • Check the server is installed and running by running curl to instance public Ip address. You should see nginx landing page.

Nginx

Step 8: Configure Reverse proxy.

  • Now go to /etc/nginx/sites-enabled. Inside this folder create a file with name as your Instance public IP address.

Proxy

  • Paste the server configuration in this file for reverse proxy. the content will look like below.
server {
listen 80;
listen [::]:80;
server_name <YOUR INSTANCE IP>;

location / {
proxy_pass http://127.0.0.1:5000;
include proxy_params;
}
}
  • save and close the file .
  • restart nginx server. After restart check the status of server which should be running.
sudo systemctl restart nginx
sudo systemctl status nginx

Nginx Status

Step 9: Test your application.

  • Once the server is restarted you can curl to instance Ip and see “Hello World”

Verification

  • You can also paste public IP in browser to see the response.

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

  • If you can’t access the landing page, make sure your Nginx server is allowed on the firewall. If not add Nginx to the allowed apps in the firewall.
  • If you get an access denied error, make sure HTTP access is enabled for the EC2 instance.

How to Deploy Flask APP on AWS EC2 Instance – FAQ’s

Can I automate the deployment process for my Flask app on EC2?

Yes, you can use tools like Ansible, Fabric, or create deployment scripts to automate the deployment process. Automation simplifies repetitive tasks and helps maintain consistency.

How do I deploy a Flask app with a database on EC2?

Ensure your database is accessible from your EC2 instance. Update your Flask app configuration to use the correct database connection details. Install the necessary database driver for your chosen database system.

What is Gunicorn, and why should I use it with Flask on EC2?

Gunicorn is a WSGI server that can serve Flask applications. It’s recommended for production deployments due to its performance and ability to handle concurrent requests. Gunicorn is often used in conjunction with Nginx or Apache.

How do I configure Nginx/Apache to serve my Flask app?

Create a virtual host configuration file for your domain in Nginx or Apache. Configure the server to forward requests to the Gunicorn server where your Flask app is running.

How can I ensure my Flask app runs continuously on the EC2 instance, even after I log out?

You can use tools like tmux or screen to run your Flask app in a detached session. Alternatively, consider using a process manager like systemd to manage your Flask app as a service.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads