Flask is a WSGI based web application framework. Flask has wrapped around Werkzeug and Jinja which are a popular template engine that will help to build advanced dynamic web applications. Running python Flask on local is very simple and easy. But, in order to run it on the enterprise-level platform with all the security standards, we need to deploy Python Flask as production-ready. Most such applications are deployed on the public cloud. Hence, in this article, we will discuss how to deploy python flask on AWS EC2 instance.
Table of Contents
Requirements
Before we get into the setup, we are assuming we have the following things ready in our hand
- AWS Account
- EC2 Instance (Ubuntu/CentOS/Windows)
- MySQL Database
- Python Flask Codebase (https://github.com/PrabhuVignesh/chatbox_flask_app)
Setting up Flask.
Python Flask framework can be installed easily with Python PIP package manager. If you do not have PIP already on your machine, install PIP first. Before that, make sure you have python installed or not. Then, Install PIP. By using PIP, we can install the python flask framework.
Installing on Ubuntu.
- Step 1: Update and Upgrade apt-get.
sudo apt-get upgrade
sudo apt-get update
- Step 2: Install PIP
sudo apt-get install python-pip #python 2
sudo apt-get install python3-pip #python 3
- Step 3: Then, run command: pip install flask-restful
Installing on CentOS
- Step 1: Install the YUM package.
sudo yum install epel-release
- Step 2: Install PIP
sudo yum install python-pip #python 2
sudo yum install python3-pip #python 3
- Step 3: Then, run command: pip install flask-restful
Setting up the Codebase
Once Python Flask is installed, Clone or copy your codebase. In this tutorial, I am cloning the code from Github (https://github.com/PrabhuVignesh/chatbox_flask_app).
If you can see the root directory, we have the requirements.txt file which will have all the dependency packages listed on that. So, to install the requirements, run the following command.
pip install -r requirements.txt
This application does not include any database. So, database Configuration is not required in this tutorial.
App server Setup
In order to make Flask application available over the internet, we need to install mod_wsgi which is an Apache server module to provide WSGI
the interface to host python flask application. To install mod_wsgi
run the following steps.
Step1: Install mod_wsgi
by running the following command.
pip install mod_wsgi
Step 2: Open the Apache virtual host configuration file.
path/to/apache2/sites-available/myapp.conf
This file can be replicated from path/to/apache2/sites-available/000-default.conf
Step 3: Once it is installed, open the virtual host configuration of apache server and add the following
WSGIScriptAlias /myapp Path/to/codebase/myapp/myapp.wsgi
This will add the alias name for the python application.
Step 4: Then, we must give the proper permission to apache to access the codebase directory
<Directory Path/to/codebase /myapp/myapp/>
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Step 5: Enable this virtual host site by running the following command
sudo a2ensite path/to/apache2/sites-available /myapp.conf
Then restart the server by running
sudo apachectl restart
Configuring Network in AWS.
As we know, the Apache server will run in HTTP and HTTPS. So, we need to open ports 80 and 443 for the EC2 instance. So, we need to open the Security Group of the AWS EC2 Instance and either create a new security group or edit the existing security group. Then follow the steps below.
Step 1: Select the EC2 dashboard and click Network & Security > Security Groups
from the left panel
Step 2: Select the security group that is associated with the EC2 instance
Step 3: Select Inbound from the bottom tab and click Edit
Step 4: Click Add Rules and select HTTP in type and Anywhere in the source. Similarly, add another rule and select HTTPS in type and Anywhere in Source. Then save the settings. So, the final configuration will look like as mentioned in screenshot
This will allow all HTTP and HTTPS Inbound requests to the instance which will expose our python flask application to the public.
Finally, Access the python application from the http://EC2_public_IP_address.
Conclusion
So far, we have discussed How to deploy python flask on AWS EC2 instance. In our upcoming article, we will discuss how to configure AWS RDS with flask application and more. Stay tuned and subscribe DigitalVarys for more articles and study materials on DevOps, Agile, DevSecOps and App Development.
Experienced DevSecOps Practitioner, Tech Blogger, Expertise in Designing Solutions in Public and Private Cloud. Opensource Community Contributor.