Introduction To EC2 Instance
If you work in any of the Artificial Intelligence domain, it will be in your daily routine to develop some exciting and crazy AI models. Either you develop it from scratch, modify the previous work or use any libraries or framework to build a smart solution, Agree?
After building that AI model, you might think of sharing it with your friends and colleagues to experience what you have developed, but soon you realize that you can’t bring your laptop or computer system to everyone to experience your fantastic AI program! Disappointed?
You have come so far, so please don’t feel disappointed, We will help you to share your efforts all over the globe by hosting it on AWS EC2 instance! So, let’s move forward and make it possible, together.
Before starting deployment on an EC2 instance, you must have to build the API to communicate with your ML program/model. If you haven’t built the API yet, kindly overview article “How to integrate Flask API with ML Models“
The prerequisites for the deployment of your AI Server on EC2 instance are:
- Active AWS Credentials (Root or AMI user with access)
- Working Python Server integrated with ML model within the API
The main problem which I faced during the initial phase of my career in deployment is, I didn’t find all the steps that needed to be followed for successful deployment of the AI model or AI Program within a single link. I had to open a number of tabs in different browsers to get this task done.
I have tried to mention every single step that can be followed by the beginner as well to deploy the AI model/program with Flask-Python.
At the very first, Open AWS Management Console and select EC2 from AWS Services.
Once you’re on the EC2 Instance dashboard, Select Instances (running).
Now you’re ready to launch the instance, so select the orange button named ‘Launch Instance’ and move to the next step.
As a developer, you already know that Ubuntu has an edge compared to any other OS. I would recommend selecting ubuntu 18.04 LTS or 20.04 LTS, a free tier eligible machine.
While creating an instance, you will be asked to choose an Instance Type, select it according to your requirements, once selected press next to jump on the next step.
Default storage would be selected on the basis of the instance type you have selected in the previous step. Add the storage now if needed. I would recommend increasing it up to 20GB. In this example, I increased it up to 40GB.
Security groups are essential rules of your instance and it will only allow the traffic based on what you have added in the rules.
It is not one of the best practices to allow ‘All traffic’ to your instance, as I have done in the attached example, but as it’s a beginner guide, you can follow the same rules for now, or add it according to your requirement. Once you are done with adding all the necessary rules, you can press the Review & Launch button.
I know it’s a long process but, The wait is OVER! You can now review and press the Launch button.
Now, create a new key pair and put the unique name for your instance key, then finally download the key pair. After pressing the download button, you will find the key with the selected name in your Downloads directory.
Before moving to the next step, take a breath and focus on this important point, that this key which you just downloaded is the most valuable thing and same as the password of your instance. If you lose it, IT’S ALL OVER!
Once the instance is launched successfully, you can come to the main instances page and wait for a while so the instance will set up and show 2/2 checks passed in the status check column.
Select the row of your instance and name it (Optional but preferable), and press the Connect button, once you press the button, navigate to the SSH client tab.
Now, open the terminal of your local system inside the directory where you have put the ‘.pem’ file which was downloaded, and then paste the ‘chmod’ command to make the key private.
Copy the example command mentioned to build the connection of your EC2 instance with your local machine.
Paste the above-copied command in the same terminal, then type ‘yes’ and press enter for making a successful connection.
Hurry! You’re now inside your EC2 instance machine!!!
As the server is built on Python, you will need Anaconda. Install and Setup Anaconda.
- Install and Setup Anaconda
- sudo apt-get update
- cd /tmp
- wget https://repo.anaconda.com/archive/Anaconda3-2020.02-Linux-x86_64.sh (or avaialble latest version)
- bash Anaconda3-2020.02-Linux-x86_64.sh (press enter, yes, and then enter again)
- Type yes here and press enter.
- source ~/.bashrc
- cd ~
- conda update conda
- conda update anaconda
- Create the conda environment for your project by conda create -n ‘name’ python=3.7 ( python=3.7 is optional or according to requirement, the name is the env name )
- conda activate ‘name’
- Make the directory for your AI-Server ( mkdir ‘dirname’ ), and then clone the code inside that directory.
- mkdir dirname ( Command to create the directory )
- cd dirname ( Command to change the directory )
- git clone [ https://… ] ( Command to clone the code inside the directory ) ( Clone your python code here )
- Install all the requirements by pip install -r requirements.txt
- Install Gunicorn (pip install gunicorn)
- Run your python app with gunicorn by gunicorn run:app –daemon
Got tired from the long list of steps? Apologize, but the good news is, your app is already deployed and running!!! And now you can test it via postman and share your API or set of APIs with your friends and colleagues.
On postman, instead of localhost, you can put the Public IPv4 with the port number mentioned in your python code!
Conclusion on EC2 Instance
In Tezeract’s this article, you have learned how to deploy an AI model integrated with the Flask-Python server on an AWS EC2 instance service. I have tried to mention every possible detail with screenshots for a better understanding considering the problem faced by the beginners in this field.
Hopefully, you guys found this article fruitful!
In the next part, I will be trying to solve the problem of installing SSL and setting up Nginx for your instance on which the Flask-Python server is running, so that you guys can make your API requests secure!