DEV Community

NAEEM HADIQ for Innovation Incubator

Posted on • Originally published at Medium on

Deploy a Flask app on AWS EC2

Source

Flask is a web framework for python, meaning that it provides a simple interface for dynamically generating responses to web requests. Which includes serving websites/API Responses. Going Cloud being the latest Trend. Let’s start by launching a Flask server on an Amazon Web Services EC2 instance.

Lets Split the Task of Setting up a flask server on EC2 as 3

1. Starting up an EC2 instance

1)Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

2)Choose Launch Instance.

3)In Step 1: Choose an Amazon Machine Image (AMI), find an Ubuntu AMI on the list and choose Select.

4)In Step 2: Choose an Instance Type , choose Next: Configure Instance Details- Choose one that best suits your requirement.

5)In Step 3: Configure Instance Details , provide the following information:

  • For Network , choose the entry for the same VPC that you noted when you created your EFS file system in Step 1: Create Your Amazon EFS File System.
  • For Subnet , choose a default subnet in any Availability Zone.
  • For File systems , make sure that the EFS file system that you created in Step 1: Create Your Amazon EFS File System is selected. The path shown next to the file system ID is the mount point that the EC2 instance will use, which you can change. Choose Add to user data to mount the file system when the EC2 is launched.
  • Under Advanced Details , confirm that the user data is present in User data.

6)Choose Next: Add Storage.

7)Choose Next: Add Tags.- Tag it appropriately for future reference

8)choose Next: Configure Security Group

Configure the security groups as shown below. This setting allows access to port 80 (HTTP) from anywhere You can also add port 443(HTTPS)if required, and ssh access only from any IP address.

Inbound Security Rules AWS

Now that you have Your instance set up let's connect to it and install the necessary requirements.

2)Connect to your EC2 and Setup Requirments

1)Connect to your EC2

Connect via an SSH Terminal

$ ssh -i “yourapp.pem” ubuntu@yourapppublicinstance.compute-1.amazonaws.com

or

Connect via the browser interface

2)Update Ubuntu and install the apache webserver and mod_wsgi.

$ sudo apt-get update 

$ sudo apt-get install apache2 

$ sudo apt-get install libapache2-mod-wsgi

Now once this is done if you hit the public ip of your ec2 in any browser you should be able to see this.

Now that Apache server is running lets set up pip and other dependencies for a flask server

$ sudo apt-get **install** python3
$ sudo apt-get **install** python3-pip 
$ sudo pip **install** flask

Python 3 most likely would be installed but the command ensures it exists and is in the correct version.

2)Create a Directory for the flask app

We’ll create a directory in our home directory to work in, and link to it from the site-root defined in apache’s configuration. Usually at

/var/www/html

Alternatively, it can be changed or checked at

/etc/apache2/sites-enabled/000-default.conf

Ensure we are on the root directory and

$ cd ~
$ 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 "Hola" > index.html

Now the publicip/flaskapp

would display the Hola screen

2)Setting up the Flask App and Deploying

1)Create an app.

We’ll use the simple “Hello world” example from the Flask documentation. Put the following content in a file named flaskapp.py

**from** flask **import** Flask app = Flask(\_\_name\_\_)
**@app.route('/')** 
**def**  **hello\_world** ():
**return**'Hello from Flask!' 
**if** \_\_name\_\_ == '\_\_main\_\_':
   app.run()

2. Create a .wsgi file to load the app.

Lets do a basic wsgi code, put the following content onto a file named flaskapp.wsgi

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

**from** flaskapp **import** app **as** application

3. Enable mod_wsgi.

The apache server displays html pages by default but to serve dynamic content from a Flask app we’ll have to make a few changes. In the apache configuration file located at /etc/apache2/sites-enabled/000-default.conf , add the following block just after the 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 
**</Directory>**

4. Restart the webserver.

Use this command to restart the server with the new configuration

$ sudo service apache2 restart

Voila! With this, Our flask Server is now running.

Play around with Flask and Python and implement anything your mind thinks about.

Best to learn Flask

The Flask mega tutorial by Miguel Grinberg is a perfect starting resource for using this web framework. Each post focuses on a single topic and builds on previous posts. The series includes 18 parts: #1 Hello World, #2 Templates, #3 Web Forms, #4 Database, #5 User Logins, #6 Profile Page and Avatars, #7 Unit Testing, #8 Followers, Contacts, and Friends, #9 Pagination, #10 Full Text Search, #11 Email Support, #12 Facelift, #13 Dates and Times, #14 I18n and L10n, #15 Ajax, #16 Debugging, Testing and Profiling, #17 Deployment on Linux and #18 Deployment on the Heroku Cloud. Miguel also wrote and recorded numerous Flask Web Development content including a great book and video book that are excellent resources worth the price, especially to support his continuous revisions to the content.

Happy Flasking


Top comments (0)