Convert your Python apps from Heroku to the AWS Free Tier

Austin Lasseter
7 min readAug 30, 2022

--

A lot of us are very disappointed to hear the recent news that Heroku is going to discontinue its free service. Heroku has been a great place to learn the basics of cloud deployment and to get started with app-building. Personally, I’ve built and deployed hundreds of Python apps to Heroku using both Flask and Plotly Dash. Like many developers, I’m looking for an easy way to redeploy those apps to a new platform that will be as easy and inexpensive as Heroku. It’s an opportunity to move to a more robust cloud platform, which for me is Amazon Web Services.

What’s the easiest way to quickly redeploy my existing army of Plotly Dash apps to AWS? One possible answer is the ElasticBeanstalk service, as part of the AWS Free Tier. Be warned that the free tier does have some serious pitfalls but when managed with prudence and attention it’s easy to keep things secure and free.

Getting started

The first step is to sign up for an AWS account. There’s a lot of ground to cover if you’ve never used AWS before, so I highly recommend you start reading. The major steps you’ll need to cover:

Opening an account is pretty straightforward — you provide your details and your credit card, and AWS will set you up as a “root user” with a username and password. But this is a dangerous tool as the root user basically has God-mode. The first thing you need to do after opening an account is stop using the root user for anything other than checking your AWS bill (which you should monitor frequently). You should also make sure that you secure the root user with Multi-factor authentication (MFA) as an extra precaution.

Read this post from AWS about setting up a non-root user; don’t skip this step. Write down the Access key ID, Secret access key and Password; these will be your ticket to entry going forward. \

Getting started with ElasticBeanstalk CLI

AWS Elastic Beanstalk is the app development platform for people who don’t really know how to use an app development platform (like data scientists, for example). It’s supposed to be an easy-to-use service for deploying and scaling web applications and services developed with Python (or other languages). Similar to Heroku, it simplifies the process of deploying your app to the cloud so that you can focus on what you do best (actually developing the app).

There are several ways to deploy apps using EB: the UI interface, docker, and the Command Line Interface (CLI) are the three most common. We’re going to focus on the third option (CLI). You can read about this in detail in the AWS documentation. I’m going to summarize the principal steps.

Install the latest version of the CLI using terminal using the following command:

pip install awsebcli

In a Sagemaker Studio Environment, that’s going to look like this:

Confirm that the EB CLI is installed correctly by running:

eb --version

which should be EB CLI 3.20.3 (Python 3.9.7). That’s it! Let’s get started with our app.

Retooling your Heroku app for ElasticBeanstalk

Let’s say you have an app you’ve already developed for deployment on Heroku, like this one. I’ve also created a repository on github with the updated version of the same code. Here are the steps you should follow.

  1. Clone the repository from github to your local workspace.
  2. Set up a virtual environment and activate it, then update and install the requirements.txt file, just as you would usually do for any app.
python -m venv env
source env/bin/activate
pip install -r requirements.txt
  1. Delete the files Procfile and runtime.txtas you won’t need them in EB. These requirements were unique to Heroku.
  2. Rename the file app.py to application.py
  3. Inside the app.py file, replace any instance of app with application . In our example, this occurs in two places, as follows. Note that you should not replace all instances of app but only these two.
application = app.serverapplication.run(debug=True, port=8080)

4. You must also update the last two lines, replacing app.run_server and adding port 8080 as follows:

if __name__ == '__main__':
application.run(debug=True, port=8080)

Add everything to your repo:

git add *
git commit -m 'updated from heroku to eb'

Deployment to ElasticBeanstalk

Initialize elastic beanstalk: eb init and choose all the default settings in the dialogue prompts that follow. The most important question concerns your credentials, which you copied down earlier when setting up your account. Be sure to use the creds from your non-root user, for extra security!

You have not yet set up your credentials or your credentials are incorrect 
You must provide your credentials.
(aws-access-id): AKIAXXXXXXXXXXX
(aws-secret-key): XXX************

The CLI will ask the following questions after you type eb init It’s typically fine to select the default response, though in some cases I recommend not taking the default. Responses as follows:

  • Select a default region [I like to use us-east-1 : US East (N. Virginia) because I live there]
  • Select an application to use [default is 3, Create new Application ]
  • Enter Application Name
  • It appears you are using Python. Is this correct? [Y]
  • Select a platform branch. [default is Python 3.8]
  • Do you wish to continue with CodeCommit? [choose “no”. CodeCommit is an inferior github knock-off which Amazon is trying to force on people].
  • Do you want to set up SSH for your instances? [probably not, unless you know why you need this. it’s not typically necessary]

Once you’ve finished answering the questions, EB will create a hidden folder with your elastic beanstalk settings. Now the contents of your directory should look like this:

Next we create an instance of our app on AWS: eb create

In the dialogue prompts that follow, go ahead and choose the defaults, as follows.

  • Enter Environment Name (default is similar to your app name)
  • Enter DNS CNAME prefix (default is similar to your environment name)
  • Select a load balancer type (default is “2: application” and you should take this).
  • Would you like to enable Spot Fleet requests for this environment? (typically “no”)

Now get up and make a sandwich, because this is going to take a while. When you come back, you should have a message that says your app has launched successfully. Somewhere above that, you’ll also see a line with “CNAME” that has the URL of your new app.

You can view the finished app deployed on EB here: http://radio-callbacks-env.us-east-1.elasticbeanstalk.com/

Additional troubleshooting steps

As soon as your run into trouble, the first thing you should do is check your logs with the command

eb logs

You should also get in the habit of checking the status of your EB dashboard (though this should not become an crutch that replaces the CLI). It looks like this:

Most problems are caused by a few common issues:

  • Your app has internal code issues that you should debug locally, prior to deploying to elastic beanstalk. Get in the habit of frequently running python application.py in your local environment.
  • Your requirements.txt is out-of-date or incorrect. The best way to solve this is to work with a virtual environment and pip install -r requirements.txt
  • Your app may be missing elements specific to EB — get in the habit of reading the documentation.

You’ll soon find that you want to make updates or changes to your app. You can redeploy this using the CLI with the command

eb deploy

I’ve created a repository on github with the updated version of the code for this exercise.

But is it really free?

So we did all this in order to move from Heroku free-tier to AWS free-tier. By default, EB will provision resources that are free-tier compatible (such as an S3 bucket and t2.micro instances). You can double-check your deployed resources by checking the “Configuration” tab of your environment in the console, like this:

You’ll also want to become familiar with the AWS Billing dashboard. While everything we do here is limited to the free tier, it’s easy to rack up charges in AWS if you’re not careful about what you leave running. Read more about precautions you can take, and keep an eye out for emails like this one.

Keep in mind that the free tier only lasts for 12 months from the time you initiate your AWS account, so you’ll need to keep an eye on the clock. At the end of that time, you’ll be charged a monthly fee. Also, if you deploy too many apps in EB, you may exceed your monthly free tier ceiling. So there are pitfalls — but as long as you’re careful and monitor, you can keep it free. Also, unlike Heroku, you’ll be learning skills that will transfer to real-world job settings and increase your knowledge of the leading cloud provider in the world. It’s high time we all left Heroku and started deploying our apps (even the smallest ones) in a way that meets industry standards and expectations.

Additional reading:

--

--