So far we have an ECS Cluster created and a Launch Template that bootstrap ECS agents and links them against the ECS Cluster. In this section, we will create an EC2 Auto Scaling group (ASG) for On-Demand Instances using the Launch Template created by the CloudFormation stack. Go back to your Cloud9 environment and 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 .
We will now replace the environment variables in the asg.json file with the On-Demand settings, changing the OnDemand percentage field in ASG and substituting the CloudFormation environment variables that we exported earlier with the asg.json placeholder names in the template.
export ASG_NAME=EcsSpotWorkshop-ASG-OD
export OD_PERCENTAGE=100 # Note that ASG will have 100% On-Demand, 0% Spot
sed -i -e "s#%ASG_NAME%#$ASG_NAME#g" -e "s#%OD_PERCENTAGE%#$OD_PERCENTAGE#g" -e "s#%PUBLIC_SUBNET_LIST%#$VPCPublicSubnets#g" asg.json
Read the asg.json file and understand the various configuration options for the EC2 Auto Scaling group. Check how although this is an OnDemand we still apply instance diversification with the Prioritized allocation strategy. Check how the Launch Template we reviewed in the previous section is referenced in the Auto Scaling Group.
Create the ASG for the On-Demand Instances.
aws autoscaling create-auto-scaling-group --cli-input-json file://asg.json
ASG_ARN=$(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name $ASG_NAME | jq -r '.AutoScalingGroups[0].AutoScalingGroupARN')
echo "$ASG_NAME ARN=$ASG_ARN"
The output of the above command will appear and will be similar to the one below. We have captured the ASG ARM to use in the next sections.
EcsSpotWorkshop-ASG-OD ARN=arn:aws:autoscaling:us-east-1:0004746XXXX:autoScalingGroup:1e9de503-068e-4d78-8272-82536fc92d14:autoScalingGroupName/EcsSpotWorkshop-ASG-OD
The On-Demand auto scaling group will appear as below in the console
Based on the configuration and steps above, try to answer the following questions:
So far there is no capacity provisioned in the Auto Scaling Group, or in our ECS cluster. Check how the desired capacity is zero in the ASG. We expect the capacity to scale up automatically when we deploy applications later.
Check in the console that there are no scaling policies attached to this Auto scaling group. Later on the policies will be created when we enable CAS (managed Cluster Auto Scaling) in the capacity providers.
To create a capacity provider, follow these steps:
Based on the configuration and steps above, try to answer the following questions:
The capacity provider creates a target tracking policy on the On-Demand Auto Scaling group.
Go to the AWS EC2 Console and select the Automatic Scaling tab on the EcsSpotWorkshop-ASG-OD.