How do I initiate rolling updates when there are no changes to the launch configuration in CloudFormation?

Last updated: 2022-08-16

I want to initiate rolling updates in my Auto Scaling group on every AWS CloudFormation stack update, without modifying the launch configuration.

Short description

You can initiate rolling updates for an Auto Scaling group only if you meet specific conditions of the UpdatePolicy attribute.

To initiate rolling updates, you can create a toggle parameter in the launch configuration of your CloudFormation template. However, if you change the value of the toggle parameter during a stack update, then the UserData property is modified. Any modification to UserData requires replacement. CloudFormation detects the modification to UserData, and then replaces the LaunchConfiguration resource. This replacement initiates the Auto Scaling rolling update, as defined by the UpdatePolicy attribute.

Resolution

The following steps assume that your AutoScalingRollingUpdate policy is configured for your Auto Scaling group, and your Auto Scaling group is configured to reference LaunchConfiguration.

Important: Make sure that you don't disrupt other elements in the UserData property when you add the toggle parameter to your template. Also, don't add the toggle parameter before cfn-signal.

1.    In your CloudFormation template, define Toggle as the parameter.

JSON:

"Parameters": {
        "Toggle": {
            "Type": "String",
            "AllowedValues": ["true","false"],
            "Default": "true"
        }
    }

YAML:

Parameters:
  Toggle:
    Type: String
    AllowedValues:
      - 'true'
      - 'false'
    Default: 'true'

2.    In the launch configuration of your template, reference the toggle parameter in the UserData property, and then launch your stack. See the following JSON and YAML examples.

JSON:

"LaunchConfig" : {
  "Type" : "AWS::AutoScaling::LaunchConfiguration",
  "Properties" : {
     "ImageId" : { "Ref" : "ImageID" },
     "UserData" : { "Fn::Base64" : { "Ref" : "Toggle" }
         ...
         ...
     },        
  "InstanceType" : { "Ref" : "Type" }
  }
}

YAML:

LaunchConfig:
  Type: 'AWS::AutoScaling::LaunchConfiguration'
  Properties:
    ImageId: !Ref ImageID
    UserData:
      'Fn::Base64': !Ref Toggle
      ...
      ...
    InstanceType: !Ref Type

Important: To initiate rolling updates when you update your stack, change the value of the parameter from true to false or false to true, depending on its initial setting.

Note: You can use the toggle parameter solution on properties where an update requires replacement, such as LaunchConfigurationName, and for resources such as, AWS::EC2::LaunchTemplate.


Did this article help?


Do you need billing or technical support?