How to deploy a Python Flask app to AWS Elastic Beanstalk

Andrey
Seamless Cloud
Published in
5 min readJul 21, 2020

--

Welcome to the first part of the series of posts where I will guide you through the steps for creating a modern web application in Python and deploying it to the cloud using Elastic Beanstalk. By the end of this article, you will have a “Hello World” application available on the internet.

Pre-requisites

  • Basic Python knowledge.

What is this blog post series about?

You’ll find this series interesting if you want to create an app in Python that other people on the internet can use. This is actually not as trivial as it may seem. There is a lot of stuff you need to do. If you’ve never deployed a production-grade web application, I strongly suggest you read this series in the consecutive order starting with this post. If you’re an experienced engineer, feel free to review the table of contents and jump to the post that interests you the most.

All posts in this series

Let’s jump right into it!

Basic Flask Application

Before we make our application available on the internet, let’s make sure we have it working locally. For the purposes of this article, we will need a simple web application, and the simplest way to make it in Python is Flask. If you don’t have it installed, please follow the instructions in their documentation.

For the purposes of this tutorial, we don’t really need a fancy application, a simple “Hello World” would do. Let’s just copy and paste a Minimal Flask Application code. Let’s create a file `application.py` and put the following code into it:

from flask import Flask
application = Flask(__name__)
@application.route('/')
def hello_world():
return 'Hello, World!'

Please note that the instance of Flask object is called `application` instead of `app` as in Flask documentation. This is actually important for the next step in this article. The default setting of AWS Elastic Beanstalk is to look into the file named `application.py` for the `application` variable. You can override this behavior later if you want.

Now you need to run the following in your terminal:

export FLASK_APP=application.py && flask run

To check that it worked, please open the http://127.0.0.1:5000/ URL in your browser. You should see this:

That’s not the next big thing in the web applications world, but this is enough for us at the moment. Let’s deploy it somewhere, so it’s not just on our local machine.

AWS Elastic Beanstalk

There are hundreds of different services to host your application. AWS Elastic Beanstalk is a decent choice for the following reasons:

  1. AWS infrastructure has everything in the world. You can probably spend your whole life learning all their services. As you develop your application, you may have different needs at different times, and chances are high AWS already can satisfy them.
  2. Easy to get started. I will prove it by the end of this tutorial.
  3. Scalable. We will get more into scalability later in this blog post series.

Setting up an account in AWS

First things first, let’s create an account in AWS. You can do it here -> https://aws.amazon.com/free/. If you already have an account in AWS — that’s awesome, you can use it.

If you’re new to AWS, you’ll get access to a free tier account for the first year. But you need to enter your credit card information to create an account. Throughout this tutorial, we’re going to use mostly free resources. However, I cannot guarantee that at the time you’re reading this article, AWS pricing is the same. Please be careful with settings in AWS and pay attention to the services you enable.

Creating your first Elastic Beanstalk Application

Now, let’s sign-in into the AWS console -> https://aws.amazon.com/console/. At the top left corner of the screen, click on “Services,” type “Elastic Beanstalk” in the search bar, and click on the first result.

Now click “Create Application.”

You should see a page with basic settings for your application. We only need to pick an application name and to choose a platform. In the last section (“Application code”), let’s select the option “Upload your code”. This is how my page looks so far.

The only thing left is to upload our python code. So, we will do it manually right now, but later in the series, you’ll get to know how to automate your deployment pipeline. For now, let’s go to our python code and create a zip archive.

Let’s upload the file and hit “Create Application.”

Now we sit and watch Elastic Beanstalk do its magic. It’s going to provision servers and create a bunch of stuff for us under the hood.

After a few minutes, hopefully, you see this:

There is a link under the application name. If you follow it, you should see the “Hello World” message. Congratulations, you have successfully made your Python application available on the internet using the AWS Elastic Beanstalk.

If you have any issues, please shoot me an email, and I’ll try to help you. The full code is available here.

In “Part 2: Automated deployment to AWS Elastic Beanstalk using Github Actions” (writing is in progress) we will automate the deployment of our application.

You can read more of our blog here.

Originally published at https://blog.seamlesscloud.io on July 21, 2020.

--

--

Andrey
Seamless Cloud

Software Engineer with a passion for automating routine tasks.