0
votes

I need to add AZRebalance as part of SuspendProcesses when defining an auto scaling group resource in Cloudformation.

The documentation https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-updatepolicy.html#cfn-attributes-updatepolicy-rollingupdate points to having this is as part of AutoScalingRollingUpdate UpdatePolicy.

"UpdatePolicy" : {
  "AutoScalingRollingUpdate" : {
    "MaxBatchSize" : Integer,
    "MinInstancesInService" : Integer,
    "MinSuccessfulInstancesPercent" : Integer,
    "PauseTime" : String,
    "SuspendProcesses" : [ List of processes ],
    "WaitOnResourceSignals" : Boolean
  }
}

Unfortunately, this causes instances in the ASG to restart whenever there is a change in LaunchConfig. Is there any way to get the best of both worlds at the time of stack creation? i.e

1) Suspend some processes in ASG at the time of stack creation
2) Disable rolling restart of instances in ASG when LaunchConfig changes

2

2 Answers

0
votes

I don’t think you can use cloud formation to suspend processes in an auto scaling group, but ignore launch configuration changes.

One option is to call auto scaling directly to suspend the processes instead of going through cloud formation

0
votes

Currently, CloudFormation AutoScaling Group resource does not have any property which enables suspend processes.

Also, using UpdatePolicy means the options only apply to the ASG during updates from CloudFormation and not creation.

One work-around is to look at custom resources to implement this:

https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/template-custom-resources.html

Also, the below link might help you get an idea of how with an example :

https://gist.github.com/atward/9573b9fbd3bfd6c453158c28356bec05

Below is a hypothetical solution ( I have not tested the above solution as well ):

#"Create" behavior, will make suspend_processes API call for AZ Rebalancing
          client = boto3.client("autoscaling")
          ASGName = event['ResourceProperties']['ASGname']
          if event['RequestType'] == 'Create':
              response = client.suspend_processes(
                  AutoScalingGroupName = ASGName,  
                  ScalingProcesses=['AZRebalance']
              )
              responseData = {}
              cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData) #Telling CFN it was a success here

But I would advise you improve on the implementation and add try catch blocks to handle exceptions and also signal Failure so that CloudFormation stack does not get hung up (i.e it will wait for a signal from the Custom resource (success/failure))and fails correctly .

You handle the update behavior in the similar way :

elif event['RequestType'] == 'Update': ........