본문 바로가기

AWS/Terraform

[AWS Terraform 기초]14. ALB Target Group 생성

반응형

 

 

1. Terraform Docs 예시

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_target_group

 

Terraform Registry

 

registry.terraform.io

 

예시:

 

 

인수 등:

 

 

2. Terraform 코드 작성

 

# 13_albtg.tf
resource "aws_lb_target_group" "pmh_albtg" {
  name     = "pmh-albtg"
  port     = 80
  protocol = "HTTP"
  vpc_id   = aws_vpc.pmh_vpc.id

  health_check {
    enabled = true
    healthy_threshold = 3
    interval = 5
    matcher  = 200
    path     = "/index.html"
    port     = "traffic-port"
    protocol = "HTTP"
    timeout  = 2
    unhealthy_threshold = 3
  }
}

 

 

resource "aws_lb_target_group" "pmh_albtg"

pmh_albtg 라는 이름의 타겟 그룹 생성

 

name = "pmh-albtg"

타겟그룹의 이름을 "pmh-albtg"로 설정

 

port = 80

타겟 그룹에서 사용할 포트 번호 설정


protocol = "HTTP"

타겟 그룹에서 사용할 프로토콜 설정


vpc_id = aws_vpc.pmh_vpc.id

타겟 그룹이 속할 VPC ID를 지정

(앞서 만들었던 VPC)


health_check

타겟 그룹에 대한 건강 상태 체크(Health Check)를 설정
1. enabled = true: 건강 상태 체크 활성화
2. healthy_threshold = 3: 건강한 상태로 판단하기 위한 최소 연속 성공 횟수를 3회로 설정
3. interval = 5: 건강 상태 체크 간격을 5초로 설정
4. matcher = 200: HTTP 상태 코드가 200인 경우 정상 응답으로 판단
5. path = "/index.html": 건강 상태 체크를 위해 사용할 경로를 "/index.html"로 설정
6. port = "traffic-port": 건강 상태 체크에 사용할 포트를 "traffic-port"로 설정

                                      (로드 밸런서가 타겟 그룹에 전달하는 트래픽 포트와 동일한 값)
7. protocol = "HTTP": 건강 상태 체크에 사용할 프로토콜을 "HTTP"로 설정
8. timeout = 2: 건강 상태 체크의 타임아웃을 2초로 설정
9. unhealthy_threshold = 3: 비정상 상태로 판단하기 위한 최대 연속 실패 횟수를 3회로 설정

 

 

 

3. Terraform 분석 및 적용

 

terraform 파일이 있는 위치에서 아래 명령어 실행

 

# 변경 사항 검토 및 확인
terraform plan

# (대화형 승인 없이)변경 사용항 적용
terraform apply -auto-approve

 

4. 결과

 

+ 변수처리하는 방법

 

tag 변수 파일 생성

# var.tf
variable "tag" {
  type        = string
  default     = "pmh"
}

 

# 13_albtg.tf
resource "aws_lb_target_group" "pmh_albtg" {
  name     = "${var.tag}-albtg"
  port     = 80
  protocol = "HTTP"
  vpc_id   = aws_vpc.pmh_vpc.id

  health_check {
    enabled = true
    healthy_threshold = 3
    interval = 5
    matcher  = 200
    path     = "/index.html"
    port     = "traffic-port"
    protocol = "HTTP"
    timeout  = 2
    unhealthy_threshold = 3
  }
}

 

 
 
반응형