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.
- 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
- Part 1: How to deploy a Python Flask app to AWS Elastic Beanstalk.
- Part 2: Automated deployment to AWS Elastic Beanstalk using Github Actions.
- Part 3: AWS Elastic Beanstalk infrastructure in code with Terraform.
- Part 4: In progress…
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:
- 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.
- Easy to get started. I will prove it by the end of this tutorial.
- 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.
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.