-1

I have to created multiple EC2 and ALB using count. ALB was created as follows:

module "ex-alb" {
  source  = "alb-module/aws"
  
  # Count set to 3
  count                 = var.ex_count
  load_balancer_type    = "Application"
  vpc_id                = var.vpc_id_internal
  subnets               = var.ex_double_subnet_ids[count.index % length(var.ex_subnet_ids)]
  security_groups       = concat(data.aws_security_groups.sg_AAA.ids, data.aws_security_groups.sg_BBB.ids)
  target_groups =  [
  {
        name_prefix      = "alb-"
        backend_protocol = "HTTPS"
        backend_port     = 8443
        targets = [
        {
              target_id = aws_instance.ex-ec2[count.index].id
              port = 8443
        }
        ]
  }
  ]
  https_listeners       = var.alb_https_listeners
  internal              = "internal"
  tags                  = module.label.tags
  name                  = lower("SH-alb-${count.index+1}")
  idle_timeout          = var.idle_timeout
}

Now I want to output some of its attributes but it's not working:

output "ex_alb_map" {
value = { for i in module.ex-alb: i.Name => "${i.lb_id}:${i.lb_dns_name}:${i.security_group_id}"}}
}

Error: Unsupported attribute on ex_outputs.tf line 20, in output "ex_alb_map": 20: value = { for i in module.ex-alb: i.Name => "${i.lb_id}:${i.lb_dns_name}:${i.security_group_id}" } This object does not have an attribute named "Name".

output "ex_alb_map" {
value = { for i in module.ex-alb: "${i.lb_id}:${i.lb_dns_name}:${i.security_group_id}"}}
}

Error: Invalid 'for' expression

on ex_outputs.tf line 20, in output "ex_alb_map": 20: value = { for i in module.ex-alb: "${i.lb_id}:${i.lb_dns_name}:${i.security_group_id}" }

Key expression is required when building an object.

I want to print all ALB names, IDs and DNS.

I added something similar for my ec2 instances which is working but not able to do it for ALB.

output "ex_ec2_map" {
  value = { for i in aws_instance.ex-ec2: i.tags.Name => "${i.id}:${i.private_ip}:${i.availability_zone}:${i.subnet_id}" }
}
V S
  • 1
  • 1
  • The error does not match your code, which means that the code you posted is different then the one erroring out. Also with your code, its not possible to obtain the error you are calming to have. – Marcin Aug 22 '23 at 05:42
  • Sry, i was trying out various things and mixed up while posting. Added 2 separate errors now. – V S Aug 22 '23 at 05:53
  • 1
    Well, for one, it seems you do not have a tag `Name` for the ALBs. But there are actually easier ways of doing this, one being `output = module.ex-alb[*].lb_id` and repeat that for the other outputs you want/need. – Marko E Aug 22 '23 at 07:34
  • I was trying to print the name defined for the alb: name= lower("SH-alb-${count.index+1}") but this doesn't work: value = module.ex-alb[*].name – V S Aug 22 '23 at 13:39
  • value = module.ex-alb[\*].lb_dns_name works but i try to join with other attributes like this: value = module.ex-alb[\*].lb_id, module.ex-alb[\*].lb_dns_name, module.ex-alb[*].security_group_id OR value = "${module.ex-alb[\*].lb_id}, ${module.ex-alb[\*].lb_dns_name}, ${module.ex-alb[\*].security_group_id}" it's not printing as per my requirement and giving error. – V S Aug 22 '23 at 13:42
  • In order to help you more, we would need to see the outputs that you have defined in the `alb-module/aws` module. – Marko E Aug 22 '23 at 14:22
  • The outputs contain following. I don't control this module so can't add anything here. ++ http_tcp_listener_arns, http_tcp_listener_ids, https_listener_arns, https_listener_ids, lb_arn, lb_arn_suffix, lb_dns_name, lb_id, lb_zone_id, target_group_arn_suffixes, target_group_arns, target_group_names ++ Name,security_group_id is not present here so i think that's why i can't print it. But lb_dns_name and lb_id should work. – V S Aug 22 '23 at 16:46

1 Answers1

0

Without seeing your module code, it's difficult to know what the issue is. But here's a good educated guess:

i.Name is not going to work if you don't explicitly define this output. This is because you're accessing an explicit output of this module, and not an exposed attribute of a resource in that module.

There's two solutions:

  1. Re-do your mapping with something like: { for i in module.ex-alb: i.aws_ec2_instance.main.tags.Name => ... }

See the difference here is I'm accessing the attribute directly from the resource in the module.

  1. Do the mapping in the module. Same concept as above, but you're removing the module.ex-alb namespace and just referencing the resource names directly.
alex067
  • 3,159
  • 1
  • 11
  • 17
  • Yes i saw the outputs thing and did not see name or security_group_id in there so i think i can't print them. May be i will need to add this name in the tags to retrieve it. – V S Aug 22 '23 at 16:48
  • I will try i.aws_ec2_instance.main.tags.Name but shouldn't this print tags.name for ec2 rather than alb ? – V S Aug 22 '23 at 17:11
  • The whole point is you need to reference the resource. I don't know anything about your module so you need to change your code yourself. – alex067 Aug 22 '23 at 17:15