fpco / terraform-aws-foundation

Establish a solid Foundation on AWS with these modules for Terraform
MIT License
203 stars 99 forks source link

Switch up how targets groups are associated with the ASG #296

Closed mcgirr closed 4 years ago

mcgirr commented 4 years ago

Small change to how we associate target groups with the ASG in the asg module

mcgirr commented 4 years ago

We're currently using this in practice now and have shown it to work. I can link to examples but I'd like to work to get this reviewed and merged. (And we can updated any places where they refer to the branch in this PR).

JoseD92 commented 4 years ago

Nice change, I like it, it solve a problem of using both target_group_arns and aws_autoscaling_attachment at the same time, mainly because the default value for the variable was [] that made target_group_arns count as used, which made terraform nuts when planning.

I just add a small change to replace the count for a foreach, as having a count might make the plans horrible if you for example delete one element, it would become a cascade update and might generate some errors in the deployment

JoseD92 commented 4 years ago

I revert from for each to count as you would get errors like:

The "for_each" value depends on resource attributes that cannot be determined
until apply, so Terraform cannot predict how many instances will be created.
To work around this, use the -target argument to first apply only the
resources that the for_each depends on.

that, although not impossible to solve using for depends_on, makes using the module a bit harder

JoseD92 commented 4 years ago

as validation, I have made test1.tf that shows the current state of the issue, after applying once, applying again does:

Terraform will perform the following actions:

  # module.asg.aws_autoscaling_group.cluster will be updated in-place
  ~ resource "aws_autoscaling_group" "cluster" {
        arn                       = "arn:aws:autoscaling:us-west-2:xxxxxxxxxxxxxxx:autoScalingGroup:xxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxx:autoScalingGroupName/jose-clusterxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        availability_zones        = [
            "us-west-2a",
            "us-west-2b",
        ]
        default_cooldown          = 300
        desired_capacity          = 1
        enabled_metrics           = []
        force_delete              = true
        health_check_grace_period = 300
        health_check_type         = "EC2"
        id                        = "jose-clusterxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        launch_configuration      = "terraform-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        load_balancers            = []
        max_instance_lifetime     = 0
        max_size                  = 1
        metrics_granularity       = "1Minute"
        min_size                  = 1
        name                      = "jose-clusterxxxxxxxxxxxxxxxxxxxxxxxxxxx
        name_prefix               = "jose-cluster"
        protect_from_scale_in     = false
        service_linked_role_arn   = "arn:aws:iam::xxxxxxxxxxxxxxxxx:role/aws-service-role/autoscaling.amazonaws.com/AWSServiceRoleForAutoScaling"
        suspended_processes       = []
        tags                      = [
            {
                "key"                 = "Name"
                "propagate_at_launch" = "true"
                "value"               = "jose-cluster"
            },
        ]
      ~ target_group_arns         = [
          - "arn:aws:elasticloadbalancing:us-west-2:xxxxxxxxxxxxxxxx:targetgroup/test1-https-tg/xxxxxxxxxxxxxxxx",
        ]
        termination_policies      = []
        vpc_zone_identifier       = [
            "subnet-xxxxxxxxxxxxxxxxxxx",
            "subnet-xxxxxxxxxxxxxxxxxxx",
        ]
        wait_for_capacity_timeout = "10m"
    }

Plan: 0 to add, 1 to change, 0 to destroy.

it removes the target_group_arns even though no change has been done. Applying another time I get:

Terraform will perform the following actions:

  # aws_autoscaling_attachment.asg_alb will be created
  + resource "aws_autoscaling_attachment" "asg_alb" {
      + alb_target_group_arn   = "arn:aws:elasticloadbalancing:us-west-2:xxxxxxxxxxxxxx:targetgroup/test1-https-tg/xxxxxxxxxxxxxxxxx"
      + autoscaling_group_name = "jose-clusterxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
      + id                     = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

restoring the aws_autoscaling_attachment, after that apply will loop the two last changes every time I apply. I add test2.tf which uses this MR on the asg and applying multiple times results in no changes

mcgirr commented 4 years ago

I've merged but I'm going to hold off deleting the branch for now until I've updated the places internally where this is used (but I will remove it soon).