I think that part of my issue is that I am not sure of where I should declare the aws_alb_listener_rule resource.
In infra-modules, in whatever modules need to have the ALB route to them. Typically, these will be the modules for your ECS services.
But doing so the “${lookup(module.alb.https_listener_arns, “443”)}” call is failing :
resource ‘aws_alb_listener_rule.host_based’ config: unknown module referenced: alb
In the example code, the ALB is defined using a module in the same file, so it is being referenced using module.alb. For you, since you have one ALB shared amongst multiple services, it is defined in a separate module, and you pull in the information for it using a terraform_remote_state data source. Therefore, you’d need to replace module.alb.https_listener_arns with something like data.terraform_remote_state.alb.https_listener_arns.
Also this would hardcode the rule priority to 95.
You have two general approaches you can take:
Each of your ECS services gets its own module, where it declares its own listener rules, and hard-codes the priorities for them. As long as those priorities don’t overlap, since you use separate ALBs in each environment, this will work fine.
You can try to create a more generic ECS module that takes in a list of priorities, ports, paths, etc and to generate the listener rules dynamically using the count parameter and lots of interpolation functions. This would be more dynamic/reusable, but probably harder to reason about.
data.terraform_remote_state.alb.https_listener_acm_cert_arns was what I needed, it works as expected now. I’ll have to take care of the rules priority now.