Scalable and Responsive EC2 Management Application

Introduction Managing workloads on high-cost EC2 instances, such as the g5.2xlarge, demands a scalable and responsive approach. This article explores how a Flask web application can efficiently manage these instances, specifically focusing on a project called Metaverse Avatar running on Unreal Engine. This project showcases a Flask-based web application designed to optimize EC2 instance management. By incorporating a suite of AWS services—DynamoDB for data storage, Cognito for user authentication, Kinesis for event streaming, and EC2 for instance management—the application ensures seamless and efficient handling of EC2 instances that are running the application. This integration not only enhances resource utilization but also ensures cost efficiency, making it a powerful tool for managing high-cost, performance-intensive workloads like in our case Metaverse Avatar. Key Components User Authentication The application employs AWS Cognito for secure user authentication. This ensures that only authorized users can access and manage EC2 instances, enhancing the overall security of the system. Cognito handles the authentication flow, storing user credentials, and managing session tokens. EC2 Instance Management The core functionality of the application revolves around managing EC2 instances. Users can start, stop, and terminate instances based on their needs. The application interacts with AWS EC2 to perform these actions, ensuring that the instances are managed efficiently and cost-effectively. Warm Pool Management To reduce the startup time of new EC2 instances, the application maintains a warm pool of pre-warmed instances. This pool is managed in a separate thread to ensure it doesn't interfere with other operations. The warm pool ensures that instances are readily available, reducing the wait time for users and optimizing resource usage. Real-time Event Streaming For real-time capabilities, the application integrates AWS Kinesis. This service streams events and logs in real-time, allowing users to monitor the state and performance of their EC2 instances continuously. This feature is crucial for maintaining an immersive and responsive user experience. Setup and Running To set up and run the project, follow these steps: Install the required dependencies: Ensure you have Python 3. x installed. Also other libraries: boto3==1.34.36 botocore==1.34.36 cachetools==5.3.2 Flask==3.0.2 isodate==0.6.1 Jinja2==3.1.2 pydantic==1.10.13 python-dateutil==2.8.2 requests==2.31.0 Ensure you have the necessary permissions to access the required AWS services (EC2, the database service, authentication service, and event streaming service). Run the Flask: The application will start running on the https://localhost:5000. ** Algorithm Logic: Step 1: Running the Flask Application First, you'll need to run the application. You can do this either on an AWS instance or your local machine. Once the script is running, the Flask app will be accessible via the domain in our case: metaverse.genai-avatar.com. Step 2: User Authentication with AWS Cognito To access the application, you need to log in with the credentials created in Amazon Cognito. The login page looks like this: After logging in, you'll be redirected to the interface where you can connect or initiate an AWS instance. Step 3: Initiating an Instance Upon successful login, you'll see an interface like the one below: Here, you can initiate your application instance. By clicking the 'Start' button, you initiate the instance specified by its Instance ID and AMI ID. Step 4: Monitoring Instance Logs While your application instance starts, you can monitor its logs in real time. This is facilitated by AWS Kinesis Data Streams, which sends logs from your application to the web UI, ensuring that the instance is initializing correctly. This process not only helps in verifying the initialization but also ensures that any issues can be promptly identified and addressed. Step 5: Automatic Instance Management During the instance initialization, several key details are saved in DynamoDB table, including: UserId InstanceId CreationTime ExpireAt PublicIp SubnetId These details are crucial for managing the instance lifecycle. The application provides each user with a one-hour window to use the instance. If there is no activity from the user within this period, the instance is automatically stopped to save costs. While the logs from the application are visible in the UI, you can also monitor the status of the instances. This ensures that the instances are initializing correctly. As shown in the screenshot, the application is running correctly so far. The instances are named according to the user authenticated via Cognito, and five warm pool instances are being initialized. This number can be configured in the configuration file. For testing purposes in this blog post, the instance type selected is t2.micro. Once th

Jan 17, 2025 - 11:09
Scalable and Responsive EC2 Management Application

Introduction

Managing workloads on high-cost EC2 instances, such as the g5.2xlarge, demands a scalable and responsive approach. This article explores how a Flask web application can efficiently manage these instances, specifically focusing on a project called Metaverse Avatar running on Unreal Engine.

This project showcases a Flask-based web application designed to optimize EC2 instance management. By incorporating a suite of AWS services—DynamoDB for data storage, Cognito for user authentication, Kinesis for event streaming, and EC2 for instance management—the application ensures seamless and efficient handling of EC2 instances that are running the application. This integration not only enhances resource utilization but also ensures cost efficiency, making it a powerful tool for managing high-cost, performance-intensive workloads like in our case Metaverse Avatar.

Key Components

  • User Authentication
    The application employs AWS Cognito for secure user authentication. This ensures that only authorized users can access and manage EC2 instances, enhancing the overall security of the system. Cognito handles the authentication flow, storing user credentials, and managing session tokens.

  • EC2 Instance Management
    The core functionality of the application revolves around managing EC2 instances. Users can start, stop, and terminate instances based on their needs. The application interacts with AWS EC2 to perform these actions, ensuring that the instances are managed efficiently and cost-effectively.

  • Warm Pool Management
    To reduce the startup time of new EC2 instances, the application maintains a warm pool of pre-warmed instances. This pool is managed in a separate thread to ensure it doesn't interfere with other operations. The warm pool ensures that instances are readily available, reducing the wait time for users and optimizing resource usage.

  • Real-time Event Streaming
    For real-time capabilities, the application integrates AWS Kinesis. This service streams events and logs in real-time, allowing users to monitor the state and performance of their EC2 instances continuously. This feature is crucial for maintaining an immersive and responsive user experience.

Setup and Running

To set up and run the project, follow these steps:
Install the required dependencies:

  • Ensure you have Python 3. x installed.
  • Also other libraries:
boto3==1.34.36
botocore==1.34.36
cachetools==5.3.2
Flask==3.0.2
isodate==0.6.1
Jinja2==3.1.2
pydantic==1.10.13
python-dateutil==2.8.2
requests==2.31.0
  • Ensure you have the necessary permissions to access the required AWS services (EC2, the database service, authentication service, and event streaming service).

Run the Flask:

** Algorithm Logic:
Logic

Step 1: Running the Flask Application
First, you'll need to run the application. You can do this either on an AWS instance or your local machine. Once the script is running, the Flask app will be accessible via the domain in our case:

metaverse.genai-avatar.com.

Step 2: User Authentication with AWS Cognito
To access the application, you need to log in with the credentials created in Amazon Cognito. The login page looks like this:
Image description

After logging in, you'll be redirected to the interface where you can connect or initiate an AWS instance.

Step 3: Initiating an Instance
Upon successful login, you'll see an interface like the one below:

Image description

Here, you can initiate your application instance. By clicking the 'Start' button, you initiate the instance specified by its Instance ID and AMI ID.

Step 4: Monitoring Instance Logs
While your application instance starts, you can monitor its logs in real time. This is facilitated by AWS Kinesis Data Streams, which sends logs from your application to the web UI, ensuring that the instance is initializing correctly.

Image description

This process not only helps in verifying the initialization but also ensures that any issues can be promptly identified and addressed.

Step 5: Automatic Instance Management
During the instance initialization, several key details are saved in DynamoDB table, including:

  • UserId
  • InstanceId
  • CreationTime
  • ExpireAt
  • PublicIp
  • SubnetId

These details are crucial for managing the instance lifecycle. The application provides each user with a one-hour window to use the instance. If there is no activity from the user within this period, the instance is automatically stopped to save costs.

While the logs from the application are visible in the UI, you can also monitor the status of the instances. This ensures that the instances are initializing correctly.

Image description

As shown in the screenshot, the application is running correctly so far. The instances are named according to the user authenticated via Cognito, and five warm pool instances are being initialized. This number can be configured in the configuration file. For testing purposes in this blog post, the instance type selected is t2.micro.

Once the instance status checks are healthy, you will see a message indicating that the instance is ready.

Image description

Now, to connect to your running on the instance, you can click the 'Connect' button. This will redirect you to a new domain specific to your username, where your application is running. With the this domain in our case we are able our launch the metaverse avatar. (If you're curious about how our metaverse-metahuman avatar looks and operates, please check out my other blog post )

Image description

Step 6: Disconnecting and Automatic Shutdown
When you are done using your application, you can log out. Upon logging out, the instance associated with your session will automatically shut down to save resources and costs.

Image description

This automatic shutdown ensures that no resources are wasted when the instance is not in use.

Step 7: Reusing Warm Pool Instances
Now that everything is in place and the algorithm works as expected, you might wonder what happens if you want to start the application again on an instance. Thanks to the warm pool of instances, the algorithm will not initialize a completely new instance for you. Instead, it will start one of the warm pool instances. This significantly reduces the starting time, allowing you to use your application quickly with your designated domain.

When you initiate the application again, the process will be as follows:

  • Starting Warm Pool Instance: The algorithm will select an available instance from the warm pool. Since these instances are pre-initialized, the startup time is minimal.

  • Domain Assignment: The selected instance will be associated with your domain. This means you can access your application at the same domain as before, ensuring a seamless user experience.

  • Quick Availability: Since the instance is already part of the warm pool, the application will be ready to use in a short time. You won't have to wait for a new instance to be initialized, which can save several minutes.

This approach optimizes both time and resources, ensuring that you can start your application promptly without unnecessary delays.

Conclusion

This Flask web application demonstrates an efficient approach to managing EC2 instances using AWS services. By leveraging AWS Cognito, DynamoDB, Kinesis, and EC2, the application ensures scalability, responsiveness, and cost-effectiveness. This project serves as a robust solution for handling dynamic workloads and managing cloud resources efficiently. With secure authentication, effective warm pool management, real-time event streaming, and seamless session management, the application offers a comprehensive and efficient tool for EC2 instance management.