Auto Scaling Groups

An Auto Scaling group contains a collection of Amazon EC2 instances that are treated as a logical grouping for the purposes of automatic scaling and management. An Auto Scaling group also enables you to use Amazon EC2 Auto Scaling features such as health check replacements and scaling policies. Both maintaining the number of instances in an Auto Scaling group and automatic scaling are the core functionality of the Amazon EC2 Auto Scaling service.


The size of an Auto Scaling group depends on the number of instances you set as the desired capacity. You can adjust its size to meet demand, either manually or by using automatic scaling.


Modifying Scaling Policy


Commander change requests can be used to modify resources using workflows. For this scenario we'll use a change request form to let users change the desired capacity to any value between the minimum and maximum number of instances. We'll use the AWS CLI and Python in the workflow, so both must be installed on the Commander server before continuing. 


See https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html

See https://www.python.org/downloads/windows/


1. Create the change request form


Create a change request form with a Dynamic List form control named Desired Capacity. Configure the control with the following:

Script Arguments: #{target.deployedName} #{target.region.name}

Script:

import os
import json
import sys

if len(sys.argv) < 3:
    print("expected script.py auto-scale-group-name region-name")
    exit (1)

asgName = sys.argv[1]
regionName = sys.argv[2]

command = "c:\path\to\aws.exe autoscaling describe-auto-scaling-groups  --auto-scaling-group-names {} --region {}".format(asgName, regionName)
output = os.popen(command).read()

asgConfig = json.loads(output)
groups = asgConfig['AutoScalingGroups']
if len(groups) == 0:
    print("No ASG named " + asgName)
    exit (2)

if len(groups) > 1:
    print("More than one ASG named " + asgName)
    exit (3)

asg = groups[0]
desired = asg['DesiredCapacity']
min = asg['MinSize']
max = asg['MaxSize']

# Create an list of valid responses with values between mix and max
options = []
options.append("{} Current".format(desired))

for x in range(min, max + 1):
    if x != desired:
        options.append(str(x))

print(json.dumps(options))

Executable: c:\path\to\python.exe


Save the form.


2. Create the workflow


Create a completion workflow for a change request. Add a script step with the following body:


c:\path\to\aws.exe autoscaling set-desired-capacity --auto-scaling-group-name #{target.deployedName} --desired-capacity #{target.settings.dynamicList['Desired Capacity']}

On the Assigned Forms page, assign the workflow to the form created in step 1.


To learn how to use change requests to make changes to existing services, see Requesting changes to a service