<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Toyyib Muhammad-Jamiu</title>
    <description>The latest articles on DEV Community by Toyyib Muhammad-Jamiu (@nerdrx).</description>
    <link>https://dev.to/nerdrx</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2530092%2Fb696a551-5090-4f11-b641-fd98f7a95e85.jpg</url>
      <title>DEV Community: Toyyib Muhammad-Jamiu</title>
      <link>https://dev.to/nerdrx</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nerdrx"/>
    <language>en</language>
    <item>
      <title>From Notebook to Production: How Amazon SageMaker Simplifies Machine Learning Deployment</title>
      <dc:creator>Toyyib Muhammad-Jamiu</dc:creator>
      <pubDate>Sun, 05 Jan 2025 16:04:21 +0000</pubDate>
      <link>https://dev.to/nerdrx/from-notebook-to-production-how-amazon-sagemaker-simplifies-machine-learning-deployment-ncm</link>
      <guid>https://dev.to/nerdrx/from-notebook-to-production-how-amazon-sagemaker-simplifies-machine-learning-deployment-ncm</guid>
      <description>&lt;h2&gt;
  
  
  INTRODUCTION
&lt;/h2&gt;

&lt;p&gt;Building machine learning models is only part of the journey; deploying them for real-world use is the real challenge. &lt;/p&gt;

&lt;p&gt;Many people are familiar with tools like Streamlit, which is great for creating quick prototypes or demos. However, they lack the scalability and features needed for production-level deployments. &lt;/p&gt;

&lt;p&gt;This is where Amazon SageMaker stands out. It’s designed for building, training, and deploying machine learning models at scale, providing the infrastructure necessary for real-world applications.&lt;/p&gt;

&lt;p&gt;Amazon SageMaker is a fully managed service that helps data scientists, ML practitioners, and AI professionals quickly and easily manage the entire ML workflow; from data preparation and model training to deployment and monitoring. It ensures your models are production-ready, with high availability and scalability built in.&lt;/p&gt;

&lt;p&gt;In this blog, we will explore how Amazon SageMaker offers the tools and features needed to take machine learning models from prototype to production, making it the go-to choice for large-scale deployments.&lt;/p&gt;




&lt;h2&gt;
  
  
  PREREQUISITES
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;AWS Account&lt;/li&gt;
&lt;li&gt;Basic Knowledge of Machine Learning&lt;/li&gt;
&lt;li&gt;Data for Training&lt;/li&gt;
&lt;li&gt;IAM Roles and Permissions: 
Create an IAM role that grants SageMaker access to necessary AWS resources like S3, EC2, and CloudWatch.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Optional but Recommended:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basic Cloud Computing Knowledge: Familiarity with AWS services like S3, EC2, and IAM can make working with SageMaker easier.&lt;/li&gt;
&lt;li&gt;Jupyter Notebooks: Experience using Jupyter notebooks is helpful as SageMaker Studio provides an interactive notebook environment.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  1. Setting Up Your SageMaker Environment
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Navigate to SageMaker Console&lt;/strong&gt;: 
Go to the &lt;strong&gt;AWS Management Console&lt;/strong&gt; and search for &lt;strong&gt;Amazon SageMaker AI&lt;/strong&gt; to get started.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzavk9gwtv98xmzyu3zyl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzavk9gwtv98xmzyu3zyl.png" alt="SageMaker" width="800" height="176"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Create a New Notebook Instance&lt;/strong&gt;: SageMaker provides Jupyter Notebooks, making it easy for practitioners to create, train, and evaluate ML models. &lt;/li&gt;
&lt;li&gt;Click on Notebook instances, then click Create notebook instance.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0ix0n42asw64pfc4ryvu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0ix0n42asw64pfc4ryvu.png" alt="Notebook instance" width="800" height="348"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Choose an appropriate instance type&lt;/strong&gt; (e.g., ml.t2.medium for small workloads).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7phrz4tx4tzi83t6ic1s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7phrz4tx4tzi83t6ic1s.png" alt="Instance type" width="800" height="351"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Set IAM roles and permissions&lt;/strong&gt; to allow SageMaker to access other services (e.g., S3 buckets for data).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ea0xek04almcjg041h7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6ea0xek04almcjg041h7.png" alt="IAM" width="800" height="244"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;For Root Access&lt;/strong&gt;: enabling or disabling it is a function of your use-case.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;a. &lt;strong&gt;For development and experimentation&lt;/strong&gt;, enable root access only if necessary and restrict permissions to trusted users.&lt;/p&gt;

&lt;p&gt;b. &lt;strong&gt;For production-level notebooks&lt;/strong&gt; or environments with sensitive data, disable root access to reduce security risks.&lt;/p&gt;

&lt;p&gt;c. &lt;strong&gt;If unsure&lt;/strong&gt;, start with root access disabled and enable it later if specific requirements arise.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Launch the notebook&lt;/strong&gt; and access it through the console for coding.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  2. Preparing Data for Training
&lt;/h3&gt;

&lt;p&gt;Machine learning begins with data, and SageMaker makes it easy to prepare, clean, and transform data. Here's how:&lt;/p&gt;

&lt;p&gt;a. &lt;strong&gt;Data Storage (Amazon S3)&lt;/strong&gt;: Store your data in Amazon S3 and ensure it is accessible by SageMaker. You can use S3 to upload and organize datasets.&lt;/p&gt;

&lt;p&gt;b. &lt;strong&gt;Data Preprocessing&lt;/strong&gt;: You can preprocess data using SageMaker's built-in Processing Jobs, which allow you to run data transformation tasks in parallel on managed compute resources. &lt;/p&gt;

&lt;p&gt;You can also use pre-built containers or your custom code for tasks like cleaning, normalization, and feature engineering.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; Use Pandas or NumPy for cleaning and preprocessing data directly within your SageMaker notebooks.&lt;/p&gt;

&lt;p&gt;c. &lt;strong&gt;Data Wrangling with SageMaker Data Wrangler&lt;/strong&gt;: Data Wrangler is an interactive tool that allows you to import, clean, and transform data with just a few clicks, providing an intuitive interface for data manipulation. &lt;/p&gt;

&lt;p&gt;It also supports export to S3 for easy integration with the SageMaker training pipeline.&lt;/p&gt;

&lt;p&gt;Steps to Access Data Wrangler&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Launch from SageMaker Studio&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;a. Open SageMaker Studio:&lt;/p&gt;

&lt;p&gt;Go to the Amazon SageMaker Console.&lt;br&gt;
Under "SageMaker Studio," click "Launch SageMaker Studio".&lt;/p&gt;

&lt;p&gt;b. Access Data Wrangler:&lt;br&gt;
In SageMaker Studio, click &lt;strong&gt;File &amp;gt; New&lt;/strong&gt; and select &lt;strong&gt;"Data Wrangler Flow"&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This will open the Data Wrangler interface where you can prepare, analyze, and visualize your data.&lt;/p&gt;


&lt;h3&gt;
  
  
  3. Model Building and Training
&lt;/h3&gt;

&lt;p&gt;Once the data is ready, you can use SageMaker to build and train models efficiently:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Built-in Algorithms&lt;/strong&gt;: SageMaker offers a variety of pre-built, high-performance algorithms (e.g., XGBoost, Linear Learner, K-Means, and Factorization Machines) that are optimized for speed and scalability.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Custom Models&lt;/strong&gt;: You can bring your own code to train models in TensorFlow, PyTorch, MXNet, and other popular frameworks using SageMaker Script Mode or Estimator API. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;SageMaker handles scaling and infrastructure behind the scenes, making it easier to focus on model design.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Distributed Training&lt;/strong&gt;: For large datasets or deep learning tasks, you can use distributed training on SageMaker’s distributed training infrastructure to speed up training times.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Automatic Model Tuning&lt;/strong&gt;: SageMaker offers Hyperparameter Optimization (HPO), allowing you to automatically search for the best hyperparameters for your model, helping improve performance without manual tuning.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt;
  
  
  4. Model Deployment
&lt;/h3&gt;

&lt;p&gt;After training your model, the next step is deployment. SageMaker offers a managed environment for deploying models:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Real-time Inference&lt;/strong&gt;: Deploy your trained model to SageMaker Endpoints for real-time predictions. You can expose an HTTP API that can be called from web or mobile applications. Real-time Inference is best for low-latency, high-availability scenarios like live applications or APIs.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Batch Transform&lt;/strong&gt;: For large datasets or non-real-time predictions, use Batch Transform to run inference on large volumes of data efficiently.It is deal for non-real-time predictions, large datasets, or scheduled batch processing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Multi-Model Endpoints&lt;/strong&gt;: SageMaker now supports multi-model endpoints, allowing you to deploy multiple models on a single endpoint, optimizing resource usage and reducing deployment costs. It is optimized for scenarios where multiple models are required but need to share resources, such as A/B testing or multi-tenant applications.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;h3&gt;
  
  
  5. Model Monitoring and Management
&lt;/h3&gt;

&lt;p&gt;Once deployed, it’s crucial to monitor model performance and manage its lifecycle:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;SageMaker Model Monitor&lt;/strong&gt;: Use Model Monitor to detect data drift, anomalies, and performance degradation. It automatically compares incoming data to the training dataset, alerting you if the model's performance is declining.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;SageMaker Debugger&lt;/strong&gt;: This tool helps track model training metrics and helps debug your model in real-time, allowing you to make adjustments and improve performance before deployment.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;SageMaker Pipelines&lt;/strong&gt;: Automate and manage the end-to-end ML lifecycle, from data preparation to model deployment. This helps maintain consistency in deployment and model training workflows.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Below is a schematic diagram showing the Amazon Sagemaker Workflow that manages the end-to-end ML lifecycle.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn5annwlt95r3k17wnfog.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn5annwlt95r3k17wnfog.jpg" alt="Workflow" width="800" height="424"&gt;&lt;/a&gt;&lt;/p&gt;


&lt;h3&gt;
  
  
  6. Collaborating with Teams
&lt;/h3&gt;

&lt;p&gt;Data science and ML projects often involve collaboration. SageMaker enables teams to work together on various stages of the ML lifecycle:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;SageMaker Studio&lt;/strong&gt;: SageMaker Studio is an integrated development environment (IDE) that provides a unified visual interface for data science and machine learning workflows. It allows you to access notebooks, manage code, and track experiments all in one place.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Version Control&lt;/strong&gt;: You can use Git integration within SageMaker to manage and version control your models and notebooks, allowing easy collaboration.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How to Integrate Version Control for easy collaboration.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;a. &lt;strong&gt;Git Integration in SageMaker Studio:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;i. Open SageMaker Studio.&lt;br&gt;
ii. Navigate to the &lt;strong&gt;"File Browser"&lt;/strong&gt; or &lt;strong&gt;"Launcher"&lt;/strong&gt; and select the &lt;strong&gt;terminal&lt;/strong&gt;.&lt;br&gt;
iii. Configure Git using terminal commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;iv. Clone a Repository:&lt;/p&gt;

&lt;p&gt;Use the terminal to clone a Git repository:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone https://github.com/your-repo.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can now work on notebooks or files within the cloned repository.&lt;/p&gt;

&lt;p&gt;v. Commit and Push Changes:&lt;br&gt;
After making changes, use standard Git commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git add .
git commit -m "Your commit message"
git push origin main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;vi. Track Changes:&lt;/p&gt;

&lt;p&gt;Collaborators can pull updates or resolve conflicts using Git commands directly within SageMaker Studio.&lt;/p&gt;

&lt;p&gt;b. &lt;strong&gt;Git Integration in SageMaker Notebook Instances&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For notebook instances, you can also manually integrate Git by installing it and running Git commands through the terminal.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to Enable Git in Notebook Instances:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;i.  Launch a notebook instance and open the terminal.&lt;br&gt;
ii. Install Git if not already installed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo yum install git -y
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;iii. Configure your Git credentials:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git config --global user.name "Your Name"
git config --global user.email "youremail@example.com"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;iv. Clone a repository and manage version control as you would on any local machine.&lt;/p&gt;

&lt;p&gt;c. &lt;strong&gt;Using AWS CodeCommit for Managed Git Repositories&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you want to use an AWS-native Git solution, SageMaker can integrate with AWS CodeCommit, a fully managed source control service.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps:&lt;/strong&gt;&lt;br&gt;
i.  Create a repository in CodeCommit.&lt;br&gt;
ii. Clone the repository in SageMaker Studio or a notebook instance.&lt;br&gt;
iii. Use Git commands to manage and version control files.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SageMaker Projects&lt;/strong&gt;: This feature helps you set up repeatable machine learning projects with pre-defined templates, improving collaboration and workflow consistency.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  7. Cost Optimization with SageMaker
&lt;/h3&gt;

&lt;p&gt;SageMaker also provides several features to help optimize costs during development and deployment:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Spot Instances&lt;/strong&gt;: Use SageMaker Managed Spot Training to reduce training costs by up to 90%. Spot instances allow you to take advantage of unused EC2 capacity at a reduced cost, though they can be interrupted.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Instance Types&lt;/strong&gt;: Choose the right instance size and type based on your workload. For smaller tasks, start with &lt;strong&gt;ml.t2.medium&lt;/strong&gt;, and scale up to more powerful instances (e.g., ml.p3.2xlarge) for larger workloads.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Model Optimization&lt;/strong&gt;: Use &lt;strong&gt;SageMaker’s Neo&lt;/strong&gt; feature to optimize machine learning models for faster inference at a lower cost, without sacrificing accuracy.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  CONCLUSION
&lt;/h2&gt;

&lt;p&gt;Amazon SageMaker is an incredibly powerful and flexible tool for data scientists and AI/ML practitioners, helping you streamline every aspect of the machine learning lifecycle, from data preparation and model building to deployment and management. &lt;/p&gt;

&lt;p&gt;It provides a vast range of features, including pre-built algorithms, powerful training environments, and easy-to-use deployment options, making it easier for you to create, optimize, and deploy AI/ML solutions at scale.&lt;/p&gt;

&lt;p&gt;With its integrated tools for monitoring, collaboration, and cost optimization, SageMaker is designed to accelerate AI/ML workflows while helping you manage costs efficiently.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>datascience</category>
      <category>amazonsagemaker</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Optimizing AWS Costs: Practical Tips for Budget-Conscious Cloud Engineers</title>
      <dc:creator>Toyyib Muhammad-Jamiu</dc:creator>
      <pubDate>Sun, 05 Jan 2025 13:09:50 +0000</pubDate>
      <link>https://dev.to/nerdrx/optimizing-aws-costs-practical-tips-for-budget-conscious-cloud-engineers-1d00</link>
      <guid>https://dev.to/nerdrx/optimizing-aws-costs-practical-tips-for-budget-conscious-cloud-engineers-1d00</guid>
      <description>&lt;h2&gt;
  
  
  INTRODUCTION
&lt;/h2&gt;

&lt;p&gt;AWS provides a flexible cloud environment, but this flexibility can lead to significant costs if not carefully managed. &lt;/p&gt;

&lt;p&gt;Cost optimization is about aligning resources and architecture with application needs while leveraging AWS' pricing options to minimize expenses.&lt;/p&gt;

&lt;p&gt;Cost optimization in AWS requires a strategic approach to resource management, pricing options, and architectural choices. &lt;/p&gt;

&lt;p&gt;Below is a list of practical steps with actionable tips to help reduce AWS bills.&lt;/p&gt;




&lt;h3&gt;
  
  
  1. Use AWS Cost Management Tools
&lt;/h3&gt;

&lt;p&gt;AWS provides a set of cost management tools that help monitor and analyze expenses:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;AWS Cost Explorer&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;AWS Cost Explorer helps visualize AWS costs and usage patterns over time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps involved in using AWS Cost Explorer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;a. Go to &lt;strong&gt;AWS Billing Console &amp;gt; Cost Explorer&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs7en0egas8oi1fdpobwk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs7en0egas8oi1fdpobwk.png" alt="Cost Explorer" width="800" height="521"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;b. Set custom date ranges, filter by services or accounts, and visualize costs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo3hky26n7vu0bck0days.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo3hky26n7vu0bck0days.png" alt="Custom Dates" width="668" height="1066"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;c. Use Cost Explorer’s forecast feature to predict future expenses based on historical usage.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fry5sldol8fajzaz37n2a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fry5sldol8fajzaz37n2a.png" alt="Forecasted values" width="654" height="422"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Regularly review Cost Explorer reports to identify and eliminate unnecessary resources.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;AWS Budgets&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;AWS Budgets allow you to set custom cost and usage budgets.&lt;/p&gt;

&lt;p&gt;Steps involved:&lt;/p&gt;

&lt;p&gt;a. In the Billing Console, navigate to &lt;strong&gt;Budgets&lt;/strong&gt; and choose &lt;strong&gt;Create a Budget&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqv6mvxryuy0vo38vgdwo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqv6mvxryuy0vo38vgdwo.png" alt="AWS Budget" width="800" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There are several budget templates here:&lt;br&gt;
i. &lt;strong&gt;Zero-Spend Budget&lt;/strong&gt;: Suitable for users on Free-tier. &lt;/p&gt;

&lt;p&gt;It creates a budget that notifies you once your spending exceeds $0.01 which is above the AWS Free Tier limits.&lt;/p&gt;

&lt;p&gt;ii. &lt;strong&gt;Monthly Cost Budget&lt;/strong&gt;: It creates a monthly budget that notifies you if you exceed, or are forecasted to exceed, the budget amount.&lt;/p&gt;

&lt;p&gt;iii. &lt;strong&gt;Daily Savings Plans coverage budget&lt;/strong&gt;: It creates a coverage budget for your savings plans that notifies you when you fall below the defined target.&lt;/p&gt;

&lt;p&gt;iv. &lt;strong&gt;Daily reservation utilization budget&lt;/strong&gt;:&lt;br&gt;
Create a utilization budget for your reservations that notifies you when you fall below the defined target.&lt;/p&gt;

&lt;p&gt;You can also customize your budget yourself rather than using these set templates by opting for the &lt;strong&gt;Customize&lt;/strong&gt; option.&lt;/p&gt;

&lt;p&gt;b. Set a budget amount, specify cost thresholds, and set up alerts.&lt;/p&gt;

&lt;p&gt;Taking Zero-Spend as an example, the budget is already set at $0.00 and the cost threshold is $0.01. &lt;/p&gt;

&lt;p&gt;You can give your Zero-spend budget a name and enable an email notification by adding an email address to notify you whenever you go beyond the AWS Free-tier limit.&lt;/p&gt;

&lt;p&gt;Then click on Create budget.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxko1bai4pj5qydksosul.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxko1bai4pj5qydksosul.png" alt="Zero-spend" width="800" height="362"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Use AWS Budgets to enforce a spending cap across teams or projects.&lt;/p&gt;




&lt;h3&gt;
  
  
  2. Rightsize Instances with AWS Compute Optimizer
&lt;/h3&gt;

&lt;p&gt;AWS Compute Optimizer suggests instance sizes based on your workloads, helping you select the optimal EC2 instance type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Steps to Use Compute Optimizer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the AWS Management Console, go to &lt;strong&gt;AWS Compute Optimizer&lt;/strong&gt;.&lt;br&gt;
Select &lt;strong&gt;Get Started&lt;/strong&gt;,  &lt;strong&gt;Opt in for Only this Account&lt;/strong&gt;, then &lt;strong&gt;EC2 Instances&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fghf7hh29ngswqodnd49i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fghf7hh29ngswqodnd49i.png" alt="Compute Optimizer" width="800" height="357"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Review instance recommendations, which are categorized as &lt;strong&gt;under-provisioned&lt;/strong&gt;, &lt;strong&gt;over-provisioned&lt;/strong&gt;, or &lt;strong&gt;optimally provisioned.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbs8p15dhdbgdrkiuhn61.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbs8p15dhdbgdrkiuhn61.png" alt="Recommendation" width="800" height="327"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Resize or change instance types based on the recommendations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Regularly review recommendations to resize instances as needs change. Switching to the recommended instance size can lead to considerable cost savings.&lt;/p&gt;




&lt;h3&gt;
  
  
  3. Use Reserved Instances and Savings Plans
&lt;/h3&gt;

&lt;p&gt;Reserved Instances (RIs) and Savings Plans provide significant discounts in exchange for a commitment to a specific usage amount or duration.&lt;/p&gt;

&lt;p&gt;Reserved Instances&lt;br&gt;
What they are: Purchase commitments for EC2 instances at reduced rates for 1- or 3-year terms.&lt;/p&gt;

&lt;p&gt;How to purchase:&lt;br&gt;
In the &lt;strong&gt;EC2 Console&lt;/strong&gt;, navigate to &lt;strong&gt;Reserved Instances&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Choose an instance type, region, and term (1 or 3 years).&lt;/p&gt;

&lt;p&gt;Select a payment plan (&lt;strong&gt;All Upfront&lt;/strong&gt;, &lt;strong&gt;Partial Upfront&lt;/strong&gt;, or &lt;strong&gt;No Upfront&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Use RIs for predictable, consistent workloads.&lt;/p&gt;




&lt;h3&gt;
  
  
  4. Savings Plans
&lt;/h3&gt;

&lt;p&gt;Savings Plans offer flexible pricing models with a commitment to specific usage for 1 or 3 years across EC2, Lambda, and Fargate.&lt;br&gt;
It is part of the budget templates that was briefly discussed under the AWS Budget Section above.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to set up:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;a. Go to &lt;strong&gt;AWS Billing Console &amp;gt; Savings Plans&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;b. Choose a &lt;strong&gt;Compute Savings Plan&lt;/strong&gt; (for any region and instance family) or &lt;strong&gt;EC2 Instance Savings Plan&lt;/strong&gt; (specific to instance family in a region).&lt;/p&gt;

&lt;p&gt;Set your hourly commitment and term length.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Choose Savings Plans if you have variable workloads that span multiple services.&lt;/p&gt;




&lt;h3&gt;
  
  
  5. Implement Auto Scaling to Match Demand
&lt;/h3&gt;

&lt;p&gt;Auto Scaling dynamically adjusts resources based on actual demand, reducing costs by releasing resources during low demand periods.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting Up Auto Scaling for EC2&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;a. In the EC2 Console, go to &lt;strong&gt;Auto Scaling Groups &amp;gt; Create Auto Scaling Group&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv2w3sqc0mb1rgoharwlf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv2w3sqc0mb1rgoharwlf.png" alt="ASG" width="800" height="349"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Under  &lt;strong&gt;Launch Template &amp;gt; Create Launch Template&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fki8kdb24h4lz9cdtvr7p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fki8kdb24h4lz9cdtvr7p.png" alt="Launch template" width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, while creating the Launch template, select the Operating System of your choice &lt;strong&gt;e.g., Ubuntu&lt;/strong&gt; and instance type.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flsrh9dlkir8emesyli6s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flsrh9dlkir8emesyli6s.png" alt="Operating system" width="800" height="442"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fywbov8rv1zo5pxp9kh26.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fywbov8rv1zo5pxp9kh26.png" alt="Instance type" width="800" height="351"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;b. Use the Launch template you just created and configure the minimum, desired, and maximum instance counts.&lt;/p&gt;

&lt;p&gt;c. Configure scaling policies to define triggers, such as CPU utilization or network traffic.&lt;/p&gt;

&lt;p&gt;d. Set up notifications for scale-in and scale-out events.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Use Auto Scaling with Spot Instances for even more savings (see below for details).&lt;/p&gt;




&lt;h3&gt;
  
  
  6. Use Spot Instances for Non-Critical Workloads
&lt;/h3&gt;

&lt;p&gt;AWS Spot Instances offer spare capacity at up to a 90% discount, ideal for flexible, fault-tolerant workloads.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Spot Instances:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;a. In the EC2 Console, choose Launch Instance.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Select the instance type and configure it as a Spot Instance under Purchase Options.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Set the maximum bid price for the instance.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;b. Launch and monitor spot requests in the Spot Requests section.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Use Spot Instances for batch jobs, data processing, or other non-time-sensitive tasks. &lt;/p&gt;

&lt;p&gt;Combine Spot Instances with Auto Scaling to automatically manage instance availability.&lt;/p&gt;




&lt;h3&gt;
  
  
  7. Optimize Storage Costs with S3 and EBS
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;S3 Storage Classes&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Standard: For frequently accessed data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Intelligent-Tiering: Automatically moves data to the most cost-effective tier based on access patterns.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Infrequent Access (IA): For less-frequently accessed data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Glacier and Glacier Deep Archive: For archival data.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Set up Lifecycle Policies in the S3 Console to transition objects to cheaper storage classes as they age.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;EBS Volume Optimization&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;a. Delete Unused Volumes: Regularly review and delete any unattached EBS volumes.&lt;/p&gt;

&lt;p&gt;b. Use Snapshots for Backups: Use snapshots for backup rather than keeping idle volumes.&lt;/p&gt;

&lt;p&gt;c. Right-Size EBS Volumes: Choose the storage type and size that align with performance needs, e.g., switching from Provisioned IOPS SSDs to General Purpose SSDs for less intensive workloads.&lt;/p&gt;




&lt;h3&gt;
  
  
  8. Leverage AWS Lambda for Serverless Architectures
&lt;/h3&gt;

&lt;p&gt;AWS Lambda enables a serverless approach, where you only pay for the compute time you use. This can lead to significant cost savings, especially for intermittent workloads.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Creating a Lambda Function&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;a. In the Lambda Console, click Create Function.&lt;/p&gt;

&lt;p&gt;b. Choose Author from scratch, name the function, and select the runtime.&lt;/p&gt;

&lt;p&gt;c. Define the function's Memory and Timeout settings, optimizing for performance and cost.&lt;/p&gt;

&lt;p&gt;d. Test and deploy the function.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Use Provisioned Concurrency only if necessary. Lambda functions billed per millisecond can be more cost-effective for many small tasks.&lt;/p&gt;




&lt;h3&gt;
  
  
  9. Monitor and Reduce Data Transfer Costs
&lt;/h3&gt;

&lt;p&gt;AWS charges for data transfer between regions and VPCs, which can accumulate quickly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tips to Minimize Data Transfer Costs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;a. Use Same Region Resources: Whenever possible, keep resources within the same region to minimize data transfer costs.&lt;/p&gt;

&lt;p&gt;b. Use Amazon CloudFront: Leverage CloudFront for content delivery to reduce cross-region transfer costs.&lt;/p&gt;

&lt;p&gt;c. Implement VPC Peering: If data transfer between VPCs is necessary, use VPC peering or AWS Transit Gateway to lower costs.&lt;/p&gt;




&lt;h3&gt;
  
  
  10. Regularly Clean Up Idle or Unused Resources
&lt;/h3&gt;

&lt;p&gt;Unused resources can quietly accumulate costs over time, so regularly auditing your AWS account can save money.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Checklist for Resource Cleanup&lt;/li&gt;
&lt;li&gt;Delete Unused EC2 Instances and EBS Volumes.&lt;/li&gt;
&lt;li&gt;Remove Old Snapshots and Backups.&lt;/li&gt;
&lt;li&gt;Terminate Idle RDS Instances.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Delete Unnecessary Elastic IPs: AWS charges for Elastic IPs not attached to running instances.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Set up automated scripts or use AWS Lambda to periodically check for and delete unused resources.&lt;/p&gt;




&lt;h2&gt;
  
  
  CONCLUSION
&lt;/h2&gt;

&lt;p&gt;Optimizing AWS costs is an ongoing process that combines strategic use of AWS tools, pricing models, and automated practices. &lt;/p&gt;

&lt;p&gt;Implementing the tips and techniques outlined here can help budget-conscious cloud engineers make the most of AWS resources without overspending. &lt;/p&gt;

&lt;p&gt;Regularly review your setup to ensure your infrastructure remains optimized as workloads and requirements evolve.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>devops</category>
      <category>cloud</category>
      <category>lambda</category>
    </item>
    <item>
      <title>Building AI-Powered Real-Time Object Detection with OpenCV, Flask, Gemini Vision, and GitHub Actions CI/CD on AWS EC2</title>
      <dc:creator>Toyyib Muhammad-Jamiu</dc:creator>
      <pubDate>Mon, 16 Dec 2024 21:31:09 +0000</pubDate>
      <link>https://dev.to/nerdrx/building-ai-powered-real-time-object-detection-with-opencv-flask-gemini-vision-and-github-25lj</link>
      <guid>https://dev.to/nerdrx/building-ai-powered-real-time-object-detection-with-opencv-flask-gemini-vision-and-github-25lj</guid>
      <description>&lt;h2&gt;
  
  
  INTRODUCTION:
&lt;/h2&gt;

&lt;p&gt;Imagine a system that can analyze live video feeds in real time, interpret scenes, and respond intelligently to questions about the environment, just like a virtual assistant with eyes. &lt;/p&gt;

&lt;p&gt;This is the potential of combining cutting-edge technologies like OpenCV for video processing and Google's Gemini vision model, leveraging its latest "gemini-1.5-flash-latest" model.&lt;/p&gt;

&lt;p&gt;In this article, I will guide you through building a Real-Time Object Detection System that uses live video streaming and AI-powered scene analysis to deliver insightful, context-aware responses. &lt;/p&gt;

&lt;p&gt;We'll deploy the application on AWS EC2, setting the stage for scalability and real-world use while employing Github Actions for automated CI/CD, ensuring a seamless update pipeline.&lt;/p&gt;

&lt;p&gt;By the end of this tutorial, you'll have a fully functional AI-powered system ready for deployment, with the confidence to expand and customize it for various use cases.&lt;/p&gt;




&lt;h2&gt;
  
  
  PROJECT STRUCTURE
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;project/
├── app.py              # Flask application code
├── requirements.txt    # Python dependencies
├── templates/
│   └── index.html     # Frontend UI
└── .env               # Environment variables (API keys, etc.)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Core Components
&lt;/h2&gt;

&lt;p&gt;A. Real-Time Video Capture (OpenCV)&lt;br&gt;
The WebcamCapture class in app.py handles video streaming:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;self.stream = cv2.VideoCapture(0)  # Open the default webcam
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ensures efficient, thread-safe frame capture and processing.&lt;/p&gt;

&lt;p&gt;B. AI-Powered Object Detection (Google Gemini)&lt;br&gt;
Using the Gemini model, we analyze frames for real-time scene understanding:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;self.model = ChatGoogleGenerativeAI(model="gemini-1.5-flash-latest")
response = self.chain.invoke({"prompt": prompt, "image_base64": image_base64})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;C. Flask Backend&lt;br&gt;
The Flask application provides endpoints for video streaming, AI queries, and system status checks:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/video_feed&lt;/code&gt;: Streams live video.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;/process_query&lt;/code&gt;: Handles AI-powered analysis based on user input and video frames.&lt;/p&gt;

&lt;p&gt;D. Frontend UI&lt;br&gt;
The &lt;code&gt;index.html&lt;/code&gt; file provides a responsive web interface for interacting with the system. It captures user queries and displays real-time AI responses.&lt;/p&gt;


&lt;h2&gt;
  
  
  PREREQUISITES
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;An AWS account.&lt;/li&gt;
&lt;li&gt;A registered domain name (e.g., example.com).&lt;/li&gt;
&lt;li&gt;A Google Cloud Account or Open AI account&lt;/li&gt;
&lt;li&gt;GitHub actions configured in your repository.&lt;/li&gt;
&lt;li&gt;Basic knowledge of SSH and Linux command-line tools.&lt;/li&gt;
&lt;/ol&gt;


&lt;h2&gt;
  
  
  APPLICATION DEPLOYMENT
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Step 1: Clone the Repository, Generate the Google Gemini API &amp;amp; Push the application files to Github
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;A. Clone the repository&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git clone https://github.com/Abunuman/Real-Time-ODS.git
$ cd Real-Time-ODS
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;B. Generate your API key and add to a .env file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;i. Create a .env file&lt;/strong&gt; either manually from the options available on the left-hand side of your text editor (I used VScode)&lt;br&gt;
OR&lt;br&gt;
On the terminal, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ touch .env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then add these in the .env&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;GOOGLE_API_KEY=your_google_api_key
OPENAI_API_KEY=your_openai_api_key
FLASK_DEBUG=True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;ii. Log into Google Cloud&lt;/strong&gt; and follow these steps to generate your API key.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg9mjw1vgqq2skctb3w6e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg9mjw1vgqq2skctb3w6e.png" alt="Google Cloud Interface" width="800" height="404"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;a. Navigate to the API &amp;amp; Services Section&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe783q2n19i5kxonlqz0d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe783q2n19i5kxonlqz0d.png" alt="API &amp;amp; Services" width="800" height="399"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;b. Click on Credentials then follow the other steps below&lt;/p&gt;

&lt;p&gt;Create Credentials &amp;gt; API Key , then the API Key is generated.&lt;/p&gt;

&lt;p&gt;Remember to note the name of your API key. You can also give it a name during the process.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5urzhl3lwh9p2oat6dt0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5urzhl3lwh9p2oat6dt0.png" alt="Create Credentials" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqnkh6vgbzvs991sn5nd1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqnkh6vgbzvs991sn5nd1.png" alt="API key" width="800" height="215"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Copy the API Key generated, go back to your .env file and replace &lt;code&gt;your_google_api_key&lt;/code&gt; with the key you just copied.&lt;/p&gt;

&lt;p&gt;c. Enable Gemini API &lt;br&gt;
Search for Gemini API and click on &lt;strong&gt;ENABLE&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7p1ehy7t803j20wz0x1d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7p1ehy7t803j20wz0x1d.png" alt="Enable Gemini API" width="800" height="276"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Confirm that your API Key is under the &lt;strong&gt;METRICS&lt;/strong&gt; and Credentials section under the Enabled Gemini API.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd807ov33szz4cw08jago.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd807ov33szz4cw08jago.png" alt="API-keys metrics" width="800" height="257"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;iii. Create a .gitignore file&lt;/strong&gt; and add .env to the file so that it is not pushed to github.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6cqpoycrixeis3yx5piw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6cqpoycrixeis3yx5piw.png" alt=".gitignore" width="800" height="206"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;N.B.: Standard practice is to ensure that secrets and environment variables are not exposed to the public. Hence the need for a .gitignore to ignore files added therein while pushing to Github.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C. Push to Repository.&lt;/strong&gt;&lt;br&gt;
i. Create a Github repository with the application name and follow the commands below to push to github&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ git init
$ git add .
$ git commit -m "first commit"
$ git branch -M main
$ git remote add origin https://github.com/Abunuman/repository-name.git
git push -u origin main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;N.B: change &lt;code&gt;repository-name&lt;/code&gt; to your repository name&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 2: Setting Up AWS EC2 Instance
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;i. Launch an EC2 Instance&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use the AWS Management Console to launch an EC2 instance (e.g., Ubuntu 22.04).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2emajtjkrh4p8x4hqqp4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2emajtjkrh4p8x4hqqp4.png" alt="Launch instance" width="800" height="346"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1l8y70t6lafqno6k79hw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1l8y70t6lafqno6k79hw.png" alt="Ubuntu" width="800" height="354"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Select an instance type (e.g., t2.micro for free tier users).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe78xjds2wnca6ajaxk3d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe78xjds2wnca6ajaxk3d.png" alt="Instance type" width="800" height="199"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create and download a key pair (.pem file) for SSH access.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Create a new key pair or use an existing one. &lt;/p&gt;

&lt;p&gt;If you are creating a new key pair, click on create key pair and give it a name of your choice. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F119k6dukipk6ff3aag7c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F119k6dukipk6ff3aag7c.png" alt="create key pair" width="800" height="178"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Select Key Pair type as RSA&lt;/p&gt;

&lt;p&gt;File format as .pem&lt;/p&gt;

&lt;p&gt;Then Create the key&lt;/p&gt;

&lt;p&gt;The key pair is automatically downloaded to your system.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Configure Security Groups&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fus22hzd12v6si1cvcogf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fus22hzd12v6si1cvcogf.png" alt="Security group" width="800" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Allow the following inbound rules:&lt;/p&gt;

&lt;p&gt;a. HTTP (port 80): For serving your application.&lt;br&gt;
b. HTTPS (port 443): For secure access.&lt;br&gt;
c. SSH (port 22): For access management.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fytbpos1gczoh2kid0s77.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fytbpos1gczoh2kid0s77.png" alt="Inbound rules" width="800" height="519"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Click on Launch instance and allow the instance to be fully launched.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now your instance is ready to use once the status shows “Running”.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ii. Configure the key pair (.pem key) for SSH access&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For Mac book users or Linux users with bash terminal, configure your key pair for SSH access thus:&lt;/p&gt;

&lt;p&gt;a. Open the downloaded .pem key using VScode or Xcode&lt;/p&gt;

&lt;p&gt;b. On your terminal, navigate to the .ssh directory from the root directory(~)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ cd .ssh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;c. Create a  .pem file in the .ssh directory using nano or vim text editors; I will be using nano in this tutorial.&lt;/p&gt;

&lt;p&gt;Install nano if you don't have it installed.&lt;/p&gt;

&lt;p&gt;For macbook users&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ brew install nano
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For linux users&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt install nano
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Having installed it, create the .pem file in the .ssh directory using nano.&lt;/p&gt;

&lt;p&gt;Ensure the file to be created bears the exact name of your .pem file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo nano name_of_pem.pem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then copy the already opened .pem file and paste in the .pem to be created in the .ssh directory.&lt;/p&gt;

&lt;p&gt;Press Ctrl X, then Y , then Enter to save.&lt;/p&gt;

&lt;p&gt;d. Change the .pem file permission&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ chmod 400 name_of_pem.pem
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;iii. Access the Instance&lt;/strong&gt;  - SSH into your EC2 instance:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Click on the Instance ID . Once the instance is in the running state, select on the connect option&lt;/p&gt;

&lt;p&gt;Once you are at the Connect page , Go to SSH Client&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1rqytc28zco44rs4xpnj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1rqytc28zco44rs4xpnj.png" alt="SSH Client" width="800" height="340"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Then copy the last command on the page that looks like this:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ssh -i path/to/key.pem ubuntu@&amp;lt;ec2-public-ip&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Paste this on your terminal and press enter. You should connect seamlessly.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;For Windows Users&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Windows Setup&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Open CMD on your windows machine&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Locate desired directory where .pem file is stored&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ideally from this directory , we can run the copied ssh command and we should be able to connect to EC2.&lt;/p&gt;

&lt;p&gt;However, sometimes we get a security permissions error when we run the ssh command.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We have to change the permissions to the .pem file. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For that follow the steps below:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Locate the .pem file folder , right click on the file and select properties&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to Security tab&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to Advanced tab&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click Disable inheritance&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This Advance options also shows other user having all permissions to .pem file. Remove permission for all other users&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Add the user with which you are trying to connect to EC2 if not already present in the user list.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enable all permissions for this user.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Ideally with these steps, you should not encounter an error.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Run the SSH command from CMD prompt&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once the permissions are fixed , prompt will successfully connect to EC2&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, you have successfully completed the steps and you can run commands from windows CMD on EC2 instance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;iv.Install Dependencies&lt;/strong&gt; - Update the package list and install necessary packages:&lt;/p&gt;

&lt;p&gt;Having connected to your EC2 instance via SSH, install dependencies on EC2. &lt;/p&gt;

&lt;p&gt;On your connected terminal, run the following commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo apt update
$ sudo apt install -y python3 python3-pip nginx

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check the version of python3 installed, ensure its 3.12&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python3 --version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Step 3: Configuring GitHub Actions for CI/CD
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;A. Set up Github Actions Environment Secrets&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Configure your AWS IAM user secrets and environment variables needed for the project.&lt;/p&gt;

&lt;p&gt;Set GitHub Secrets in your repository, navigate to Settings &amp;gt; Secrets and variables &amp;gt; Actions, and add:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;EC2_USERNAME&lt;/code&gt; (e.g., ubuntu).&lt;br&gt;
&lt;code&gt;EC2_HOST&lt;/code&gt; (your EC2 public IP or domain).&lt;br&gt;
&lt;code&gt;EC2_SSH_KEY&lt;/code&gt; (path to your private key file).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;i. Navigate to Settings in your repository&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2klqp2cg71g7ich9gxuz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2klqp2cg71g7ich9gxuz.png" alt="Github Settings" width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ii. Click on Secrets and Variables, then Actions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flv3c7xystkq7gcbsvt14.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flv3c7xystkq7gcbsvt14.png" alt="Secrets and Variables" width="800" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;iii. Add your Secrets and Variables like below&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhaklga4nw3sfw3b1zvw0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhaklga4nw3sfw3b1zvw0.png" alt="Secrets" width="800" height="455"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqei9u3ooe9eql2ffm7lb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqei9u3ooe9eql2ffm7lb.png" alt="secret2" width="800" height="123"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;B. Create a Workflow File in your repository, add a .github/workflows/main.yml file:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Navigate to Actions under your repository and click on &lt;strong&gt;set up a workflow yourself&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwquzbzl9bfpbqn24mpmd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwquzbzl9bfpbqn24mpmd.png" alt="Set workflow" width="800" height="406"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Then add this to the space provided&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;name: Deploy to AWS
on:
  push:
    branches: [main]
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      # Check out the repository
      - uses: actions/checkout@v4

      # Set up Python environment
      - name: Set up Python 3.12  
        uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      # Configure AWS credentials
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: ${{ secrets.AWS_REGION }}

      # Copy files to EC2 instance
      - name: Transfer files to EC2
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.EC2_HOST }}
          username: ${{ secrets.EC2_USERNAME }}
          key: ${{ secrets.EC2_SSH_KEY }}
          source: "app.py,requirements.txt,templates/index.html"
          target: "/home/${{ secrets.EC2_USERNAME }}/app"

      # SSH to EC2 and set up the app
      - name: Deploy to EC2
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.EC2_HOST }}
          username: ${{ secrets.EC2_USERNAME }}
          key: ${{ secrets.EC2_SSH_KEY }}
          script: |
            # Create and navigate to the app directory
            cd /home/${{ secrets.EC2_USERNAME }}/app

            # Ensure system dependencies are installed
            sudo apt-get update
            sudo apt-get install -y python3-venv python3-pip git libopencv-dev python3-opencv portaudio19-dev ffmpeg

            # Setup Python virtual environment
            if [ ! -d "venv" ]; then
              python3 -m venv venv
            fi

            # Activate virtual environment and install dependencies
            source venv/bin/activate
            pip install --upgrade pip
            pip install -r requirements.txt

            # Ensure templates directory exists and move files if needed
            mkdir -p templates

            # Create or update systemd service
            sudo tee /etc/systemd/system/flask_app.service &amp;lt;&amp;lt; EOF
            [Unit]
            Description=Flask App
            After=network.target

            [Service]
            User=${{ secrets.EC2_USERNAME }}
            WorkingDirectory=/home/${{ secrets.EC2_USERNAME }}/app
            Environment="PATH=/home/${{ secrets.EC2_USERNAME }}/app/venv/bin"
            ExecStart=/home/${{ secrets.EC2_USERNAME }}/app/venv/bin/python app.py

            [Install]
            WantedBy=multi-user.target
            EOF

            # Reload and restart the service
            sudo systemctl daemon-reload
            sudo systemctl enable flask_app
            sudo systemctl restart flask_app

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Having done this, run the CI/CD and check your terminal connected to EC2 to check if the files are now moved to EC2.&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 4: Acquiring an SSL Certificate with AWS ACM
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;A. Request a Certificate&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Navigate to AWS Certificate Manager in the AWS Console.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Click Request a certificate and choose Request a public certificate.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;B. Enter your domain name&lt;/strong&gt; (e.g., example.com)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C. Choose DNS validation and complete the request.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;D. Validate the Certificate&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;ACM will provide a CNAME record.&lt;/p&gt;

&lt;p&gt;Add this record in your domain’s Route 53 hosted zone.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;E. Verify the Status&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Once validated, the certificate status will change to Issued.&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 5: Managing a Custom Domain with AWS Route 53
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;A. Navigate to AWS Route 53 in the AWS Console&lt;/strong&gt; - Create a Hosted Zone for your domain&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F17xmec19l84rk5v8ehno.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F17xmec19l84rk5v8ehno.png" alt="Hosted zone" width="800" height="346"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;B. Update Domain Name Servers&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Copy the nameservers (NS) from the hosted zone and update the NS records in your domain registrar’s settings.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6dztnh8vrrdr0gmyolww.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6dztnh8vrrdr0gmyolww.png" alt="Nameservers" width="762" height="294"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C. Add an A Record&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create an A record pointing to your EC2 instance's public IP.&lt;/p&gt;

&lt;p&gt;e.g realtime.example.com (realtime as sub-domain)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft0z011gu0d17mdqybd4v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft0z011gu0d17mdqybd4v.png" alt="A-record" width="800" height="330"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;D. Add a CNAME for SSL&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Add a CNAME record from ACM for DNS validation if not already done.&lt;/p&gt;

&lt;p&gt;This can be created during your SSL certificate generation by clicking on the CNAME option and it redirects you to AWS Route53 and its created for the domain.&lt;/p&gt;

&lt;p&gt;You can also create it under AWS Route 53 by going through similar steps followed in creating an A record.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff2l7fslwexptgkggej7k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ff2l7fslwexptgkggej7k.png" alt="CNAME" width="800" height="67"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Step 6: Configure NGINX for reverse proxy
&lt;/h3&gt;

&lt;p&gt;Configure NGINX for reverse proxy&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A. Edit nginx sites available for flask app&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Navigate to /etc/nginx/sites-available/flask_app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo nano /etc/nginx/sites-available/flask_app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;B. Add this configuration&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

# HTTP redirect server block
server {
    listen 80;
    server_name your_a_record_domain.com;
    # Only redirect if not already HTTPS
    if ($http_x_forwarded_proto != 'https') {
        return 301 https://$server_name$request_uri;
    }

    location / {
        proxy_pass http://localhost:5000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

        # Increased timeouts for streaming
        proxy_read_timeout 3600s;
        proxy_send_timeout 3600s;
    }
}

# Main HTTPS server block
server {
    listen 443;
    server_name your_a_record_domain.com.com;

    # Increase timeout settings
    proxy_connect_timeout 600;
    proxy_send_timeout 600;
    proxy_read_timeout 600;
    send_timeout 600;

    # Prevent timeout for video streaming
    keepalive_timeout 650;
    keepalive_requests 10000;
    client_max_body_size 50M;

    location /video_feed {
        proxy_pass http://localhost:5000;
        proxy_buffering off;
        proxy_cache off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_http_version 1.1;
        proxy_set_header Connection '';
        proxy_read_timeout 24h;
        proxy_send_timeout 24h;
        proxy_connect_timeout 24h;
    }

    location /process_frame {
        proxy_pass http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_cache off;
        proxy_buffering off;
    }

    location / {
        proxy_pass http://localhost:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;

    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;C. Restart nginx and flask app&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo systemctl restart nginx
$ sudo systemctl restart flask_app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Step 7: Create an Application Load Balancer
&lt;/h3&gt;

&lt;p&gt;To secure your application with HTTPS using an SSL certificate from AWS Certificate Manager (ACM), you need to link the SSL certificate to your domain via an Application Load Balancer (ALB) and configure your domain in Route 53.&lt;/p&gt;

&lt;p&gt;You can create the Load Balancer thus:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A. Set Up an Application Load Balancer (ALB)&lt;/strong&gt;&lt;br&gt;
Go to the AWS Management Console and navigate to EC2 &amp;gt; Load Balancers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqwntcqsrc2susmk5d9jo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqwntcqsrc2susmk5d9jo.png" alt="Create Load Balancer" width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;B. Click Create Load Balancer and choose Application Load Balancer.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdp4rp9drnxc3hn63x780.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdp4rp9drnxc3hn63x780.png" alt="ALB" width="800" height="277"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Step-by-Step Configuration:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Name: Provide a name (e.g., realtime-alb).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Scheme: Choose Internet-facing.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwgaye28lypm5g9z2f18i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwgaye28lypm5g9z2f18i.png" alt="ALB-configuration" width="800" height="361"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;C. Listeners:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Add two listeners:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Port 80 (HTTP) → Target Group (Redirect to HTTPS).&lt;/li&gt;
&lt;li&gt;Port 443 (HTTPS) → SSL Certificate.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9w34uimob83zu307yjsg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9w34uimob83zu307yjsg.png" alt="Listeners" width="800" height="347"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Availability Zones: Select the VPC and all subnets for redundancy.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvrmdxsps2ofu6l3ud7ew.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvrmdxsps2ofu6l3ud7ew.png" alt="Availability zones" width="800" height="335"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Configure HTTPS Listener and Attach SSL Certificate&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In the Listeners section during ALB creation:&lt;/p&gt;

&lt;p&gt;For Port 443 (HTTPS):&lt;br&gt;
Choose SSL Certificate from AWS Certificate Manager (ACM).&lt;br&gt;
Select the validated certificate for your domain (e.g., example.com).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3m2o172fnyu5ayjduii7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3m2o172fnyu5ayjduii7.png" alt="Add SSL Certificate" width="800" height="257"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Forwarding Rules:&lt;/p&gt;

&lt;p&gt;Under Actions, ensure the HTTPS listener forwards traffic to a Target Group that includes your EC2 instance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;D. Set Up a Target Group&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go to Target Groups in the EC2 dashboard.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a target group:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Name: e.g, realtime-tg.&lt;br&gt;
Target Type: Instances.&lt;br&gt;
Protocol: HTTP (port 80).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Register your EC2 instance:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Select your running EC2 instance and add it to the target group.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Verify the health checks:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;ALB will perform periodic checks to ensure your application is running.&lt;br&gt;
Use HTTP and / for health checks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;E. Redirect HTTP to HTTPS (Optional but Recommended)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the Load Balancer Listeners section:&lt;/p&gt;

&lt;p&gt;Add a rule for Port 80 to redirect all traffic to HTTPS (Port 443).&lt;/p&gt;

&lt;p&gt;Set the redirection response to 301 Moved Permanently.&lt;/p&gt;

&lt;p&gt;Example Rule:&lt;/p&gt;

&lt;p&gt;Condition: All requests&lt;br&gt;
Action: Redirect to HTTPS (port 443).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;F. Update Route 53 to Point to the Load Balancer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Go to Route 53 and navigate to your hosted zone.&lt;/p&gt;

&lt;p&gt;Click on your A Record and edit thus:&lt;/p&gt;

&lt;p&gt;Alias: Enable Alias.&lt;/p&gt;

&lt;p&gt;Target: Select your Application Load Balancer from the dropdown list.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frkmw7wdqutxbz4uu9gze.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frkmw7wdqutxbz4uu9gze.png" alt="Edit" width="776" height="590"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foew1dnpoju3z95jdjr2z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foew1dnpoju3z95jdjr2z.png" alt="Select ALB" width="760" height="1024"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Save the record.&lt;/p&gt;

&lt;p&gt;Finally, you can restart the flask app and nginx on your connected terminal, and check the status afterwards&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ sudo systemctl restart nginx
$ sudo systemctl restart flask_app
$ sudo systemctl status nginx
$ sudo systemctl status flask_app
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Final Test
&lt;/h3&gt;

&lt;p&gt;Visit your domain e.g &lt;code&gt;https://real.example.com&lt;/code&gt; to access your deployed application securely. You should see the real-time object detection system up and running!&lt;/p&gt;

&lt;p&gt;N.B: Note that Gemini API free tier for the gemini-1.5-flash-latest model has a limit that if exceeded will start throwing a process query error. Hence, ensure to get a paid version to forestall this.&lt;/p&gt;




&lt;h2&gt;
  
  
  CONCLUSION
&lt;/h2&gt;

&lt;p&gt;In this tutorial, we embarked on a comprehensive journey to build and deploy a real-time object detection system that seamlessly integrates OpenCV for live video capture and Google's Gemini vision model for intelligent scene analysis.&lt;/p&gt;

&lt;p&gt;From configuring the application locally to deploying it securely on AWS EC2 with a custom domain and SSL, we covered every essential step to transform your idea into a functional and scalable solution.&lt;/p&gt;

&lt;p&gt;This project highlights the power of combining cutting-edge technologies like Flask, OpenCV, and AI to solve real-world problems while ensuring best practices for cloud deployment.&lt;/p&gt;

&lt;p&gt;By following these steps, you've not only deployed a robust AI-powered system but also ensured scalability, security, and efficient CI/CD pipelines.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>ai</category>
      <category>devops</category>
      <category>python</category>
    </item>
  </channel>
</rss>
