From 1bdcfe5ec366724c74994d6d862a7ba26885d9e2 Mon Sep 17 00:00:00 2001
From: "chelsea.gille" <chelsea.gille@wisc.edu>
Date: Mon, 15 Feb 2021 16:28:34 -0600
Subject: [PATCH] Force a new deployment of the autoscaling group when changes
 are made to the launch config

---
 terraform/autoscaling-group.tf | 46 ++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 22 deletions(-)

diff --git a/terraform/autoscaling-group.tf b/terraform/autoscaling-group.tf
index 70b8028..9f15b2f 100644
--- a/terraform/autoscaling-group.tf
+++ b/terraform/autoscaling-group.tf
@@ -1,30 +1,8 @@
-resource "aws_autoscaling_group" "secure-agent-autoscaling-group" {
-  # as per our current licesning in IICS, each docker instance that
-  # runs on EC2 will treat at as a new license.
-  name             = "secure-agent-autoscaling-group"
-  desired_capacity = 1
-  max_size         = 1
-  min_size         = 1
-
-  # make sure deployed EC2 instance is in the same AZ as the EFS
-  # see https://docs.aws.amazon.com/efs/latest/ug/mounting-fs-mount-cmd-dns-name.html
-  vpc_zone_identifier  = [sort(data.aws_subnet_ids.subnets.ids)[0]]
-  health_check_type    = "EC2"
-  launch_configuration = aws_launch_configuration.secure-agent-launch-config.name
-  tag {
-    key                 = "Name"
-    value               = "IICS Secure Agent"
-    propagate_at_launch = true
-  }
-}
-
 data "aws_ssm_parameter" "ecs-optimized" {
   name = "/aws/service/ecs/optimized-ami/amazon-linux-2/recommended/image_id"
 }
 
 resource "aws_launch_configuration" "secure-agent-launch-config" {
-  name              = "secure-agnet-launch-configuration"
-
   # Fix me: Make this use the ecs-optimized parameter so that it always uses the latest version.
   image_id          = "ami-00f69adbdc780866c"
 
@@ -44,3 +22,27 @@ resource "aws_launch_configuration" "secure-agent-launch-config" {
     create_before_destroy = true
   }
 }
+
+resource "aws_autoscaling_group" "secure-agent-autoscaling-group" {
+  # as per our current licesning in IICS, each docker instance that
+  # runs on EC2 will treat at as a new license.
+  name             = "secure-agent-autoscaling-group-${aws_launch_configuration.secure-agent-launch-config.name}"
+  desired_capacity = 1
+  max_size         = 1
+  min_size         = 1
+  min_elb_capacity = 1
+
+  # make sure deployed EC2 instance is in the same AZ as the EFS
+  # see https://docs.aws.amazon.com/efs/latest/ug/mounting-fs-mount-cmd-dns-name.html
+  vpc_zone_identifier  = [sort(data.aws_subnet_ids.subnets.ids)[0]]
+  health_check_type    = "EC2"
+  launch_configuration = aws_launch_configuration.secure-agent-launch-config.name
+  tag {
+    key                 = "Name"
+    value               = "IICS Secure Agent"
+    propagate_at_launch = true
+  }
+  lifecycle { 
+    create_before_destroy = true
+  }
+}
-- 
GitLab