Yet another guide to deploy Plotly Dash on AWS Elastic Beanstalk

In August, I got interested in Amazon Web Service (AWS) and spent some time to get an AWS Cloud Practitioner certificate. To put into practice what I have learned during the training, why not develop a web application, I asked myself. Thus, I decided to create a Plotly Dash dashboard and deploy it on AWS. The service that I chose is AWS Elastic Beanstalk. You can find, on the Internet, several guides written by amateurs to teach you how to deploy Dash on AWS. However, there is something lacking in all these guides. Therefore, I, also an amateur, decided to write a guide myself. In the following, I will show you how to achieve this “feat” step by step. To understand this guide, it is a prerequisite to know how to develop a Dash application and what AWS Elastic Beanstalk is.

This guide is composed of two parts:

  • How to prepare a deployable Dash application, and
  • How to deploy the Dash application on AWS using the command line interface (CLI).

How to prepare a deployable Dash application

First thing first, you need a Dash application. If you download this application file from the Dash online documentation, then congratulation, you application won’t work! Well, there are several things inappropriate in that file:

  • The online doc suggests you to name your application file as app.py. This is a big trap. Actually, you need to name it as application.py. If you insist on giving it a cute name such as taylor-swift.py, you will have to do extra work on AWS Elastic Beanstalk’s configuration, which will not be covered in this guide.

  • Be sure to define a variable named application. Usually, you define it as application = app.server, where app is the application object in question. This variable is not needed for a Dash application, but is an absolute must for the deployment on AWS.

  • Be sure not to run the server with debug=True. This option is useful for development, but is a disaster for production because debug output can reveal internal aspects of your application.

Note: Another blogger, Ruslan Korniichuk, pointed out that an extra line app.scripts.config.serve_locally = True is required for Dash DAQ components. See here for the origin of this statement.

Since now you have a valid application file, the next thing you need is the dependency file named requirements.txt. If you started you application with a virtual environment, just run

pip freeze > requirements.txt

Otherwise, you can manually create this file with the format <package name>==<version number: x.y.z> line by line.

To be sure your application run like a charm (at least locally), you can optionally run python application.py locally before deploying it on AWS.

How to deploy the Dash application on AWS using the command line interface (CLI).

Now, everything is ready except the east wind. You will finally deploy your awesome application on AWS. In this guide, I will teach you how to do it with the command line interface (CLI). Of course, you can instead use the web interface, but I find the CLI more smooth.

First thing first, you need to install the Elastic Beanstalk Command Line Interface. AWS recommends using the EB CLI setup scripts for this purpose. I, nonetheless, recommend installing it manually, for it is hard for you to know what that script does for you.

To install it manually, follow the instruction on AWS documentation. The punchline thereby is

pip install awsebcli --upgrade --user

Just make sure that you do it with a separate virtual environment, so that it would not mess with other environments (virtual or not). If you are not able to do it, you might as well consider abandoning the idea of becoming a data scientist.

With the installed CLI, you will be capable of, among others,

  • initiating your repository,
  • deploying your application,
  • opening the URL of your web application,
  • updating your web application,
  • terminate your web application.

To initiate the repository, run

eb init

It will make your repository a Git repo as well as set the necessary configuration.

To deploy the application for the first time, run

eb create

To open the URL of the web application, run

eb open

To update the web application after you developed a new verson, run

eb deploy

To terminate the web application and release all resources, run

eb terminate

Conclusion

Now, you have everything you need to make a simple application. Put it into practice!

If you discover any mistake in this guide, or you have any suggestion, please do not hesitate to leave a comment below. I highly appreciate this kind of interaction.

If you find it hard to follow some of these paragraphs, the following guides might be able to help you:

Last not but least, for French readers, have a look at the simple application that I developed: http://guessfromyourname-env.apqnxmc3gr.eu-west-3.elasticbeanstalk.com/

Written on September 28, 2019