In this section, you launch an instance directly into an Auto Scaling group. The Auto Scaling group you created
ec2-workshop-asg uses lifecycle hooks to manage the application installation process.
As a part of initial set up with CloudFormation, you created a launch template with user data script. This script is executed on the instance during the the boot up, to install and starts the application. Once the application is installed, another command is executed to complete the lifecycle action and allow the instance to transition to the next lifecycle step.
To ensure that no instances are added or removed by the Auto Scaling policies you created earlier, in this step you will pause the predictive scaling and reset desired capacity to 0.
workshop-predictive-scaling-policyand turn off toggle Scaling based on forecast.
aws autoscaling set-desired-capacity --auto-scaling-group-name "ec2-workshop-asg" --desired-capacity 0
Set the desired capacity of the Auto Scaling group to 1 to launch an instance directly into the Auto Scaling group.
aws autoscaling set-desired-capacity --auto-scaling-group-name "ec2-workshop-asg" --desired-capacity 1
Watch the instance being launched in the AWS Console
You need to wait a few minutes for the instance to be launched by the previous step. Then run this script to measure the launch speed of the instance.
activities=$(aws autoscaling describe-scaling-activities --auto-scaling-group-name "ec2-workshop-asg" | jq -r '.Activities') && \ start_time=$(date -d "$(echo $activities | jq -r '.StartTime')" "+%s") && \ end_time=$(date -d "$(echo $activities | jq -r '.EndTime')" "+%s") && \ activity=$(echo $activities | jq -r '.Description') && \ echo $activity Duration: $(($end_time - $start_time))"s" || echo "Current activity is still in progress.."
Because the instance launched directly into the Auto Scaling group, all initialization actions needed to complete to prepare the instance to be placed in-service. From the results below you can see that these actions took a long time to complete, delaying how quickly your Auto Scaling group can scale and increases the application response time.
Launching a new EC2 instance: i-075fa0ad6a018cdfc Duration: 243s