IT/Terraform

[Terraform ] Autoscaling Group에 ELB할당하기

반응형

Intro

AWS에 가장 강력한 기능 중 하나가 Autoscaling Group입니다. 해당 기능을 사용할때 엔드포인트 단일화와 부하분산을위해 AWS ELB를 필수적으로 사용하게 되는데요. Terraform을 이용하여 Autoscaling Group을 사용하여 ELB를 할당할 때 주의 해야할점이 있습니다.

autoscaling_group_attachment

테라폼에서 autoscaling group에 alb target group을 추가할때 아래와 같이 "aws_autoscaling_attachment" 라는 리소스를 사용합니다.

resource "aws_autoscaling_attachment" "asg_attachment_bar" {
  autoscaling_group_name = aws_autoscaling_group.asg.id
  lb_target_group_arn    = aws_elb.test.id
}

aws_autoscaling_attachment 리소는 오토스케일링 그룹을 생성하는 리소스인 aws_autoscaling_group 과 상호 배타적인 관계(mutually-exclusive)가 아닙니다. 즉, 별도의 리소스로 구분되어있지만 이 두개의 리소스는 서로 관여하게 된다는 이야기 입니다.

어떻게 관여하고 있나?

aws_autoscaling_attachment리소스를 통해 target group을 추가하여 terraform apply를 한 뒤 terraform plan을 하게 되면 aws_autoscaling_group 입장에서는 어?나는 target group을 설정한 적이 없는데? 하면서 target group을 destroy하려는 메시지를 확인할 수가 있습니다.

그럼 어떻게 해결 해야하는지?

따라서 아래와 같이 aws_autoscaling_attachment 에서 lb target group을 추가했다면 aws_autoscaling_group 자원에서 꼭! ignore_changes로 target_group_arns 항목을 넣어주어서 aws_autoscaling_attachment 리소스에서 target group을 넣어주었으니까 aws_autoscaling_group 넌 그냥 신경쓰지말고 무시해 라는 의미 입니다.

resource "aws_autoscaling_group" "asg" {
  # ... other configuration ...

  lifecycle {
    ignore_changes = [load_balancers, target_group_arns]
  }
}

resource "aws_autoscaling_attachment" "asg_attachment_bar" {
  autoscaling_group_name = aws_autoscaling_group.asg.id
  lb_target_group_arn    = aws_elb.test.id
}
반응형