Auto Scaling Group
Credits Educative.io
We’ll create Auto Scaling Group (ASG), under EC2, and stress machines to see how auto-scaling works in action.
Let’s create:
- EC2 instance, create Security Group, install nodejs and some packages (note: you can also use the EC2 “User Data” to run commands on your instance at launch)
- a template based on this instance,
- ASG, set min to 1 and max to 2 instances, with 20% CPU threshold,
We then stress the instance to increase the load above 20% for 4 minutes, this triggers creating another instance automatically per auto-scaler:
sudo amazon-linux-extras install epel -y
sudo yum install stress -y
#Stress the CPU with four worker threads for 320 seconds
sudo stress --cpu 4 --timeout 320
To install node and express on a new EC2 instance
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
. ~/.nvm/nvm.sh
nvm install 16
Then install express and ip:
mkdir eduapp
cd eduapp/
npm install --save express
npm install --save ip
cat > index.js << EOF
put this in index.js
:
const express = require('express');
var ip = require("ip");
const app = express();
const PORT = process.env.PORT || 3000;
.get('/',(req, res) => res.send(ip.address()));
app.listen(PORT, () => console.log('Server listening at port 3000'))
app EOF