Logo

Deploying Machine Learning Models on AWS EC2 using Python: A Step-by-Step Guide

Default Alt Text

Table of Contents

Understanding the workflow of deploying Machine Learning Models

Deploying machine learning models is a crucial step in the data science pipeline to make your built model available for real-world use. The process can be complex, but it typically follows a standard workflow. Initially, a machine learning model is trained using datasets and then tested for evaluation. Once we are satisfied with the model’s performance, it is serialized (i.e. converted into a format that can be stored or transmitted) and then deployed onto a hosting service like AWS EC2. This allows the model to handle real-time data and generate predictions. Deployment also requires establishing an interface (like a web service or an API) to allow other software components to use the model’s predictions. Now, the model is available for real-time uses like recommendation systems, image recognition systems, voice intelligent systems, and more. The specific steps can vary, especially when considering the cloud infrastructure, but having a general understanding of this flow can help you navigate the details as we move forward.

Brief on AWS EC2 and its relevance in model deployment

Amazon Web Services (AWS) Elastic Compute Cloud (EC2) is a web service that offers secure, resizable compute capacity in the cloud. It is designed to make the web-scale cloud computing easier for developers. The implications of its ease-of-use extend to deploying machine learning models, a task that call for a solution that is versatile and scalable. AWS EC2 enables users to launch instances, which are virtual servers in the cloud. These instances can be used as a platform to run and store ML models. The inherent benefits of EC2 instances are that they can be scaled up or down based on the computational requirements of the model, and they can be configured to fit the specific needs of your model, like choosing the operating system and software stack. With EC2, you can host your ML models in a secure, highly available environment, and manage them using automated tools to reduce the time and cost of system administration. This makes AWS EC2 an excellent choice for deploying ML models.

Setting up Amazon Web Services (AWS) Environment

Creating AWS Account and setting up EC2

As a point of clarification, signing up for AWS and setting up EC2 can’t be accomplished with Python code. These are manual steps that need to be taken on the AWS platform by a user. Below is a simplified step-by-step process, however, of how you can achieve this:

1. Visit the AWS Website: You can visit the AWS website and click on ‘Sign Up’.
2. Create an AWS account: You’ll need to provide your personal information and credit card details. After providing these details and confirming your email address, you’ll be able to create an AWS account.
3. To set up an EC2 instance, you need to:
– Sign in to the AWS Management Console and navigate to the EC2 section.
– Click the ‘Launch Instance’ button.
– Choose an Amazon Machine Image (AMI).
– Choose an instance type.
– Configure the instance.
– Add storage to the new instance.
– Tag your new instance.
– Configure your security group.
– Review the configured instance and finally, launch it.

It’s important to note that launching a new EC2 instance may incur additional costs on your AWS account. Also, the creation of an AWS account and the EC2 instance need to be done through the AWS Management Console (web interface) and not via a Python script.

In terms of Python, once the AWS account and EC2 are set up, you can then interact and manage your AWS services programmatically using the AWS SDK for Python (Boto3). Bookkeeping considerations include safely storing your AWS access and secret keys. To maximize security, it’s recommended to assign just the necessary permissions to your AWS users/roles for accessing the AWS resources.

Configuring AWS EC2 instance

Setting up and configuring an EC2 instance is largely done via the AWS Management Console. Since the interface is graphical and interactive, it’s not something that’s typically driven through Python or any other coding language. However, you can control instances through AWS SDKs once they are running. Below is an example of how to launch a pre-configured EC2 instance using AWS SDK for Python (Boto3).

import boto3
ec2 = boto3.resource('ec2')
instances = ec2.create_instances(
     ImageId='ami-0d5d9d301c853a04a',  # replace with your image id
     MinCount=1,
     MaxCount=1,
     InstanceType='t2.micro',  # replace with your instance type
     KeyName='my-key-pair'   # replace 'my-key-pair' with your key pair name
 )

for instance in instances:
    print("Instance id", instance.id)

This script launches an EC2 instance with specifics pre-defined by you. Ensure you’ve setup AWS credentials on your machine and installed the ‘boto3’ library, which allows Python developers to write software that makes use of services like Amazon S3, Amazon EC2, and others. At completion, it will output the ID of the created instance.
Reiterating, always remember to stop or terminate your EC2 instances when they’re not in use to avoid unexpected charges.

Setting up Python Environment

Installing Python on AWS EC2

As you begin setting up your Python environment, the first step is to install Python on your AWS EC2 instance. This can easily be achieved via Secure Shell (SSH). An SSH client enables you to log into your EC2 instance and run commands as if you were sitting in front of the computer. The `yum` command that we use here is a package manager that allows you to install, update, and remove software packages on your instance.

Please note that you would typically first connect to your EC2 instance using SSH from your local machine before executing these commands.

sudo yum update -y

sudo yum install python3 -y
python3 --version
pip3 --version

Here’s what the code is doing:
– The `yum update -y` command updates all the software packages on the instance.
– `yum install python3 -y` installs Python 3 and pip3, the Python package manager.
– The `–version` flag confirms that Python and pip have been successfully installed by printing their version number.

Remember to replace `/path/my-key-pair.pem` with the relative path to your key pair file, and `ec2-user@ec2-198-51-100-1.compute-1.amazonaws.com` with the Public DNS (IPv4) of your instance which can be found in your instance description on the AWS console. Always ensure you properly manage your access credentials to maintain the security of your resources.

Installing necessary Python libraries

Installing the necessary machine learning libraries on your AWS EC2 instance is a relatively straightforward process with Python’s package installer, pip. Keep in mind your instance should have an attached storage (EBS Volume) with enough space for installing libraries. Let’s take for instance, the popular library for machine learning, ‘scikit-learn’. We will also install ‘pandas’, a library used for data manipulation and analysis.

sudo yum update -y
sudo yum install python3-pip
pip3 install pandas
pip3 install scikit-learn

What this code does is: it first connects to your EC2 instance. It then uses ‘yum’, the package installer for Linux, to update the system and install ‘python3-pip’, the Python package installer. Here ‘sudo’ is used to grant administrator permissions for the commands. Finally, it installs ‘pandas’ and ‘scikit-learn’ via ‘pip3 install’. You should replace ‘pandas’ and ‘scikit-learn’ with the necessary library names for your project. You now have access to the necessary machine learning libraries on your EC2 instance.

Deploying Machine Learning Models

Preparing the Machine Learning Model

Understanding the deployment of Machine Learning (ML) models is an operation that involves a series of systematic processes. First, you must train your model on ample data for it to gain the ability to make accurate predictions or decisions. In doing so, this training will typically yield a final model that encapsulates the patterns and relationships it has inferred from the data. Once satisfied with the model’s performance, the model is then saved and made ready for deployment. The deployment process involves setting up the target environment in such a way that it can accept new data input, run the model, and offer outputs – or predictions – based on that input. This process may include installing necessary software and libraries, configuring system or application settings, or utilizing platforms like AWS for a more scalable approach. In a cloud environment like AWS, a trained model can be hosted on an EC2 instance and accessed as a web or application service. Understanding this workflow is crucial for effective and efficient deployment of ML models.

Hosting the model on EC2

Setting up hosting for a machine learning model in AWS EC2 involves copying the model file from your local system to the EC2 instance and then running a prediction service on the EC2 instance that can use the model to make predictions. Here, we assume that the ML model is a pickle file, `model.pkl`.

Firstly, install the necessary Python libraries for running SSH and SCP in Python. You can do it using pip:

pip install paramiko scp

Next, use the following code to upload your model to the EC2 instance:

import paramiko
from scp import SCPClient

ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect('', username='', key_filename='')

scp = SCPClient(ssh.get_transport())

scp.put('', '')

scp.close()

Remember to replace ``, ``, ``, ``, and `` with your EC2 instance’s IP address, username, path to your PEM file, and paths to your model file.

After your model is successfully uploaded to your EC2 instance, you can set up a prediction service on your EC2 instance that uses this ML model to make predictions. This can be a simple flask-based API server running on the EC2 instance.

Having executed the above code, you should now have your machine learning model uploaded and ready to host from your AWS EC2 instance. This opens the doors to sharing your model’s capabilities with other services and reusing the model whenever you require it.

Running the model

Deploying a Machine Learning model on AWS EC2 involves exposing the model as a web service that can accept inputs, run predictions, and return outputs. The workflow involves several steps, starting from setting up the AWS environment till the management and monitoring of the deployed model. First, it starts with the creation of an account and setting up an EC2 instance on Amazon Web Services (AWS). This is followed by the installation and configuration of Python and the relevant libraries on the AWS EC2 instance. The machine learning model is then prepared on a local environment before being hosted on EC2. Finally, the model is run on new data to get prediction results, and its performance is monitored using AWS Cloudwatch. Any necessary updates or improvements to the model are then made, and the process is repeated. Familiarity with these steps allows for efficient deployment and management of machine learning models on AWS EC2.

Monitoring and Management

Monitoring model performance

AWS Cloudwatch is an in-built tool from AWS that allows collection and tracking of metrics. These metrics can then be used to monitor and manage the performance of the application. Here is how you could set it up using Python and Boto3, the Amazon Web Services (AWS) SDK for Python:

import boto3

cloudwatch = boto3.client('cloudwatch', region_name='us-west-2')
cloudwatch.put_metric_alarm(
    AlarmName='CPUUtilization',
    ComparisonOperator='GreaterThanThreshold',
    EvaluationPeriods=1,
    MetricName='CPUUtilization',
    Namespace='AWS/EC2',
    Period=60,
    Statistic='Average',
    Threshold=70.0,
    ActionsEnabled=False,
    AlarmDescription='Alarm when server CPU utilization exceeds 70%',
    Dimensions=[
        {
            'Name': 'InstanceId',
            'Value': 'INSTANCE_ID'
        },
    ],
    Unit='Seconds'
)

This script sets up a CloudWatch Alarm called ‘CPUUtilization’, which triggers when the CPU usage rises above a certain threshold on a specified EC2 instance. Replace “INSTANCE_ID” with your actual instance ID. The `put_metric_alarm` method configures the alarm based on parameters like the number of evaluation periods, the metric name, the namespace, the period over which the data is collected and average, and the comparison operator.

Managing model updates

Since creating an AWS account, setting up an EC2 instance or updating details is a manual process, these operations are performed using AWS Management Console UI and not directly using Python code. However, once EC2 instance is set, you can interact with it using Python.

Let us assume that the EC2 instance is available and active. We have an existing ML model, ‘model.pkl’, that we have trained previously and the same is available to us locally. Now, we want to update this model file to the EC2 instance. We can use Python’s `paramiko` package which allows us to create SSH client and interact with the EC2 instance.

First, let’s install `paramiko` if not already done.

!pip install paramiko

Now, we will use the SSHClient from `paramiko` and upload the new file to EC2 instance.

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname=EC2-PUBLIC-IP,username=EC2-USER, key_filename=PATH-TO-PEM-FILE)

sftp = ssh.open_sftp()
sftp.put(localpath='model.pkl', remotepath='/home/ec2-user/model.pkl')
sftp.close()

ssh.close()

Ensure, you replace ‘EC2-PUBLIC-IP’, ‘EC2-USER’, ‘PATH-TO-PEM-FILE’ with appropriate details of your EC2 instance and PEM file.

The above code block connects to the EC2 instance via SSH and then uploads the ‘model.pkl’ file from the local system to EC2 instance.

When updating a ML model, updating the instances might also be necessary. As updating an EC2 instance is a manual process, navigate to your AWS console, identify your EC2 instance and alter its details as required. Saving these changes will finalize your updated EC2 instance setup.

It’s important to remember to manage your resources responsibly to avoid unexpected charges and to maximize efficiency.

Conclusion

In conclusion, this step-by-step guide provided a comprehensive process for deploying machine learning models on the powerful AWS EC2 platform using Python. When correctly utilized, the potent combination of Machine Learning, Python, and Cloud Computing with AWS EC2 redefines the boundaries of what we can achieve in the field of data science. As the landscape of AI and Machine Learning continues to evolve, such knowledge proves instrumental in leveraging these advancements to solve complex problems and deliver optimized solutions. Always remember that consistently monitoring and timely updating of your models are equally important as setting up and deploying them. Do not hesitate to venture, experiment, and optimize, as with every step comes learning and enhancement.

Share This Post

More To Explore

Default Alt Text
AWS

Integrating Python with AWS DynamoDB for NoSQL Database Solutions

This blog provides a comprehensive guide on leveraging Python for interaction with AWS DynamoDB to manage NoSQL databases. It offers a step-by-step approach to installation, configuration, database operation such as data insertion, retrieval, update, and deletion using Python’s SDK Boto3.

Default Alt Text
Computer Vision

Automated Image Enhancement with Python: Libraries and Techniques

Explore the power of Python’s key libraries like Pillow, OpenCV, and SciKit Image for automated image enhancement. Dive into vital techniques such as histogram equalization, image segmentation, and noise reduction, all demonstrated through detailed case studies.

Do You Want To Boost Your Business?

drop us a line and keep in touch