Deploying a Flask App to AWS Elastic Beanstalk

Marvin Davila
Analytics Vidhya
Published in
6 min readDec 11, 2019

A Simple Tutorial (Using Python and MacOS) for AWS Beginners

Photo by NASA on Unsplash

If you’re just getting started with AWS services, hopefully you’ll find this helpful! There are few errors I ran into when I started working with AWS. I’m providing solutions for each error. Now you don’t have to struggle and waste time.

What is Flask?

Flask is a web framework written in python. It provides you with tools, libraries, and technologies that allow you to build a web application. This web application can be some web pages, a blog, a wiki or go as big as a web-based calendar application or a commercial website.

What is AWS Elastic Beanstalk?

AWS Elastic Beanstalk is a service for deploying applications which performs various AWS services, including EC2, S3, Simple Notification Service, CloudWatch, autoscaling, and Elastic Load Balancers.

With Elastic Beanstalk, you can quickly deploy and manage applications in the AWS Cloud without worrying about the infrastructure that runs those applications. It reduces management complexity without restricting choice or control. You simply upload your application, and Elastic Beanstalk automatically handles the details of capacity provisioning, load balancing, scaling, and application health monitoring.

What is covered in this article:

  • How to create a AWS Elastic Beanstalk environment
  • How to create a simple Flask App
  • How to upload your Flask App on AWS Elastic Beanstalk
  • How to debug errors

Creating the Application

Go to AWS Elastic Beanstalk and click create a new application on the top right.

Click create after giving your application a name and short description. The name can be anything.

Creating Environment

Click create one now to create environment in your tutorial application.

Now you have to select an environment tier. For this tutorial we will use a web server environment.

Environment Information

  • Give your environment a name
  • Choose a unique name for the Domain
  • Write a short description

Base Configuration

  • For this tutorial we are using python for our preconfigured platform
  • We will upload the flask app later. For application code keep sample application selected.

Click Create environment. It should take a couple of minutes until it’s ready.

Create a Simple Flask App

If you want to skip this part, you can download this Flask Zip File which contains all the files needed for the flask app and you can go directly to the Uploading a Flask App section :)

Create a new file called application.py and paste the code below into it.

from flask import Flaskapplication = Flask(__name__)

@application.route("/")
def index():
return "Your Flask App Works!"

@application.route("/hello")
def hello():
return "Hello World!"


if __name__ == "__main__":
application.run(port=5000, debug=True)

Create a new file called requirements.txt and paste the app dependencies below into it.

Click==7.0
Flask==1.1.1
itsdangerous==1.1.0
Jinja2==2.10.3
MarkupSafe==1.1.1
Werkzeug==0.16.0

Create a folder named .ebextensions.
Copy the code below and save it as a new file called app.config in the folder .ebextensions.

files:
"/etc/httpd/conf.d/wsgi_custom.conf":
mode: "000644"
owner: root
group: root
content: WSGIApplicationGroup %{GLOBAL}

Note: The code block above was used in my project because I was having a time out error on Elastic Beanstalk. You might not need to use this solution for your deployment, but if you ever have a similar error this can help. Here is some documentation on WSGIApplicationGroup.

Now you will need to zip all the files together(including hidden folder) to upload the flask app on Elastic Beanstalk. To view hidden files in your finder press cmd+shift+.. Name the zip file flask_app.zip or any name you want to give it.

Uploading a Flask App to Elastic Beanstalk

You should now have the flask app in a zip file with the following folder structure:

- application.py
- requirements.txt
- /.ebextensions
- app.config

Sometimes when you create the zip file it will include hidden files that are not needed. This usually will cause an error when you try to upload on Elastic Beanstalk. In order to fix this follow the next steps.

Open terminal and go to where the zip file is located.
Note: This may not be in the same location where your zip file is saved. You can find your zip file and drag it to your Documents folder to follow along.
Example:

cd Documents

Run this line below:
replace flask_app.zip with the name of your zip file if needed

zip -d flask_app.zip __MACOSX/\*

Note: This picture is for my project flask app zip file. An example of what it can look like. You shouldn’t get the same result if you are following this tutorial.

This will clean out any extra hidden files that is included in you zip file. They are not needed to upload on Elastic Beanstalk and it might cause deployment to fail. Sometimes you won’t have any hidden files.

Note: I run the line of code again and it returns a zip error. That’s good!

Now we are ready to upload our zip file to Elastic Beanstalk. Go back to your elastic beanstalk environment dashboard and click on Upload and Deploy.

Choose your zip file and click deploy. Then wait until your health symbol is a green checkmark and click on your website link above the health symbol.

If you completed all these steps successfully, your website link should take you to a page saying Your Flask App Works! :)

How to view error logs in your environment?

For this tutorial you shouldn’t be getting any errors when deploying the simple flask app, but it is good to know how to debug.

Follow Steps Below to view Error Logs

On the left side in your Elastic Beanstalk environment dashboard click Logs

Then click on Request Logs and select Last 100 Lines for the most recent errors.

Click Download and it should take you to a page where you can view the last 100 lines.

Below is an example error log page. Make sure you are looking at the most recent time when your application was executed.

Now you have access to your error logs and will be able to find a solution.

Tutorial is over! Now your flask app can be set up on AWS Elastic Beanstalk. If I had access to this article, it would have save me so much time when I was working on my project.

This article will eventually become part of a series of articles from lessons I learned with my team while working on a project called Cryptolytic (article will be written about this soon and linked here). It’s the guide we wish we had when working on the project.

The notebook containing all the code used in this article can be found here which is inside the repo of our Cryptolytic project — so if you’re curious, check it out!

Find me on Twitter @malexmad, Github, or connect with me on LinkedIn!

--

--