+359 888 400 290

Deploy Data Science model in production on AWS Elastic Beanstalk using flask application for your ecommerce or retail site

Deploy Data Science model in production on AWS Elastic Beanstalk using flask application for your ecommerce or retail site

Every Data scientist soon or later is challenged by flask 🙂


Sergi Sergiev loves to say.

Many of us data scientists are doing various models with exposure to different segments like Retail, Ecommerce, Finance, Gaming industry or many other, but we all want to see our models live in production and normally there is a need to deal with developers. Our work as a consultant is to define(finalize) the task, dive deeper, explore and clean the data, redefine the requirements, speak with the domain experts and then start making or using AI magic with our tools. We can produce different models for prediction, forecasting, item recommendations, object detection or many others, but we don’t really know how to deploy it in production.

Therefore we decided at Shopup to show you how to setup your first ready to deploy model by yourself without the support of developers or devops. It is so easy to create your API interface which can be plugged into any customer flow no matter are they retailers, ecommerce site, finance institutions or whatever. Being an independant and also doing something which can be productionize is so great. We are back in the game and with the tutorial each one is able to provide and the next steps of the consulting, implementing the model running on flask app deployed in production on AWS platform called Elastic Beanstalk.

We will not cover how to create the flask application, because there are many articles and also codes on github, but something which we found that once it comes to the next step making it in production there are so many hidden obstacles which are popping up one by one. So let’s start and we hope you enjoy and learn a lot 🙂 

Steps:

1. Create AWS account

Login to AWS and create your account freetier, also you can apply for their activate program which supports startups 

2. Create flask app.

We assume you already have your flask application there are many articles about how to create such and we are not going to cover it here.

3. Prepare virtual environment

  • It is a good practice to keep your code on git
  • Clone your code from github in the local folder with the name you prefer, we choose “demo”.
  • You need to install virtual environment and activate it



python -m venv virt
source virt/Scripts/activate

 

4. Install needed packages

Test and validate your virtual environment and install all needed packages

  • Install Flask


python -m pip install --upgrade pip
pip3 install Flask


 
  • Change start code to run it in test environment



application.run( host='0.0.0.0', port=8080, threaded=True, debug=True)
pip install python-dotenv
set FLASK_APP=demo
Flask run
 

  • Launch the application and start fixing the issues.

You need to be calm and read all the logs. The most important part is to be able to send real data and monitor the behaviour of the app and fix all issues, while you are in testing mode.

For instance we launched the app and we take the link add the section above and all relevant input data for the model. The link should look like this, which we paste on our browser: 

http://127.0.0.1:5000/dl/?xml=%7B”page”%3A%7B”cc_page_data”%3A%7B”type”%3A”checkout”%2C”id”%3A1589%2C”status”%3A”pending”%2C”status_format ….

  • Prepare the packages which are going to be installed on Elastic Beanstalk

Generate requirements file and save all needed packages with respective versions.


pip freeze > requirements.txt


 

  • Change debug to False, threaded to true and ssl_context are not needed any more.

5. Create an Access Account for ELB at AWS

To launch Elastic Beanstalk you need to create an Access Account

  • Go to IAM console

Users -> Add user -> Name “ebuser”, Programmatic access 

  • Add group – > create “beanstalkadmin” -> select “AWSElasticBeanstalkFullAccess” 
  • Select Next: Tags and Next:Review 
  • Create User

Once you see the “Success” message, this means you have successfully created the “ebuser” account. Make sure you download the “.csv” file to your local machine by clicking on the “Download.csv” button. This file is important as it holds your key and secret code. Store it in a known location on your local machine as you will need that information to connect and Secure Shell (SSH) into your EB (we won’t need SSH in this section but will in subsequent ones).

6. AWS Command Line Interface installation

The next step is to install Elastic Beanstalk (ELB) command line interface on your laptop in order to take advantage of a fast and easy way of work.


pip3 install awscli

pip3 install awsebcli  


 

7. EB setup

Once main packages are installed it is time to setup Elastic Beanstalk environment using – Eb Command Line Interface

a). Create file (.ebignore)

Add The virt folder in order to be ignored while Elastic Beanstalk is deployed.

b). start configuration


eb init -i


 

  • Select a default region: 5 Frankfort
  • If this is your first time running AWS on your computer, it will ask for your credentials. Open the “credentials.csv” that was downloaded on your machine when you created a user and enter the two fields required.
  • Enter Application name: “demo” or what you prefer
  • It appears you are using Python. Is this correct?: Y
  • Select a platform version: of you python. We tried with python 3.7, but there were some problems so we stick to python 3.6 – option 1
  • Do you wish to continue with CodeCommit? N
  • Do you want to setup SSH for your instance? Y
  • Select a key pair ( I choose one of mines) you can create new or choose something else you have.

c). Deploy and create your app



eb create demo
 

d). Next deployment

You can use the code for new release you may have.



Eb deploy
 

8. Enter into EC2

  • Check is instances demo created and working

Open Elastik Beanstalk -> Environment -> select your enviornment -> Go to Environment and check that it is set and working

  • Check security group

Select the used Security group and edit inbound and outbound traffic to all traffic

Be careful the source needs to look like the image above not the image below.

Check again the environment until it works. You can choose Go to environment or check the link below the name of the app.

9. Add Certificate (it is optional)

It is not mandatory, but sometimes you need to add a certificate to your API address instead of having http://yourdomain/delimeter it make sense to be https://yourdomain …. Below are the steps which you can follow: 

  • Short video 
  • Select AWS Certificate Manager
  • Select organization certificate
  • Add domain name
  • Select validation method. We recommend choosing email, if you have access to domain mails.
  • Select confirm and check your inbox of the following emails in order to confirm the requests

postmaster@ , administrator@, hostmaster@, admin@, webmaster@

  • Once it is confirmed check the status for the certificate it has to be Success

10. Add the certificate on ELB

a). Enter into ELB

b). Edit Load Balancer

Select application -> configuration -> Load Balancer -> edit

Add two listener of port http (80) and https(433) (( we tried only with 433 but it raises a warning and if you add with http everything is green again 🙂 

c). Add Availability zones

Something  specific which was a problem, you need to add availability zones on ELB (this need to be done because your load balancer is configured for multiple availability zones by default, but there are not instances in each zone it start generate error 503)

  • Enter into ELB
  • Select application -> configuration -> Configuration -> edit
  • Choose min and max mines are 1 and 4.
  • Select Availability zone – Any 1
  • Placement – choose one of them

11. Modify the DNS address

  • Log into CPanel
  • DNS editor
  • Copy EB instance
  • Define subdomain and paste EB instance

12. Validate

Test is the link working with the new path from the subdomain. 

If the app is not working you can modify your Load balancer again or login EC2 instance and add certificate there 

  • Log in to EC2
  • Select Load balancer and then Listener
  • Configure the settings

Congratulations you are done, great job 🙂  

We hope that we didn’t miss anything and you are up and running, there are many links and references used and each one of them is solving different problems. They are mentioned below  for your use.

If we miss something or you notice some gaps or areas of improvement let us know and share your opinion.

We will appreciate if you support us by sharing that post or commenting below.

References:

http://amunategui.github.io/aws-beanstalk/

https://blog.memcachier.com/2018/11/13/flask-elastic-beanstalk-and-memcache/

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create-deploy-python-flask.html

https://aws.amazon.com/premiumsupport/knowledge-center/elastic-beanstalk-https-configuration/

468

Submit a Comment

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