Deploying a Flask App on AWS Elastic Beanstalk
Image 1- https://static7.depositphotos.com/1020804/754/i/600/depositphotos_7541455-stock-photo-green-beans.jpg
Whether you are an experienced or an aspiring data scientist, you must have worked on machine learning model development comprising of data cleaning, wrangling, comparing different ML models, training the models on Python Notebooks like Jupyter. All the above steps are of course undoable without the presence of such notebooks. However, these notebooks are limited for use by the developers as they cannot be directly utilized for an application by any non-technical user.
Moving from Notebooks to Products
If you use any website for checking the weather forecasts of your city, the forecasts that you see are coming from a model that might have got developed in some notebook but the users are using the website for the forecasts.
As you must have guessed, there are therefore ways to convert the notebooks to a form that can be integrated into a product that can be accessed and utilized by any person – technical or non-technical users. This process is called ‘model deployment’.
What’s in this Article for You?
- ✔️ Get a basic understanding of AWS ElasticBeanstalk – what it does, when should you use it
- ✔️ Get to know how to build a basic Flask App
- ✔️ Learn stepwise how to deploy the app on AWS ElasticBeanstalk
- ✔️ Understand when you should use AWS ElasticBeanstalk for deployment over other AWS services
Icebreaking with AWS Elastic Beanstalk:
AWS Elastic Beanstalk is a compute service that allows you to upload code of your web application along with environment configurations – based on which Elastic Beanstalk automatically provisions and deploys the necessary resources required within AWS to make the web app operational. These resources can include other AWS services and features like EC2 instances, elastic load balancing, autoscaling, etc.
As a data science professional, if you are not familiar with the nitty-gritty of model deployment, it would be ideal for you to use AWS Elastic Beanstalk to deploy your ML model as it automates and simplifies the whole process of deployment.
How much does it cost to use AWS Elastic Beanstalk?
AWS Elastic Beanstalk as a service is free to use. However, the resources that are utilized to create the application like EC2 instances come at a charge as per the standard pricing policy at the time of deployment.
Benefits of AWS Elastic Beanstalk
- ⭐ AWS EBS offers a simple and quick way to deploy web applications.
- ⭐ With EBS, you can focus on your application code rather than provisioning and configuring AWS resources.
- ⭐ Auto-scaling settings on EBS helps automatically scale the application.
- ⭐ One has control over all the AWS resources like EC2 that are powering the app.
- ⭐ AWS EBS provides a cost-effective price in which one has to pay for what one uses and there are no hidden costs.
- ⭐ AWS EBS supports Java, .NET, PHP, Node.js, Python, Ruby, Go, and Docker web applications.
- ⭐ Access to monitoring metrics like CPU utilization, request count, and latency.
Steps to Follow for Deployment👣
Things you need:
- ☝ AWS account
- ☝ Knowledge of Python (Jupyter Notebooks)
✍ Building a Flask API
Flask is a web framework that can be used to build web apps with Python.
We can install ‘Flask’ using the following line of code :
pip install flask
Post installing, you could import the library and then create an application object. We add a route (“/”) to display the ‘Hello World! ’ message. A route enables mapping a web URL to a Python function. Whatever the function returns is shown to the user on the web URL.
from flask import Flask application = Flask(__name__) @app.route(“/”) def hello(): return “Hello World!” if __name__ == “__main__”: app.run()
Write the above lines of code in a .py file and name it as ‘application.py’.
✍ Creating a Requirements.txt file
We will need a requirements.txt file that lists all the libraries that we have used in our ‘application.py’ file. For our case, this consists of only the following line:
After we have created this file, we can zip these two files that can be later used while creating the application.
✍ Creating an application on AWS Elastic Beanstalk
Sign in to AWS Console and search for AWS ElasticBeanstalk. Click on ‘Create Application. We can give our application a name. For the platform, we choose Python 3.8 and for the application code, we upload the .zip file that we created before.
✍ Creating an Environment
Once we have uploaded the source code of our application, we can see the environment of our application getting created and finally the application getting created with a web URL assigned to the application.
What else do you get?
- 👉 Dashboard for checking the health status of your app and recent events.
- 👉 Configuration board using which you can manage the entire environment.
- 👉 Logs – the logs from your app that enables tracking what went wrong in case your deployment fails
- 👉 Monitoring board – for checking some basic metrics like CPU utilization and network traffic.
- 👉 Alarms based on monitoring metrics – Warning email could be set up which you can receive when for instance CPU is utilized over 80% for a specific period of time.
When to deploy AWS Elastic Beanstalk over EC2?
EC2 is Amazon’s service that can be utilized for creating a server (or instances) in the AWS cloud. On the other hand, Elastic Beanstalk is one layer of abstraction away from the EC2 layer. Elastic Beanstalk sets up an “environment” for us that can contain multiple EC2 instances, an optional database, as well as a few other AWS components such as elastic load balancing, autoscaling, etc. Elastic Beanstalk manages these items for us whenever we want to update our application.
If you are trying to deploy a simple app in a short period of time(like an app that just displays a list of things), you can go for Elastic Beanstalk.
⚠️⚠️ However, there are some situations when you would like to prefer EC2 over AWS Elastic Beanstalk. If you have developed an app that has a lot of things going around in it like multiple file uploads, simultaneous calculations, continuous delivery of notifications to users, etc., it would be a better choice to leverage EC2.
Nibedita completed her master’s in Chemical Engineering from IIT Kharagpur in 2014 and is currently working as a Senior Consultant in an analytics firm. In her current capacity, she works on building AI/ML-based solutions for clients from an array of industries.
The media shown in this article is not owned by Analytics Vidhya and are used at the Author’s discretion.