Deploy a Flask app on AWS EC2
Tags: webdev, python, aws, ec2
Published: 2020-04-24 08:38:46

---
title: Deploy a Flask app on AWS EC2
published: true
date: 2020-04-16 22:54:21 UTC
tags: web-development,python,aws,ec2
canonical_url: https://medium.com/innovation-incubator/deploy-a-flask-app-on-aws-ec2-d1d774c275a2
---

![](https://cdn-images-1.medium.com/max/954/0*1x121BdR057EyFav.jpg)<figcaption><a href="https://www.fullstackpython.com/flask.html">Source</a></figcaption>

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/](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](https://docs.aws.amazon.com/efs/latest/ug/gs-step-two-create-efs-resources.html).
- 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](https://docs.aws.amazon.com/efs/latest/ug/gs-step-two-create-efs-resources.html) 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.

![](https://cdn-images-1.medium.com/max/1024/0*GfxWzIlIf9HzXzEO)<figcaption>Inbound Security Rules AWS</figcaption>

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.

![](https://cdn-images-1.medium.com/max/556/1*X_0Klf7_N3N6KpmzLGSSTw.png)

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](http://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>**
```

![](https://cdn-images-1.medium.com/max/791/0*lzlQGThyKBU7x8go)

**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](https://twitter.com/miguelgrinberg) 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](http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world), [#2 Templates](http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-ii-templates), [#3 Web Forms](http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iii-web-forms), [#4 Database](http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iv-database), [#5 User Logins](http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-v-user-logins), [#6 Profile Page and Avatars](http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-vi-profile-page-and-avatars), [#7 Unit Testing](http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-vii-unit-testing), [#8 Followers, Contacts, and Friends](http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-viii-followers-contacts-and-friends), [#9 Pagination](http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-ix-pagination), [#10 Full Text Search](http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-x-full-text-search), [#11 Email Support](http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xi-email-support), [#12 Facelift](http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xii-facelift), [#13 Dates and Times](http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xiii-dates-and-times), [#14 I18n and L10n](http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xiv-i18n-and-l10n), [#15 Ajax](http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xv-ajax), [#16 Debugging, Testing and Profiling](http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvi-debugging-testing-and-profiling), [#17 Deployment on Linux](http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvii-deployment-on-linux-even-on-the-raspberry-pi) and [#18 Deployment on the Heroku Cloud](http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xviii-deployment-on-the-heroku-cloud). Miguel also wrote and recorded numerous [Flask Web Development content including a great book and video](https://flaskbook.com/) book that are excellent resources worth the price, especially to support his continuous revisions to the content.

### **Happy Flasking**

* * *