How to Run a Flask App on AWS EC2?

Webtunix Blog

AWS (Amazon Web Services) is a secure platform for cloud services, database storage, content delivery and for functionality of business.

Step 1: To create instance

1. First of all go to Amazon Web Services EC2- Simple Cloud Hosting, click on its link and create account for Amazon Cloud Service.
After login, go to console and select EC2 in compute.

Run Flask on AWS

2. Then in EC2 dashboard click on running instances.

Run Flask on AWS

3. To create an instance go to create instance and then click launch instance.

Run Flask on Flask

4. Then first step is to Choose an Amazon Machine Image (AMI). Here you have to choose your system if it is window, Linux or Ubuntu.

Run Flask on AWS

5. After selecting the system 2nd step is Choose Instance Type.  For a single website micro instance size is enough.

Run Flask on AWS

6. Then click next and after all steps are done you have to generate a key pair. Go to side bar i.e left side of your screen and in network and security click key pairs. This key pair is generated once for each instance thus it should be kept safe.

Run Flask on AWSStep 2: To connect instance

1. To connect instance with local PC, select the instance, then right click on it and connect  generated window click.
After this you will get an SSH which is used to connect instance. Copy this SSH and run it on terminal.

Step 3: Setting up instance

1. Now we have connected to the instance then we have to install some programs. For this type following commands:
$ sudo apt-get update
$ sudo apt-get install apache2
$ sudo apt-get install libapache2-mod-wsgi

2. Then install flask using pip tool, which should also be installed first.
$ sudo apt-get install python-pip
$ sudo pip install flask

3. For creating directory for our Flask app.
It creates a directory in home directory to work and link to it from site-root defined in apache's configuration
(/var/www/html by default, see /etc/apache2/sites-enabled/000-default.conf for the current value).
$ mkdir ~/flaskapp
$ sudo ln -sT ~/flaskapp /var/www/html/flaskapp
To verify our operation is working, create a simple index.html file.
$ cd ~/flaskapp
$ echo "Hello World" > index.html
You should now see "Hello World" displayed if you navigate to (your instance public DNS)/flaskapp in your browser.

You can also Check Out:

How to Run a Django App on AWS EC2

To Run a simple Flask app

1. To create an app.
We'll use the simple "Hello world" example from Flask documentation. Put the following content in a file
from flask import Flask
app = Flask(__name__)
def hello_world():
return 'Hello from Flask!'
if __name__ == '__main__':

2. Create a .wsgi file to load the app
Put the following content in a file named flaskapp.wsgi:
import sys
sys.path.insert(0, '/var/www/html/flaskapp')
from flaskapp import app as application

3. Enable mod_wsgi
Apache server displays html pages by default but to serve dynamic content from a Flask app we have to make
some changes. In the apache configuration file located at /etc/apache2/sites-enabled/000-default.conf, add the
following block just after DocumentRoot /var/www/html line:
WSGIDaemonProcess flaskapp threads=5
WSGIScriptAlias / /var/www/html/flaskapp/flaskapp.wsgi
<Directory flaskapp>
WSGIProcessGroup flaskapp
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all

4 . Restart the webserver
Use this command to restart the server with new configuration.
$ sudo apachectl restart

5. Test configuration
If you navigate your browser to your EC2 instance's public DNS again, you should see the text returned by
hello_world function of our app, "Hello from Flask!"
Our server is now running and ready to crunch data and if something isn't working, try checking the log file
in /var/log/apache2/error.log .