This repository focuses on cloud computing and demonstrates how to set up virtual machines, S3, and other services using LocalStack. It provides a comprehensive guide to simulating AWS services locally for development and testing purposes.
Cloud-Computing
This repository focuses on cloud computing and demonstrates how to set up virtual machines, S3, and other services using LocalStack. It provides a comprehensive guide to simulating AWS services locally for development and testing purposes.
AWS Command Line Interface (CLI) is a powerful tool that allows users to interact with AWS services directly from the terminal. It simplifies managing cloud resources by providing commands for a wide range of AWS services, enabling tasks such as provisioning, managing, and automating workflows with ease.
LocalStack is a fully functional, local testing environment for AWS services. It enables developers to simulate AWS services on their local machines, facilitating the development and testing of cloud-based applications without needing access to an actual AWS account.
we explored the world of serverless computing, where AWS Lambda took center stage.
Now, it’s time to dive deeper into the magic that makes cloud apps resilient and responsive: Cloud Load Balancing and Auto Scaling, your dynamic duo for performance and efficiency.
Let’s balance the load, scale the servers, and cloud like pros. ☁️🚀
First, I’ll explain the design behind load balancing and auto scaling.
Then, we’ll implement it using Python and LocalStack — simulating traffic with a simple Flask app instead of using paid services like NGINX.
I’ve also explained all the steps for setting up NGINX-based load balancing, which I initially tried.
So if you’re using the paid version or actual AWS services, everything will work as expected too. ✅
What is Load Balancing?
Load balancing is the process of distributing incoming network traffic across multiple servers to ensure no single server bears too much demand. This improves performance, reliability, and fault tolerance by preventing overload and ensuring continuous availability.
Key Benefits of Load Balancing:
Improved Performance: Spreads traffic efficiently across multiple servers.
High Availability & Redundancy: Ensures uptime even if a server fails.
Scalability: Easily accommodates increased traffic by adding more servers.
Optimized Resource Utilization: Ensures servers are used efficiently.
Types of Load Balancing:
Round Robin: Distributes requests sequentially across servers.
Least Connections: Directs traffic to the server with the fewest connections.
IP Hash: Uses the client's IP address to route requests consistently to the same server.
Weighted Load Balancing: Assigns different weights to servers based on their capacity.
Load Balancing Illustration
This image visually represents load balancing, showing how traffic is distributed among multiple servers to ensure high availability and reliability.
What is Nginx?
Nginx (pronounced "engine-x") is a high-performance web server and reverse proxy designed for speed, stability, and scalability. It is widely used for load balancing, caching, and serving static content efficiently.
Support for Microservices: Works seamlessly with modern containerized applications.
Nginx Overview
This image provides a high-level view of Nginx’s role in handling requests, acting as a reverse proxy, load balancer, and web server for modern applications.
🖼️ About the Cover Image:
It shows growing user traffic (👥🛒) reaching a load balancer (⚖️), which then distributes tasks efficiently.
Auto scaling (📈📉) ensures resources scale up or down as demand changes — keeping your cloud setup both effective and economical.
Simple Load balancing Function with LocalStack and Flask
Since Nginx doesn't support load balancing for LocalStack, a Python Flask application is used for round-robin load balancing simulation.
fromflaskimportFlask,render_template_stringimportitertoolsimportmatplotlib.pyplotaspltimportseabornassnsimportnumpyasnpimportioimportbase64app=Flask(__name__)# Backend servers
backends=["Backend 1","Backend 2"]# Simulated processes
processes=[f"P{i+1}"foriinrange(10)]load_values=np.random.randint(3,15,size=len(processes))# Round-robin assignment
backend_cycle=itertools.cycle(backends)schedule={process:next(backend_cycle)forprocessinprocesses}@app.route("/")defhome():"""Renders the home page with the load balancer visualization."""try:sns.set_theme(style="darkgrid")fig,ax=plt.subplots(figsize=(9,5))x_labels=list(schedule.keys())y_labels=list(schedule.values())custom_colors=["#52796F","#E63946","#6A0572","#457B9D","#9C6644","#8D99AE","#2A9D8F","#E76F51","#6D597A","#B56576"]bars=ax.bar(x_labels,load_values,color=custom_colors[:len(processes)],width=0.6)forbar,(backend,load)inzip(bars,zip(y_labels,load_values)):ax.text(bar.get_x()+bar.get_width()/2,bar.get_height()+0.4,backend,ha="center",fontsize=10,fontweight="bold",bbox=dict(facecolor="white",alpha=0.6,edgecolor="black",boxstyle="round,pad=0.3"))ax.set_ylabel("Assigned Load")ax.set_title("Round Robin Load Balancer Simulation",fontsize=14,fontweight="bold")ax.set_xticklabels(x_labels,rotation=30,ha="right")img=io.BytesIO()plt.savefig(img,format="png",bbox_inches="tight")img.seek(0)plt.close(fig)image_data=base64.b64encode(img.getvalue()).decode()html_template="""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Load Balancer</title>
<style>
body { font-family: Arial, sans-serif; background-color: #f0f2f5; text-align: center; padding: 20px; }
.container { background: white; padding: 20px; box-shadow: 0 0 15px rgba(0,0,0,0.1); max-width: 850px; margin: auto; border-radius: 10px; }
h2 { color: #2c3e50; font-size: 24px; margin-bottom: 10px; }
p { font-size: 16px; color: #555; margin-bottom: 20px; }
img { max-width: 100%; border-radius: 10px; margin-top: 10px; }
</style>
</head>
<body>
<div class="container">
<h2>Round Robin Load Balancer Simulation</h2>
<p>
Load balancing distributes tasks among multiple backend servers to optimize performance and reliability.
This simulation demonstrates a Round Robin approach, where each process is assigned to a backend in a cyclic order.
</p>
<img src="data:image/png;base64,{{ image_data }}" alt="Load Balancer Graph">
</div>
</body>
</html>
"""returnrender_template_string(html_template,image_data=image_data)exceptExceptionase:returnf"Error generating graph: {str(e)}",500if__name__=="__main__":app.run(host="0.0.0.0",port=5000)
This application displays a bar chart illustrating the distribution of processes across backend servers using the round-robin approach. The graph helps in understanding how the tasks are assigned dynamically.
Run the app using:
python app.py
Once the server is running, open a web browser and navigate to http://localhost:5000 to access the application.
📄 Want to see how it all worked step by step? Check it out here:
🎉 And that’s a wrap on Load Balancing and Auto Scaling!
💡 I’ve explained both the simulated LocalStack + Flask setup and the full NGINX-based approach, so if you’re using a paid tier, you can follow the complete setup seamlessly.
💬 Do tell me about your own experience, whether you used NGINX, LocalStack, or any other tools for load balancing. What worked, what didn’t?
📚 Found any great resources, tutorials, or articles? Drop them in the comments. I’d love to include helpful links in the article for others to explore!
🔥 Stay tuned for the next article!
We’re diving into Cloud Databases and Data Management, where I’ll talk about storing data the cloud-native way. 🗄️🌩️