Deploying Analytical Web App with AWS Elastic Beanstalk⏱ Время чтения текста – 6 минут
If you need to deploy a web application and there’s an AWS EC2 Instance at hand, why not use Elastic Beanstalk? This is an AWS service that allows us to orchestrate many other ones, including EC2, S3, Simple Notification Service, CloudWatch, etc.
Setting things up
Previously, in our article “Building a Plotly Dashboard with dynamic sliders in Python” we created a project with two scripts: application.py – creates a dashboard on a local server, and get_plots.py – returns a scatter plot with Untappd breweries from “Building a scatter plot for Untappd Breweries”. Let’s modify the application.py script a bit to make it run with Elastic Beanstalk. Assign app.server to the application variable, it should look something like this:
application = app.server if __name__ == '__main__': application.run(debug=True, port=8080)
Before deploying our app we need to create a compressed archive. This archive should contain all the necessary files, including requirements.txt that specifies what python packages are required to run the project. Just type pip freeze in your terminal window and save the output to a file:
pip freeze > requirements.txt
Now we can create a compressed archive. Unix-based systems have a built-in zip command for archiving and compression:
zip deploy_v0 application.py get_plots.py requirements.txt
Application and Environment
Navigate to Elastic Beanstalk, click the “Applications” section and then “Create a new application”.
Fill in the necessary fields by specifying your app name and its description. After this, we are suggested to assign metadata and tag our app. The format of the tag is similar to a dictionary in Python, it’s a key-value pair, where the value of a key is unique. Once you’re ready to continue click the orange “Create’” button.
After this step, you will see a list of environments available for your app, which is initially empty. Click “ Create a new environment”
Since we are working with a web app, we need to select a web server environment:
On the next step we need to specify our environment name and also choose a domain name, if available:
Next, we select the platform for our app, which is written in Python:
Now we can upload the file with our app, click “ Upload your code” and attach the compressed file. Afterward, click “Create environment”.
You will see a terminal window with event logs. We have a couple of minutes for a coffee break.
Now our app is up and running, if you need to upload a new version, just create a new archive with updated files and click the” Upload and deploy” button again. If everything’s done right, you will see something like this:
We can switch to the site with our dashboard by following the link above. Using the <iframe> tag our dashboard can be embedded into any other site.
<iframe id="igraph" scrolling="no" style="border:none;" seamless="seamless" src="http://dashboard1-env.eba-fvfdgmks.us-east-2.elasticbeanstalk.com/" height="1100" width="800"></iframe>
As a result, you can see the following dashboard: