Deploy A Flask App On AWS EC2

Deploy A Flask App On AWS EC2

Flask is an online framework for python, that means that it offers a easy interface for dynamically producing responses to internet requests. Let’s begin by launching a Flask server on an Amazon Net Companies EC2 occasion.

1. Beginning up an EC2 occasion

1. Launch an EC2 occasion.

  1. Login to the AWS console right here
  2. Choose EC2 from AWS providers.
  3. Click on on Launch Occasion.
  4. Choose the Ubuntu Server 16.04 LTS (HVM), SSD Quantity Sort — ami-43a15f3e as an alternative of the Amazon Linux. The precise variations could change with time.
  • Click on on Evaluation and Launch.
  • Configure the safety teams as proven beneath. This setting permits entry to port 80 (HTTP) from anyplace, and ssh entry solely from any IP handle.

2. Establishing the occasion

Now that we’ve linked to the occasion, it’s time to put in a few of the applications we’ll want.
1. Connect with the occasion utilizing ssh from the Terminal.
$ ssh -i "yourapp.pem" [email protected]
2. Set up the apache webserver and mod_wsgi.

$ sudo apt-get replace
$ sudo apt-get set up apache2
$ sudo apt-get set up libapache2-mod-wsgi

For those who level your browser at your occasion’s public DNS identify it is best to see the apache server’s “It works!” web page.
3. Set up Flask utilizing the pip device (which additionally must be put in).

$ sudo apt-get set up python-pip
$ sudo pip set up flask

4. Create a listing for our Flask app.
We’ll create a listing in our residence listing to work in, and hyperlink to it from the site-root outlined in apache’s configuration (/var/www/html by default, see /and many others/apache2/sites-enabled/000-default.conf for the present worth).

$ mkdir ~/flaskapp
$ sudo ln -sT ~/flaskapp /var/www/html/flaskapp

To confirm our operation is working, create a easy index.html file.

$ cd ~/flaskapp
$ echo "Hello World" > index.html

It’s best to now see “Hello World” displayed should you navigate to (your occasion public DNS)/flaskapp in your browser.

READ  Selecting a Internet Host

3. Working a easy Flask app

1. Create an app.
We’ll use the easy “Hello world” instance from the Flask documentation. Put the next content material in a file named

from flask import Flask
app = Flask(__name__)
def hello_world():
  return 'Hiya from Flask!'
if __name__ == '__main__':

2. Create a .wsgi file to load the app.
Put the next content material in a file named flaskapp.wsgi:

import sys
sys.path.insert(0, '/var/www/html/flaskapp')

from flaskapp import app as software

3. Allow mod_wsgi.
The apache server shows html pages by default however to serve dynamic content material from a Flask app we’ll must make a couple of modifications. Within the apache configuration file situated at /and many others/apache2/sites-enabled/000-default.conf, add the next block simply after the DocumentRoot /var/www/html line:


WSGIDaemonProcess flaskapp threads=5
WSGIScriptAlias / /var/www/html/flaskapp/flaskapp.wsgi

    WSGIProcessGroup flaskapp
    WSGIApplicationGroup %{GLOBAL}
    Order deny,permit
    Permit from all

4. Restart the webserver.
Use this command to restart the server with the brand new configuration
$ sudo service apache2 restart
5. Check configuration.
For those who navigate your browser to your EC2 occasion’s public DNS once more, it is best to see the textual content returned by the hello_world perform of our app, “Hello from Flask!”
Our server is now operating and able to crunch some information (if one thing isn’t working, attempt checking the log file in /var/log/apache2/error.log.

4. Have Flask app do work

Now that we now have a server able to do work we’ll arrange a easy service to offer letter counts from an enter string.
1. Acquire info from the url.
Flask permits us to route requests to capabilities primarily based on the url requested. We are able to additionally get enter from the url to go into the perform. Add the next to

def count_me(input_str):
    return input_str

This count_me() perform will return something after the countme/ portion of the url. Restart the webserver to see it in motion:
$ sudo service apache2 restart

2. Course of info
Let’s make our count_me() perform just a little extra fascinating. Modify like so:

from collections import Counter
def count_me(input_str):
    input_counter = Counter(input_str)
    response = []
    for letter, rely in input_counter.most_common():
        response.append('"{}": {}'.format(letter, rely))
    return '
'.be a part of(response)

Restart the server and think about the outcomes:
$ sudo service apache2 restart

Notice that it’s unhealthy follow to format html responses inside these capabilities, templates ought to usually be used as an alternative.
Your complete count_me() perform might be written in a single logical line:

return '
'.be a part of('"{}": {}'.format(let, cnt) for let, cnt in Counter(in_str).most_common())

If the appliance throws any errors the error log may help you debug.
$ sudo tail -f /var/log/apache2/error.log

For those who loved this tutorial, plase give me some likes. Blissful coding!

Leave a Reply

Your email address will not be published. Required fields are marked *