From My Laptop to the Cloud: Deploying an ML Model using Flask & AWS (A Beginner’s Guide)
In my previous article, we tackled the messy world of data cleaning and built a highly accurate Machine Learning model to predict the Fire Weather Index (FWI).
But here is the harsh truth: An ML model sitting perfectly on your local laptop is useless to the rest of the world.
Think of it like this: Imagine you have spent months perfecting a secret recipe for the world's best pizza (this is your trained ML Model). It tastes amazing, but right now, it is only sitting in your home kitchen. If you want people to actually eat it, you need to rent a restaurant space, print a menu, and hire waiters to take orders.
In the tech world, that "restaurant" is the Cloud, the "menu" is your User Interface, and the "waiter" is your Web API.
Here is a step-by-step, simple breakdown of how I took my saved ML model and made it live for the world using Flask and AWS (Amazon Web Services).
Step 1: Hiring the Waiter (Building the Flask API)
Once our model was trained, I saved it as a .pkl file (a Pickle file is just a way to save a trained Python model so you don't have to retrain it every time you use it).
To let users interact with this file, I used Flask (a lightweight Python framework that helps you build web applications and APIs easily).
I created a simple script called application.py. This script acts as our waiter. It does two main things based on the type of request:
GET Request: When a user simply visits the website URL, Flask serves them an HTML form (just like handing them a blank menu).
POST Request: When the user fills in the weather details (like Temperature, Rain, Humidity) and hits "Predict", the browser posts (sends) this data to our Flask app. Flask then feeds this data to our Pickle model, calculates the FWI prediction, and sends the result back to the screen.
Step 2: Preparing the Kitchen Instructions (.ebextensions)
Before renting our cloud server, we need to tell the server how to run our code. AWS needs a starting point.
To do this, I created a hidden folder in my code called .ebextensions and added a python.config file. Inside, I wrote a single rule: WSGIPath: application:application. (In simple terms, this tells AWS: "Hey, when you start the server, look for the file named 'application.py' to run the website").
Step 3: Renting the Restaurant (AWS Elastic Beanstalk)
Now it was time to put the app on the internet. Instead of manually configuring a raw computer server, I used AWS Elastic Beanstalk (a Platform-as-a-Service that automatically handles the messy server setup, capacity, and health monitoring for you).
I just selected the "Python" environment on AWS, and Beanstalk automatically created a fully functioning Linux virtual machine ready to host my code.
Step 4: The Automated Manager (AWS CodePipeline)
The final piece of the puzzle is CI/CD (Continuous Integration / Continuous Deployment).
If I find a bug in my code and update my GitHub repository, I don't want to manually upload files to AWS every single time. That is tedious and prone to errors.
I set up AWS CodePipeline (an automation tool that connects your GitHub to your AWS server). Now, they are synced. The moment I write git push on my laptop, CodePipeline detects the change, grabs the fresh code, and automatically deploys it to the live Elastic Beanstalk environment. Zero manual effort required!
📌 Quick Revision (TL;DR)
Pickle (
.pkl) Files: Used to save the trained model so it can predict new data instantly.Flask: The web framework that takes user inputs from the browser and passes them to the ML model.
GET vs POST: GET just views the page; POST securely sends user data to the server.
AWS Elastic Beanstalk: The cloud service that hosts the application without us needing to manage raw servers.
AWS CodePipeline (CI/CD): The bridge that automatically updates the live website the moment new code is pushed to GitHub.
💡 Top 10 Interview Questions from this Architecture
For my fellow engineers preparing for Data or Cloud roles, here are 10 questions a recruiter might ask based on this pipeline:
1. Why do we save models as .pkl files? To serialize the model object into a byte stream. It saves the "learned rules" so we can load the model in a web app instantly without retraining it from scratch.
2. What is the difference between a GET and POST request? GET is used to request data from a server (like loading the home page). POST is used to submit data to the server (like submitting the filled weather form for prediction).
3. Why must we apply transform() and not fit_transform() on new user input? fit_transform() would calculate a new mean/variance from the single user input, breaking the math. We must strictly use transform() using the saved scaler.pkl to apply the exact same scaling rules used during training.
4. What is the role of requirements.txt? It lists all the Python libraries (like Pandas, Flask, Scikit-learn) required to run the project. AWS reads this file to automatically install dependencies on the cloud server.
5. Why did you rename app.py to application.py for AWS? By default, AWS Elastic Beanstalk’s WSGI server looks for a file named application.py and a callable named application to start the Python web service.
6. What is AWS Elastic Beanstalk? It is a PaaS (Platform as a Service) that allows developers to deploy applications without worrying about provisioning EC2 instances, setting up load balancers, or managing network configurations.
7. What is CI/CD and how did you implement it? Continuous Integration and Continuous Deployment. I used AWS CodePipeline to listen to my GitHub main branch. Any new code pushed triggers an automatic deployment to the Beanstalk environment.
8. What does .ebextensions do? It contains configuration files that allow us to customize the Elastic Beanstalk environment, such as defining the WSGI path or setting environment variables.
9. How does Flask connect HTML to Python? Using the render_template function. It allows Flask to serve HTML files and dynamically pass Python variables (like the final prediction result) back into the HTML using Jinja2 syntax {{ result }}.
10. What happens if the AWS Health Check turns Red? It means the application crashed. I would immediately check the Elastic Beanstalk logs (specifically /var/log/web.stdout.log) to debug the Python traceback error.
Coming from an SDET background, implementing an automated CI/CD pipeline felt right at home. You don't just want code that works; you want code that deploys safely and reliably.
💻 Check out the full code on my GitHub: https://github.com/Ravinder-Labs/My_ML_Project 📺 Watch the live deployment process: [Click Me]
