Hi everyone,
I am stuck in this for a very long time, and I am struggling to find any relevant solution on the internet either.
I have two modules, first creates an EC2 instance, the latter creates ALB, target group and the EC2 instance from the first module is attached to the target group. These are the configurations:
module "instance" {
source = "../modules/instance"
count = 1
ami = var.ami
instance_name = var.instance_name
instance_type = var.instance_type
user_data = var.user_data_path
subnets = ["sg-********"]
key_name = var.key_name
monitoring = var.monitoring
vpc_security_group_ids = [data.aws_security_group.ec2-sg.id]
associate_public_ip_address = var.associate_public_ip_address
}
output "instance_ids" {
value = one(module.instance[*].instance_ids)
}
module "load-balancer" {
source = "../modules/load-balancer"
instance_count = var.alb_count
load_balancer_type = var.load_balancer_type
name = var.alb_name
subnets = var.alb_subnets
internal = var.is_internal
security_groups = var.alb_security_groups
cross_zone_load_balancing = true
##application-target-group
vpc_id = var.vpc_id
server_type_name = var.server_type_name
instance_ids = module.instance.instance_ids
listener = var.listener
health_check = var.health_check
target_groups = var.target_groups
depends_on = [ module.instance ]
}
The root instance module looks something like this:
resource "aws_instance" "this" {
count = var.instance_count
ami = data.aws_ami.this.id
instance_type = var.instance_type
subnet_id = var.var.subnets
key_name = var.key_name
vpc_security_group_ids = var.vpc_security_group_ids
iam_instance_profile = var.iam_instance_profile
}
And output.tf:
output "instance_ids" {
description = "The list of instance IDs created."
value = aws_instance.this.*.id
}
Any help would be appreciated. Cheers !
Edit: I managed to solve the issue. Had to store the output of the module to locals, and pass the instance the instance id as local argument in load balancer module. The solution looks something like:
locals "instance_ids" {
value = one(module.instance[*].instance_ids)
}
module "load-balancer" {
source = "../modules/load-balancer"
instance_count = var.alb_count
load_balancer_type = var.load_balancer_type
name = var.alb_name
subnets = var.alb_subnets
internal = var.is_internal
security_groups = var.alb_security_groups
cross_zone_load_balancing = true
##application-target-group
vpc_id = var.vpc_id
server_type_name = var.server_type_name
instance_ids = local.instance_ids
listener = var.listener
health_check = var.health_check
target_groups = var.target_groups
depends_on = [ module.instance ]
}