Create an EC2 Auto Scaling Group

To launch, maintain and scale EC2 instances dynamically for your application, you are going to create an Amazon EC2 Auto Scaling group. To help you meet your cost optimization goals, EC2 Auto Scaling allows you to combine purchase options and instance types within your Auto Scaling group. Stateless web applications are a great fit to run on EC2 Spot Instances as they can tolerate interruptions and are often flexible to run on multiple instance types. In this section, you will create an Auto Scaling group combining a base of On-Demand instances and scaling out with EC2 Spot instances and save an average of 70% in your compute costs.

To learn more about EC2 Auto Scaling click here
  1. Open asg.json on the Cloud9 editor and review the configuration. Pay special attention at the Overrides and the InstancesDistribution. Take a look at our docs to review how InstancesDistribution and allocation strategies work. You will also notice that the CapacityRebalance parameter is set to true, which will proactively attempt to replace Spot Instances at elevated risk of interruption. To learn more about the Capacity Relabancing feature, take a look at the docs.

    Help me understand the AutoScaling configuration

  2. You will notice there are placeholder values for %TargetGroupArn%, %publicSubnet1% and %publicSubnet2%. To update the configuration file with the values of the Target Group you created previously and the outputs from the CloudFormation template, execute the following command:

    sed -i.bak -e "s#%TargetGroupARN%#$TargetGroupArn#g" -e "s#%publicSubnet1%#$publicSubnet1#g" -e "s#%publicSubnet2%#$publicSubnet2#g" asg.json
    
  3. Save the file and create the auto scaling group:

    aws autoscaling create-auto-scaling-group --cli-input-json file://asg.json
    

    This command will not return any output if it is successful.

  4. Browse to the Auto Scaling console and check out your newly created auto scaling group. Take a look at the instances it has deployed.

Optional exercise

Now that you have deployed an EC2 Auto Scaling group with Mixed Instance Types and Purchase options, take some time to go through the different configurations in the console. Click on the myEC2Workshop Auto Scaling group and go to the Purchase options and instance types section and try to edit the instance types configured on the Auto Scaling group and change the “primary instance type” to see how the Auto Scaling console provides you a recommended list of instance types based on your selected instance type.