Reputation: 733
This is how my code looks,
module "alb_api" {
source = "./modules/load-balancer"
env = "${lower(var.env)}"
project = "api"
vpc_id = "${data.aws_vpc.main.id}"
public_subnet1_id = "${var.public_subnet1_id}"
public_subnet2_id = "${var.public_subnet2_id}"
health_check_target_group_path = "/status"
certificate_arn = "${var.certificate_arn}"
alb_target_group_port = "1984"
current_ec2_instance_ids = "${aws_instance.ec2[*].id}"
next_ec2_instance_ids = "${aws_instance.next_ec2[*].id}"
}
I need to be able to combine instances Ids from two instances to a single array
Expectation
ec2_instance_ids = "${aws_instance.ec2[*].id},${aws_instance.next_ec2[*].id}"
This give me a syntax error, Is it possible to achieve this ? what would be the proper syntax is so ?
Inside the module I'm registering these instances to a load-balancer, code below
resource "aws_alb_target_group_attachment" "alb_target_group_attachment" {
count = length("${var.current_ec2_instance_ids}")
target_group_arn = "${aws_alb_target_group.alb_target_group.arn}"
target_id = "${var.current_ec2_instance_ids[count.index]}"
port = "${var.alb_target_group_port}"
}
resource "aws_alb_target_group_attachment" "next_alb_target_group_attachment" {
count = length("${var.next_ec2_instance_ids}")
target_group_arn = "${aws_alb_target_group.alb_target_group.arn}"
target_id = "${var.next_ec2_instance_ids[count.index]}"
port = "${var.alb_target_group_port}"
}
I need to combine these to a single as well
resource "aws_alb_target_group_attachment" "alb_target_group_attachment" {
count = length("${var.ec2_instance_ids}")
target_group_arn = "${aws_alb_target_group.alb_target_group.arn}"
target_id = "${var.ec2_instance_ids[count.index]}"
port = "${var.alb_target_group_port}"
}
Current version of code
module "alb_example-backend" {
source = "./modules/load-balancer"
env = "${lower(var.env)}"
project = "example-backend"
vpc_id = "${data.aws_vpc.main.id}"
public_subnet1_id = "${var.public_subnet1_id}"
public_subnet2_id = "${var.public_subnet2_id}"
health_check_target_group_path ="/status"
certificate_arn = "${var.certificate_arn}"
alb_target_group_port = "2010"
ec2_instance_ids = concat(aws_instance.ec2[*].id,aws_instance.next_ec2[*].id)
}
resource "aws_alb_target_group_attachment" "alb_target_group_attachment" {
count = length("${var.ec2_instance_ids}")
target_group_arn = "${aws_alb_target_group.alb_target_group.arn}"
target_id = "${var.ec2_instance_ids[count.index]}"
port = "${var.alb_target_group_port}"
}
Upvotes: 0
Views: 379
Reputation: 238319
To join two lists, aws_instance.ec2[*].id
and aws_instance.next_ec2[*].id
you can use concat. Since your syntax is for 0.11, then it would be:
ec2_instance_ids = "${concat(aws_instance.ec2.*.id, aws_instance.next_ec2.*.id)}"
For TF 0.12+:
ec2_instance_ids = concat(aws_instance.ec2[*].id, aws_instance.next_ec2[*].id)
Upvotes: 1