single-primary: use launch template

After December 31, 2022 no new Amazon Elastic Compute Cloud (Amazon EC2)
instance types will be added to launch configurations. After this date,
existing launch configurations will continue to work, but new EC2
instances will only be supported through launch templates.

more info at [1]

[1]https://aws.amazon.com/blogs/compute/amazon-ec2-auto-scaling-will-no-longer-add-support-for-new-ec2-features-to-launch-configurations/

Bug: Issue 16490
Change-Id: I8b79e6e67d835695bdf7f4720fa1ea6fa1eef388
diff --git a/single-primary/cf-cluster.yml b/single-primary/cf-cluster.yml
index a9d667d..569122e 100644
--- a/single-primary/cf-cluster.yml
+++ b/single-primary/cf-cluster.yml
@@ -121,7 +121,9 @@
     Properties:
       VPCZoneIdentifier:
         - !GetAtt ECSTaskNetworkStack.Outputs.PublicSubnetOneRef
-      LaunchConfigurationName: !Ref 'ContainerInstances'
+      LaunchTemplate:
+        LaunchTemplateId: !Ref 'GerritLaunchTemplate'
+        Version: !GetAtt GerritLaunchTemplate.DefaultVersionNumber
       MinSize: '1'
       MaxSize: !Ref 'MaxSize'
       DesiredCapacity: !Ref 'DesiredCapacity'
@@ -131,105 +133,113 @@
     UpdatePolicy:
       AutoScalingReplacingUpdate:
         WillReplace: 'true'
-  ContainerInstances:
-    Type: AWS::AutoScaling::LaunchConfiguration
+
+
+  GerritLaunchTemplate:
+    Type: AWS::EC2::LaunchTemplate
     Properties:
-      ImageId: !Ref 'ECSAMI'
-      SecurityGroups: [!Ref 'EcsHostSecurityGroup']
-      InstanceType: !Ref 'InstanceType'
-      IamInstanceProfile: !Ref 'EC2InstanceProfile'
-      KeyName: !Ref ECSKeyName
-      UserData:
-        Fn::Base64: !Sub |
-          #!/bin/bash -xe
-          echo ECS_CLUSTER=${ECSCluster} >> /etc/ecs/ecs.config
-          echo ECS_INSTANCE_ATTRIBUTES={\"target_group\":\"primary\"} >> /etc/ecs/ecs.config
-          # Make sure latest version of the helper scripts are installed as per recommendation:
-          # https://github.com/awsdocs/aws-cloudformation-user-guide/blob/master/doc_source/cfn-helper-scripts-reference.md#using-the-latest-version
-          yum install -y aws-cfn-bootstrap aws-cli xfsprogs
-          EC2_INSTANCE_ID=`curl -s http://169.254.169.254/latest/meta-data/instance-id`
-          VOLUME_ID=${GerritVolumeStack.Outputs.GerritVolumeRef}
-          echo "Volume Id: $VOLUME_ID"
+      LaunchTemplateName: !Sub ${AWS::StackName}-lt-gerrit
+      LaunchTemplateData:
+        ImageId: !Ref 'ECSAMI'
+        SecurityGroupIds:
+          - !GetAtt EcsHostSecurityGroup.GroupId
+        InstanceType: !Ref 'InstanceType'
+        IamInstanceProfile:
+          Arn: !GetAtt
+            - EC2InstanceProfile
+            - Arn
+        KeyName: !Ref ECSKeyName
+        UserData:
+          Fn::Base64: !Sub |
+            #!/bin/bash -xe
+            echo ECS_CLUSTER=${ECSCluster} >> /etc/ecs/ecs.config
+            echo ECS_INSTANCE_ATTRIBUTES={\"target_group\":\"primary\"} >> /etc/ecs/ecs.config
+            # Make sure latest version of the helper scripts are installed as per recommendation:
+            # https://github.com/awsdocs/aws-cloudformation-user-guide/blob/master/doc_source/cfn-helper-scripts-reference.md#using-the-latest-version
+            yum install -y aws-cfn-bootstrap aws-cli xfsprogs
+            EC2_INSTANCE_ID=`curl -s http://169.254.169.254/latest/meta-data/instance-id`
+            VOLUME_ID=${GerritVolumeStack.Outputs.GerritVolumeRef}
+            echo "Volume Id: $VOLUME_ID"
 
-          aws ec2 attach-volume --region ${AWS::Region} --volume-id $VOLUME_ID --instance-id $EC2_INSTANCE_ID --device /dev/xvdg
+            aws ec2 attach-volume --region ${AWS::Region} --volume-id $VOLUME_ID --instance-id $EC2_INSTANCE_ID --device /dev/xvdg
 
-          for i in $(seq 1 ${GerritVolumeAttachMaxRetries}); do
-            echo "Waiting for volume $VOLUME_ID to be attached to the instace $EC2_INSTANCE_ID"
-            volumeStatus=`aws ec2 describe-volumes --region ${AWS::Region} --volume-ids $VOLUME_ID`
-            if [[ $volumeStatus =~ "\"State\": \"attached\"" ]]; then
-              echo "Volume $VOLUME_ID attached to the instace $EC2_INSTANCE_ID"
-              break
-            elif [[ "$i" -eq "${GerritVolumeAttachMaxRetries}" ]]; then
-              echo "Could not attach the volume $VOLUME_ID to the instace $EC2_INSTANCE_ID"
-              exit 1
+            for i in $(seq 1 ${GerritVolumeAttachMaxRetries}); do
+              echo "Waiting for volume $VOLUME_ID to be attached to the instace $EC2_INSTANCE_ID"
+              volumeStatus=`aws ec2 describe-volumes --region ${AWS::Region} --volume-ids $VOLUME_ID`
+              if [[ $volumeStatus =~ "\"State\": \"attached\"" ]]; then
+                echo "Volume $VOLUME_ID attached to the instace $EC2_INSTANCE_ID"
+                break
+              elif [[ "$i" -eq "${GerritVolumeAttachMaxRetries}" ]]; then
+                echo "Could not attach the volume $VOLUME_ID to the instace $EC2_INSTANCE_ID"
+                exit 1
+              fi
+              sleep ${GerritVolumeAttachRetryDelay}
+            done
+
+            if [[ "${GerritVolumeId}" = "" && "${GerritVolumeSnapshotId}" = "" ]]; then
+              echo "Create file system for Gerrit volume"
+              mkfs -t xfs /dev/xvdg
             fi
-            sleep ${GerritVolumeAttachRetryDelay}
-          done
 
-          if [[ "${GerritVolumeId}" = "" && "${GerritVolumeSnapshotId}" = "" ]]; then
-            echo "Create file system for Gerrit volume"
-            mkfs -t xfs /dev/xvdg
-          fi
+            mkdir /gerrit-mount-point
+            mount /dev/xvdg /gerrit-mount-point
 
-          mkdir /gerrit-mount-point
-          mount /dev/xvdg /gerrit-mount-point
+            if [[ "${GerritVolumeId}" = "" && "${GerritVolumeSnapshotId}" = "" ]]; then
+              echo "Create Gerrit directories"
+              mkdir -p /gerrit-mount-point/gerrit-logs \
+               /gerrit-mount-point/gerrit-cache \
+               /gerrit-mount-point/gerrit-data \
+               /gerrit-mount-point/gerrit-git \
+               /gerrit-mount-point/gerrit-index \
+               /gerrit-mount-point/gerrit-db
+            fi
 
-          if [[ "${GerritVolumeId}" = "" && "${GerritVolumeSnapshotId}" = "" ]]; then
-            echo "Create Gerrit directories"
-            mkdir -p /gerrit-mount-point/gerrit-logs \
-             /gerrit-mount-point/gerrit-cache \
-             /gerrit-mount-point/gerrit-data \
-             /gerrit-mount-point/gerrit-git \
-             /gerrit-mount-point/gerrit-index \
-             /gerrit-mount-point/gerrit-db
-          fi
+            chown 1000:1000 -R /gerrit-mount-point
 
-          chown 1000:1000 -R /gerrit-mount-point
-
-          # Get the CloudWatch Logs agent
-          echo -e "
-            {\"logs\":
-              {\"logs_collected\":
-                {\"files\":
-                  {\"collect_list\":
-                    [
-                      {\"file_path\": \"/gerrit-mount-point/gerrit-logs/httpd_log\",
-                      \"log_group_name\": \"${AWS::StackName}\",
-                      \"log_stream_name\": \"${EnvironmentName}/{instance_id}/httpd_log\",
-                      \"timezone\": \"UTC\"
-                      },
-                      {\"file_path\": \"/gerrit-mount-point/gerrit-logs/sshd_log\",
-                      \"log_group_name\": \"${AWS::StackName}\",
-                      \"log_stream_name\": \"${EnvironmentName}/{instance_id}/sshd_log\",
-                      \"timezone\": \"UTC\"
-                      },
-                      {\"file_path\": \"/gerrit-mount-point/gerrit-logs/gc_log\",
-                      \"log_group_name\": \"${AWS::StackName}\",
-                      \"log_stream_name\": \"${EnvironmentName}/{instance_id}/gc_log\",
-                      \"timezone\": \"UTC\"
-                      },
-                      {\"file_path\": \"/gerrit-mount-point/gerrit-logs/error_log\",
-                      \"log_group_name\": \"${AWS::StackName}\",
-                      \"log_stream_name\": \"${EnvironmentName}/{instance_id}/error_log\",
-                      \"timezone\": \"UTC\"
-                      },
-                      {\"file_path\": \"/gerrit-mount-point/gerrit-logs/audit_log\",
-                      \"log_group_name\": \"${AWS::StackName}\",
-                      \"log_stream_name\": \"${EnvironmentName}/{instance_id}/audit_log\",
-                      \"timezone\": \"UTC\"
-                      }
-                    ]
+            # Get the CloudWatch Logs agent
+            echo -e "
+              {\"logs\":
+                {\"logs_collected\":
+                  {\"files\":
+                    {\"collect_list\":
+                      [
+                        {\"file_path\": \"/gerrit-mount-point/gerrit-logs/httpd_log\",
+                        \"log_group_name\": \"${AWS::StackName}\",
+                        \"log_stream_name\": \"${EnvironmentName}/{instance_id}/httpd_log\",
+                        \"timezone\": \"UTC\"
+                        },
+                        {\"file_path\": \"/gerrit-mount-point/gerrit-logs/sshd_log\",
+                        \"log_group_name\": \"${AWS::StackName}\",
+                        \"log_stream_name\": \"${EnvironmentName}/{instance_id}/sshd_log\",
+                        \"timezone\": \"UTC\"
+                        },
+                        {\"file_path\": \"/gerrit-mount-point/gerrit-logs/gc_log\",
+                        \"log_group_name\": \"${AWS::StackName}\",
+                        \"log_stream_name\": \"${EnvironmentName}/{instance_id}/gc_log\",
+                        \"timezone\": \"UTC\"
+                        },
+                        {\"file_path\": \"/gerrit-mount-point/gerrit-logs/error_log\",
+                        \"log_group_name\": \"${AWS::StackName}\",
+                        \"log_stream_name\": \"${EnvironmentName}/{instance_id}/error_log\",
+                        \"timezone\": \"UTC\"
+                        },
+                        {\"file_path\": \"/gerrit-mount-point/gerrit-logs/audit_log\",
+                        \"log_group_name\": \"${AWS::StackName}\",
+                        \"log_stream_name\": \"${EnvironmentName}/{instance_id}/audit_log\",
+                        \"timezone\": \"UTC\"
+                        }
+                      ]
+                    }
                   }
                 }
-              }
-            }" >> /home/ec2-user/gerritlogsaccess.json
-          # Install the CloudWatch Logs agent
-          yum install -y wget
-          wget https://s3.amazonaws.com/amazoncloudwatch-agent/centos/amd64/latest/amazon-cloudwatch-agent.rpm
-          rpm -U ./amazon-cloudwatch-agent.rpm
-          /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/home/ec2-user/gerritlogsaccess.json -s
-          # Signal to CloudFormation aws-cfn-bootstrap has been correctly updated
-          /opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource ECSAutoScalingGroup --region ${AWS::Region}
+              }" >> /home/ec2-user/gerritlogsaccess.json
+            # Install the CloudWatch Logs agent
+            yum install -y wget
+            wget https://s3.amazonaws.com/amazoncloudwatch-agent/centos/amd64/latest/amazon-cloudwatch-agent.rpm
+            rpm -U ./amazon-cloudwatch-agent.rpm
+            /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c file:/home/ec2-user/gerritlogsaccess.json -s
+            # Signal to CloudFormation aws-cfn-bootstrap has been correctly updated
+            /opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource ECSAutoScalingGroup --region ${AWS::Region}
 
   EC2InstanceProfile:
     Type: AWS::IAM::InstanceProfile