Skip to content

Commit

Permalink
AWS node_group module add TG length parameter
Browse files Browse the repository at this point in the history
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: hashicorp/terraform#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`.
  • Loading branch information
afda16 committed Dec 13, 2017
1 parent 3b4e3fb commit d3e4c5c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
1 change: 1 addition & 0 deletions terraform/modules/aws/node_group/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | `<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 |
Expand Down
8 changes: 7 additions & 1 deletion terraform/modules/aws/node_group/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)}"
}
Expand Down

0 comments on commit d3e4c5c

Please # to comment.