Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

220
Visualizações
How organize terraform modules for common load balancer

I have AWS ALB that using for multiple EC2 instances. In Terraform i have alb module which already created load balancer and listeners, target groups for that EC2 instances.

I want attach ec2 instance to target group . I did this in EC2 instance module which got ALB instace id from load balancer module outputs.

When i perform terraform apply command in ec2 instance module terraform wants to crete new ALB but its already created and using. Terraform state in ec2 module don't know about ALB but wants to perform code from this module to get outputs

How can i organize modules using common ALB for multiple EC2 instances without recreating ALB for every new EC2 instance?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The way in which you should do this is the following:

  • Create a module for ALB, in your output.tf output the target group arn that you would like your Ec2 instance(s) to use
  • Create a module for EC2, include a aws_lb_target_group_attachment resource. Add a variable for the target group
  • In your main.tf create the load balancer via the ALB module first, then call the EC2 module for each instance you want to use, for the target group argument reference the ALB module name and its output variable for the target group.

An example main.tf including only the relevant parts

module "my_alb_module" {
   .....
}

module "my_ec2_module" {
    target_group_arn = module.my_alb_module.target_group
}

As a point of best practice if you have components like a load balancer and ec2, try to keep them in separate modules if you want to support reusability between resources.

Whilst not a direct example checkout the code from this GitHub repository, it illustrates how values can be exported between modules.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda