6
votes

I have a stack in cloudformation (ECS cluster, App LB, Autoscaling Group, launch templates, etc etc.) It all works fine and we have been using this in production and pre production environments for a while.

A problem recently arose while trying to push a stack update. I made some changes to UserData in the AWS::EC2::LaunchTemplate. If i launch a new stack from this template it works great.
BUT: If i make a change set and apply a stack update cloudformation creates a NEW launch template version -however- the autoscaling group still references the OLD version. Looking at the AWS docs for AWS::AutoScaling::AutoScalingGroup LaunchTemplateSpecification

I see: "AWS CloudFormation does not support specifying $Latest, or $Default for the template version number."

Anyone wrangled w/ stack updates creating new versions of resources that need to be referenced elsewhere? I feel like i am missing something obvious.

1
So i started thinkinking maybe i could fix this with UpdateReplacePolicy but i found in the doc for that "UpdateReplacePolicy is only executed if you update a resource property whose update behavior is specified as Replacement, thereby causing AWS CloudFormation to replace the old resource with a new one with a new physical ID. " So it looks like that would be ignored in my case - the changeset show this resource will be updated not replaced.Trevor Griffiths

1 Answers

7
votes

yay, i'm dumb: use Fn::GetAtt ok, make fun of me for using json not yaml

...

"ECSAutoScalingGroup": {
            "Type": "AWS::AutoScaling::AutoScalingGroup",
            "Properties": {
                "VPCZoneIdentifier": {"Ref" : "Subnets"},
                "MinSize": "1",
                "MaxSize": "10",
                "DesiredCapacity": { "Ref": "DesiredInstanceCount" },
        "MixedInstancesPolicy": {
             "InstancesDistribution" :
                    {
                     "OnDemandBaseCapacity" : "0",
                     "OnDemandPercentageAboveBaseCapacity" : { "Ref" : "PercentOnDemand"}
                    },
             "LaunchTemplate" : {
               "LaunchTemplateSpecification" : {
                      "LaunchTemplateId" : {"Ref" : "ECSLaunchTemplate"},
                      "Version" : { "Fn::GetAtt" : [ "ECSLaunchTemplate", "LatestVersionNumber" ] }
                       },
                    "Overrides" : [ {"InstanceType": "m5.xlarge"},{"InstanceType": "t3.xlarge"},{"InstanceType": "m4.xlarge" },{"InstanceType": "r4.xlarge"},{"InstanceType": "c4.xlarge"}]
                                   }
        }
      },

...