In this section, you create an Auto Scaling group for EC2 Spot Instances using the Launch Template created by the CloudFormation stack. This procedure is exactly the same as the previous section, except for a few changes specific to the configuration for Spot Instances.
Copy the file templates/asg.json for the EC2 Auto Scaling group configuration.
cd ~/environment/ec2-spot-workshops/workshops/ecs-spot-capacity-providers/ cp templates/asg.json spot-asg.json
Read the spot-asg.json file. We configured the instance diversification in spot-asg.json according to the guidelines from our previous section. Notice how we’ve chosen instance types with similar hardware characteristics in order to have a consistent auto scaling experience. Check also how the allocation strategy chosen for Spot is Capacity-optimized, this will let the ASG select the instances that mimimize the frequency of spot interruptions.
We will now replace the environment variables in the spot-asg.json file with the Spot settings, setting the OnDemand percentage to 0 and substituting the CloudFormation environment variables that we exported earlier.
export ASG_NAME=EcsSpotWorkshop-ASG-SPOT export OD_PERCENTAGE=0 # Note that ASG will have 0% On-Demand, 100% Spot sed -i -e "s#%ASG_NAME%#$ASG_NAME#g" -e "s#%OD_PERCENTAGE%#$OD_PERCENTAGE#g" -e "s#%PUBLIC_SUBNET_LIST%#$VPCPublicSubnets#g" spot-asg.json
Finally we create the ASG for the Spot Instances and store the ARN for the spot group.
aws autoscaling create-auto-scaling-group --cli-input-json file://spot-asg.json ASG_ARN=$(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name $ASG_NAME | jq -r '.AutoScalingGroups.AutoScalingGroupARN') echo "$ASG_NAME ARN=$ASG_ARN"
The output of the above command will appear as below:
The EC2 Spot auto scaling group should appear as below in the console Note that there is no capacity provisioned i.e. desired capacity is zero in the ASG. We expect the capacity to scale up automatically when we deploy applications later.
To create the capacity provider, follow these steps:
We encourage you to do a similar exercise to what you did with the OnDemand Auto Scaling Group. Check in the console that the ECS Cluster has the new CP-SPOT Capacity Provider, and check out the configuration and scaling policy created on the EcsSpotWorkshop-ASG-SPOT Auto Scaling Group
Refresh the Capacity Providers tab, and you will see the CP-SPOT is created and attached to the ECS cluster.