From d3e4c5c061271c951e01397294528ae21ec5a47a Mon Sep 17 00:00:00 2001 From: Ana Fernandez Date: Wed, 13 Dec 2017 14:37:16 +0000 Subject: [PATCH] AWS node_group module add TG length parameter The way in which we use the 'lb' and 'node_group' modules, the `instance_target_group_arns` value is the output of the lb module. We tried using the length of the value in a count parameter which is not supported by Terraform (because it's computed). There are some issues in the Terraform project regarding this limitation, for instance: https://github.com/hashicorp/terraform/issues/12570 In some cases it's recommended to use `-target` when applying Terraform changes to create the LB resources before the node_group, but this is quite difficult to integrate in our solution because we don't have a proper CD pipeline yet. At this point, our way of getting around this issue is by creating a `_length` variable with the expected length of instance_target_group_arns, that we calculate outside the module. This is aligned with the solution used with private networks and NAT instances in `terraform/modules/aws/network/private_subnet/main.tf`. --- terraform/modules/aws/node_group/README.md | 1 + terraform/modules/aws/node_group/main.tf | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/terraform/modules/aws/node_group/README.md b/terraform/modules/aws/node_group/README.md index 03b17bcdb..324537bc1 100644 --- a/terraform/modules/aws/node_group/README.md +++ b/terraform/modules/aws/node_group/README.md @@ -38,6 +38,7 @@ to use with Application Load Balancers with the `instance_target_group_arns` var | instance_security_group_ids | List of security group ids to attach to the ASG | list | - | yes | | instance_subnet_ids | List of subnet ids where the instance can be deployed | list | - | yes | | instance_target_group_arns | The ARN of the target group with which to register targets. | list | `` | no | +| instance_target_group_arns_length | Length of instance_target_group_arns | string | `0` | no | | instance_type | Instance type | string | `t2.micro` | no | | instance_user_data | User_data provisioning script (default user_data.sh in module directory) | string | `user_data.sh` | no | | name | Jumpbox resources name. Only alphanumeric characters and hyphens allowed | string | - | yes | diff --git a/terraform/modules/aws/node_group/main.tf b/terraform/modules/aws/node_group/main.tf index ed98c53dc..ec2f3edd7 100644 --- a/terraform/modules/aws/node_group/main.tf +++ b/terraform/modules/aws/node_group/main.tf @@ -102,6 +102,12 @@ variable "instance_target_group_arns" { default = [] } +variable "instance_target_group_arns_length" { + type = "string" + description = "Length of instance_target_group_arns" + default = 0 +} + variable "asg_desired_capacity" { type = "string" description = "The autoscaling groups desired capacity" @@ -282,7 +288,7 @@ resource "aws_autoscaling_group" "node_autoscaling_group" { } resource "aws_autoscaling_attachment" "node_autoscaling_group_attachment_alb" { - count = "${length(var.instance_target_group_arns)}" + count = "${var.instance_target_group_arns_length}" autoscaling_group_name = "${aws_autoscaling_group.node_autoscaling_group.id}" alb_target_group_arn = "${element(var.instance_target_group_arns, count.index)}" }