Deploy Flask on AWS Elastic Beanstalk – News Couple

Deploy Flask on AWS Elastic Beanstalk

Photo 1-

Learning model development consists of data cleaning, arguing, comparing different ML models, and training models on Python Notebooks computers such as Jupyter. Of course, all of the above steps can be undone without such notebooks. However, the use of these laptops is restricted to developers as they cannot be directly used for an application by any non-technical user.

Moving from laptops to products

If you use any website to check weather forecasts for your city, the forecasts you see come from a model that may have been developed in some laptops but users use the website for forecasts.

As you must have guessed, there are ways to turn laptops into a form that can be combined into a product that can be accessed and used by anyone – technical or non-technical users. This process is called “form publishing”.

Form publishing process

Picture 2 –

What is in this article for you?

  1. ✔️ Get the basics Understanding AWS ElasticBeanstalk What does it do and when should it be used?
  2. ✔️ Learn how to do it Building a basic Flask app
  3. ✔️ Learn how to gradient Deploy the app to AWS ElasticBeanstalk
  4. ✔️ Understand when you should Use AWS ElasticBeanstalk to deploy to other AWS services

Icebreaking with AWS Elastic Beanstalk:

Aws beanstalk

AWS Elastic Beanstalk is a computing service that allows you to load your web application code along with environment configurations – based on this, Elastic Beanstalk automatically provision and deploy the necessary resources required within AWS to run the web application. These resources can include other AWS services and features such as EC2 instances, flexible load balancing, automatic scaling, etc.

As a data science professional, if you are not familiar with the intrinsic things 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 entire deployment process.

How much does it cost to use AWS Elastic Beanstalk?

AWS Elastic Beanstalk as a service is free to use. However, resources used to build an application such as EC2 instances come with a fee as per the standard pricing policy at the time of publication.

AWS Elastic Beanstalk Benefits

  1. ⭐ AWS EBS presents a Simple and fast method To publish web applications.
  2. ⭐ With the Electronically Controlled Brake System (EBS), you can Focus on your application code instead of provisioning and configuration AWS Resources.
  3. ⭐ Automatic scaling settings On EBS it helps scale the application automatically.
  4. ⭐ one has Control all AWS resources Like EC2 running the app.
  5. ⭐ AWS EBS Provides a cost-effective price As one has to pay for what they use and there are no hidden costs.
  6. ⭐ AWS EBS Supports Java, .NET, PHP, Node.js, Python, Ruby, Go and Docker web applications.
  7. ⭐ Access to monitoring metrics Such as CPU usage, number of requests, and response time.
Publish the form

Steps to continue publishing👣

Things you need:

  1. ☝ AWS Account
  2. ☝ Knowledge of Python (Jupyter notebooks)

Build Flask API

Flask is a web framework that can be used to create web applications using Python.

We can install Flask using the following line of code:

pip install flask

After installation, you can import the library and then create an application object. We add a path (“/”) to display a ‘Hello World!’ message. The path allows us to set a web URL for the Python function. Whatever function returns the user is shown on the web address.

from flask import Flask
application = Flask(__name__)
def hello():
    return “Hello World!”
if __name__ == “__main__”:

Write the above lines of code in a .py file and name it “”.

Create a Requirements.txt file

We will need a requirements.txt file that lists all the libraries we used in our ‘’ file. For our case, this only consists of the following line:


After creating this file, we can compress these two files which can be used later while creating the application.

Create an app on AWS Elastic Beanstalk

Sign in to the AWS Console and search for AWS ElasticBeanstalk. Click Create Application. We can name our app. For the platform, we choose Python 3.8 and for the application code, we upload the .zip file we created before.

AWS flex bean
AWS flex bean

create an environment

Once the source code of our app is loaded, we can see our app’s environment being created and finally the app being built using a custom web URL for the app.

Flexible beanstalk

What else do you get?

  • 👉 Dashboard To check the health status of your app and recent events.
  • 👉 Configuration Panel With it, you can manage the entire environment.
  • 👉 Records – Logs from your application which allows tracking of errors that occurred in the event of a deployment failure
  • control panel – To check some basic metrics like CPU usage and network traffic.
  • 👉 Alarms based on monitoring metrics – The warning email that you can receive when the CPU usage is for example above 80% can be set up for a specified period of time.

When will AWS Elastic Beanstalk be deployed across EC2?

EC2 is an Amazon service that can be used to create a server (or instances) in the AWS cloud. On the other hand, Elastic Beanstalk is one layer of abstraction apart from the EC2 layer. Elastic Beanstalk creates an “environment” for us that can contain multiple EC2 instances, an optional database, as well as some other AWS components like elastic load balancing, auto-scaling, etc. Elastic Beanstalk manages these items for us whenever we want to update our app.

If you are trying to publish a simple app in a short period of time (like an app that only shows a list of things), you can go for Elastic Beanstalk.

⚠️⚠️ However, there are some situations where you would like to prefer EC2 over AWS Elastic Beanstalk. If you develop a file An app that has a lot of things going around like multiple file uploads, simultaneous accounts, continuous delivery of notifications to users, etc. would be a better choice to take advantage of EC2.

About the author

Nepdita Dutta

Nibedita completed her MSc 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 builds AI/Machine Learning-based solutions for clients from a range of industries.

Image sources:





The media described in this article is not owned by Analytics Vidhya and is used at the author’s discretion.

Source link

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button