Because Predictive scaling requires a large amount of data over more than 2 days to be able to make predictions for the following days, and here we do not have that data, we will need to prepare to simulate an environment
First, we will create a new folder named metric-preparation
and move it into this folder
mkdir metric-preparation && cd metric-preparation
Then download the script to prepare the data
curl -o prepare-metric-data.sh https://raw.githubusercontent.com/awslabs/ec2-spot-workshops/master/workshops/efficient-and-resilient-ec2-auto-scaling/prepare-metric-data.sh
After downloading, go inside to change the command section in the script
vim prepare-metric-data.sh
#!/bin/bash
set -e
file=$1
group=$2
echo "=== Format metric file ==="
echo $file
echo $group
l=$(jq length $file)
i=0
while [ $i -lt $l ]
do
time=$(date -d "$[5*$i] minutes ago")
cat $file | jq --argjson i $i --arg t "$time" '.[$i].Timestamp |= $t' > tmp.json && mv tmp.json $file
i=$[$i+1]
done
echo "replace autoscaling group name.."
sed -i $file -e "s/#ASGPLACEHOLDER#/$group/g"
echo "=== Complete ==="
After editing, we now proceed to load the unprocessed data, first the metrics for the instances.
curl -o metric-instances.json https://raw.githubusercontent.com/awslabs/ec2-spot-workshops/master/workshops/efficient-and-resilient-ec2-auto-scaling/metric-instances.json
CPU Data
curl -o metric-cpu.json https://raw.githubusercontent.com/awslabs/ec2-spot-workshops/master/workshops/efficient-and-resilient-ec2-auto-scaling/metric-cpu.json
Modify these two data types in turn, first for the CPU
bash prepare-metric-data.sh metric-cpu.json FCJ-Management-ASG && cat metric-cpu.json
Instances
bash prepare-metric-data.sh metric-instances.json FCJ-Management-ASG && cat metric-instances.json
💡 Note In the 2 commands above, the parameter FCJ-Management-ASG appears, which is the name of the Auto Scaling Group that we will create later, so later you will need to create an ASG with the same name. Otherwise, you should change to another name from now on.
In Amazon Linux 2023, and using the correct AMI, AWS CLI is already installed inside, at this time we just need to take it out to reconfigure the credentials. Remember that you must have an IAM User with enough permissions to upload data to CloudWatch or at least enough permissions to do this workshop.
Go to the IAM page, enter the IAM User information and enter the Access Key Id and Serect Access Key
aws configure
Configure
Then upload the 2 data files that we have prepared earlier to CloudWatch
aws cloudwatch put-metric-data --namespace 'FCJ Management Custom Metrics' --metric-data file://metric-cpu.json
aws cloudwatch put-metric-data --namespace 'FCJ Management Custom Metrics' --metric-data file://metric-instances.json
Go to CloudWatch to check the results result
In the Console interface of CloudWatch
Select AutoScalingGroupName
Select 2 more parameters as shown below, wait a while to receive the results.