Option 1: Create Auto Scaling Group

Configurations for auto scaling group

Once our launch template is ready, we can create our auto scaling group. Our auto scaling group will be able to increase or decrease the number of instances for our server based on changes in CPU usage, and this will help keep the cost of our server down.

To open the launch template configurations,

  1. Under "Auto Scaling", select "Auto Scaling Groups".

  2. Select "Create Auto Scaling Group".

Step 1: Choose launch template or configuration

Name your auto scaling group here, and select the launch template configured in the last section.

  1. Name, Auto Scaling group name (Example): PostgreSQL Ubuntu

  2. Launch template (Example): PostgreSQL-Ubuntu

    1. Version: Leave as Default (1)

Step 2: Configure settings

  1. Under "Instance purchase options", leave as the default option: "Adhere to launch template"

  2. Under "Network", select your VPC and select your subnets

    1. In my case, I selected the first three subnets in my region.

Step 3: Configure advanced options

Leave options as defaults.

Step 4: Configure group size and scaling policies

The goal for our auto scaling group is to scale up when there is an increase in CPU usage on our server.

To configure group size and scaling policies,

  1. Under "Group size", set the following:

    • Desired capacity: 1

    • Minimum capacity: 0

    • Maximum capacity: 2

  2. Under "Scaling policy", select "Target tracking scaling policy" and set the following:

    • Scaling policy name (Example): PostgreSQL Server Average CPU

    • Metric type: Average CPU utilization

    • Target value (Example): 75

    • Disable scale in to create only a scale-out policy: Leave unchecked

  3. Under "Instance scale-in protection", leave the box unchecked.

Note on size and scaling policies:

  • Change the minimum group size to 0

    • This will prevent the launch of an instance when we want to stop the server

    • Why? If the minimum group size capacity is "1" and the instance is stopped, your auto scaling group will launch another instance to keep the minimum group size as 1.

  • Change the maximum capacity from 1 to 2

    • Auto scaling group can then scale out when there's an increase in CPU utilization.

  • Set predefined metric type "Average CPU utilization" in dynamic scaling policy

    • For our instance to scale in and out depending on CPU utilization, we will set the (dynamic) scaling policy

    • Selecting "Average CPU utilization" will keep the average CPU utilization at our defined target value by scaling in or out.

  • Ensure that auto scale group can scale in and scale out

    • Scale-out: If we "disable scale in to create only a scale-out policy," then our auto scale group will only add instances and not remove them.

    • We want our auto scale group to remove instances. If not, our costs would add up because we would have extras instances that we do not need for our CPU utilization.

    • Scale-In: If we enable "instance scale-in protection" for the entire auto scale group, then the auto scale group could not terminate the instances and scale in.

    • "Instance scale-in protection" is a crucial setting to add for our "master" instance, but we do not want to set it for all of our instances.

Step 5: Add Notifications

I did not add notifications, but if you'd like to be notified of when your serve instances launch, terminate, fail to launch, and/or fail to terminate, then "add notification" and add your email address.

Step 6: Add tags

We are not going to add tags to our auto-scale group because we already added tags in the launch template.

The launch template, which is how auto scale will be creating instances, will already tag all our resources. Adding tags here would be redundant.

Step 7: Review

Check your settings. Once you're satisfied, select "Create Auto Scaling group".

Your Auto scaling group is now working, and you should see that an instance has launched under "Instances".

Master Instance Settings

We want to ensure that our master instance with all our PostgreSQL configurations, which we will configure in the next chapter, will not terminate if

  1. the auto scale group decides to scale-in,

  2. the instance is accidentally terminated, or

  3. the instance is stopped

In order to prevent our master instance from being terminated in any of the above scenarios, we will

  1. enable scale-in protection,

  2. suspend the "Replace Unhealthy" process, and

  3. change termination protection

Enable Instance Scale-in Protection

To set scale-in protection,

  1. In the EC2 menu, under "Auto Scaling", select "Auto Scaling Groups"

  2. Select the auto scaling group you created

  3. Select "Instance management"

  4. Under "Instances", select your master instance (it should be the only instance present in the auto scale group)

  5. Under "Actions", and under "Instance scale-in protection", select "Set scale-in protection"

Suspend the "ReplaceUnhealthy" process

To suspend "ReplaceUnhealthy",

  1. In the EC2 menu, under "Auto Scaling Groups", select "Auto Scaling Groups"

  2. Select your auto scaling group

  3. Under details, go to "Advanced configurations" and select "edit"

  4. Under "Suspended processes", select "ReplaceUnhealthy"

  5. Select "Update"

Note on suspending "ReplaceUnhealthy": The job of the "ReplaceUnhealthy" process is to terminate instances that are marked as unhealthy by the "HealthChecks" process and launch a new instance to replace them. Instances that are stopped (either due to instance failure or being stopped) will activate "ReplaceUnhealthy". Therefore, this is the process that would cause our instance to be terminated and replaced if we stopped our instance.

The reason we are choosing "ReplaceUnhealthy" and not the "Terminate" process is because "Terminate" does not allow any instance to be terminated in the auto scale group. However, we do want instances that are no longer necessary (i.e our server does not demand as much CPU to stay running) to be terminated. Therefore, we want "Terminate" to continue operating.

Change Termination Protection

To enable termination protection,

  1. In the EC2 menu, select "Instances"

  2. Select your master auto scaling instance

  3. Select "Actions"

  4. Select "Instance Settings"

  5. Select "Change Termination Protection"

  6. Select "Yes, Enable"

Last updated