Don't build aws_lb until ec2 exists during a single terraform plan

My application git project contains several tf files for creating aws_launch_template, aws_autoscaling_group, aws_lb, aws_target_group_attachment, etc. I am encountering an error during terraform apply when ec2 instance doesn’t exist yet. I have a data code black that searches for the ec2 instance that I want to register to the alb. However, the data block fails because it hasn’t created an ASG yet. So how can we configure aws_lb so that it is dependent on ASG and/or launch template or the ec2 instance?

Hi @ronneilcamara!

From your description, it seems that you’re trying to have the “target group + alb” decoupled from the autoscaling group. It is possible to implement if you don’t specify the arguments load_balancers and target_group_arns for the aws_autoscaling_group resource.

So how can we configure aws_lb so that it is dependent on ASG and/or launch template or the ec2 instance?

If you want to specify an explicit dependency between resources, you should use the depends_on meta-argument (link to the doc).

Could you kindly provide your code to further elaborate on the situation? This would facilitate the assistance. :blush: