How to Launch the Instance by showing hands? ML Project Do It with ATTITUDE

mdshamsfiroz
6 min readJun 6, 2023

--

Once upon a time in a bustling cloud computing kingdom, there lived a skilled system administrator named Alex. Known for their ingenious problem-solving techniques, Alex possessed a unique talent for communicating with the virtual world through hand gestures. Their latest challenge was to scale EC2 instances seamlessly, all while ensuring uninterrupted performance through dynamic load balancer adjustments.

One sunny morning, as the kingdom’s infrastructure faced an increasing surge in traffic, Alex sprung into action. They stood before their trusted workstation, where the powerful AWS console awaited their commands. With a swift gesture, Alex conjured an image of a flourishing garden, symbolizing the need for growth and scalability.

As if guided by an invisible force, Alex’s hands gracefully moved through the air, mimicking the motions of expanding clouds. In perfect synchronization, the EC2 instances multiplied, replicating themselves to meet the growing demands of the kingdom’s subjects. The instances, like obedient soldiers, aligned themselves in formation, awaiting further instructions.

Alex’s hands then transitioned into a fluid dance, signifying the next phase of the intricate process. They gestured towards an ethereal load balancer, conjuring an image of its dynamic nature. As if responding to the enchantment, the load balancer came to life, pulsating with energy.

With a flick of their fingers, Alex initiated a symphony of movements. The newly spawned EC2 instances gracefully danced towards the load balancer, registering themselves with a simple touch. It was as if they seamlessly synchronized their steps, integrating into the grand ensemble of cloud resources.

However, the rhythm of the kingdom’s needs was ever-changing. Alex’s hands, now moving with precision and agility, signaled the need for dynamic adjustments. Like a maestro conducting a symphony, they swirled their fingers, orchestrating a flawless performance. Instantaneously, selected EC2 instances gracefully withdrew from the load balancer, gracefully bowing out to ensure optimal resource allocation.

The virtuoso display of hand gestures continued as Alex maintained a delicate balance, intuitively scaling EC2 instances in response to the kingdom’s ever-fluctuating demands. Their hands seemed to paint an invisible masterpiece in the air, seamlessly adapting the cloud infrastructure to suit the kingdom’s needs.

As the day turned to dusk, the once-overflowing kingdom now enjoyed smooth and uninterrupted digital services, thanks to the harmonious collaboration between Alex’s hand gestures, the scaling of EC2 instances, and the dynamic adjustments of the load balancer. The kingdom’s subjects marveled at the seamless performance, unaware of the behind-the-scenes magic.

In this mesmerizing tale, Alex’s mastery of hand gestures had transformed scaling EC2 instances and adjusting load balancers into an enchanting art form. With each stroke of their hands, they wove a tapestry of scalability and resilience, ensuring the kingdom’s cloud infrastructure thrived amidst the ever-changing digital landscape. And so, with their unique abilities and unwavering dedication, Alex continued to protect the realm, ready to face any future challenge that lay ahead.

The architecture depicted above showcases a seamless process facilitated by a desktop workstation, the Boto3 library of Python, and the AWS Command Line Interface (CLI) configuration settings. Together, they enable the execution of various tasks as desired. Let’s dive into the precise steps involved in this architecture.

  1. Initiation of Request: The process begins with the desktop workstation, where a request is initiated. This request triggers the execution of specific tasks related to scaling and load balancing of EC2 instances.
  2. Boto3 Library: The Python-based Boto3 library comes into play, acting as the intermediary between the desktop workstation and AWS services. Boto3 provides a simple yet powerful interface to interact with various AWS resources programmatically.
  3. AWS CLI: To accomplish the desired tasks, the Boto3 library utilizes the AWS CLI installed on the desktop workstation. The AWS CLI is a unified tool that allows users to manage AWS services from the command line interface. It provides convenient commands to interact with AWS resources and perform administrative tasks.
  4. Configuration Settings: The AWS CLI on the desktop workstation is configured with the necessary settings, including access keys and region information. These settings enable the CLI to authenticate and authorize the requests made by Boto3.
  5. Classic ELB Creation: Using the Boto3 library, a classic Elastic Load Balancer (ELB) is created. The ELB acts as a virtual appliance that distributes incoming traffic across multiple EC2 instances to enhance availability and scalability.
  6. Launching Instances: With the aid of Boto3 and the AWS CLI, the EC2 instances are launched dynamically. These instances are responsible for handling the actual workload and responding to incoming requests.
  7. Terminating Instances: Similarly, Boto3 and the AWS CLI allow for the termination of EC2 instances when they are no longer required. This helps optimize resource utilization and cost efficiency.

In summary, the architecture combines the power of the desktop workstation, the Boto3 library, and the AWS CLI configuration settings to seamlessly create classic ELBs and launch or terminate EC2 instances. This enables efficient scaling and load balancing, ultimately improving the performance and resilience of the cloud infrastructure.

Code explanation:

The program uses cv2, boto3 modules, and HandDetector module from cvzone.HandTrackingModule library.

The program is segregated in three parts(functions). The first one is __main__().

It’s the default function in python. Whenever a code is wriiten directly, It is stored in __main__(). Nevertheless, the second func used is genOS() → It will generate the OS andregister it to the load balancer.

The code for the same:

def genOS():
#replace security goup id with yours
instances= ec2.create_instances(MinCount=1, MaxCount=1, InstanceType="t2.micro", ImageId="ami-0a2acf24c0d86e927", SecurityGroupIds=['sg-0e1df919b8da67f2e'])
elb.register_instances_with_load_balancer(
LoadBalancerName='MadeByBoto3',
Instances=[
{
'InstanceId': instances[0].id
}
]
)
return instances[0].id

In the above code the instances variable gets the output of the new ec2 instance launced while ec2 is the object of EC2 resource of boto3. elb is the object of elb client of boto3. In case you don’t wish to use classic load balancer change it to elbv2 and specify the type of load balancer. The elb client registers the newly generated instance to the load balancer. The id of instance is return to store it in a list for further use.

The third function is delOS() → It will deregister the instance from load balancer and terminate it.

The code is given below,

def delOS(id):
elb.deregister_instances_from_load_balancer(
LoadBalancerName='MadeByBoto3',
Instances=[
{
'InstanceId': id
},
]
)
ec2.instances.filter(InstanceIds=[id]).terminate()

It takes the instance id as the string in input and stores in variable id. Then this instances is deregistered from load balancer and then it is terminated.

Python Script for detection of hand:

The Hand Detector in Python is provided by HandDetector class, located in HandTrackingModule from cvzone library.

Install dependencies of cvzone.HandTrackingModule.

pip install cvzone mediapipe

The code for the scaler is given below:

  1. cv2 module is used for access the webcam. urllib.request module for sending url request to the API endpoint HandTrackingModule to detect hand
  2. The detector variable is the object of HandDetector class which is initialized with two parameters maximum hands to detect at a point in time and treshold confidence of detection
  3. In loop, the video stream is analyzed and Hands are Detected via findHands() function.
  4. If Hands are detected and fingers too then fingers that are up are counted by fingersUp()
  5. Now, if the hand is left then we hit the API that will scale out the Containers (Docker.py)
  6. If the hand is right then scale in. (Deletecon.py Check Github)
  7. The fingers that are up are represented by 1 so when 1 is present then only call the API.
  8. This loop will continue until Esc key is pressed.

I will update this project video and github link furthur .

Thanks.

--

--

mdshamsfiroz
mdshamsfiroz

Written by mdshamsfiroz

Trying to learn tool by putting heart inside to make something

No responses yet