오늘의 인기 글
최근 글
최근 댓글
Today
Total
05-08 16:23
관리 메뉴

우노

[Terraform] GCP Load Balancer에 특정 IP만 접근 허용하는 방법 본문

DevOps/Terraform

[Terraform] GCP Load Balancer에 특정 IP만 접근 허용하는 방법

운호(Noah) 2024. 3. 7. 17:10

들어가기 앞서,

  • 해당 포스팅에선 Terraform을 통해, GCP Load Balancer에 특정 IP 접근만 허용하는 방법에 대해서 다뤄보겠습니다.
  • 결론부터 말씀드리면, Google Cloud Armor security policy를 google_compute_backend_service에 연결하는 방식으로 접근 제어를 설정할 수 있습니다.
    • google_compute_backend_service는 Google Cloud Load Balancing 사용 시, 트래픽을 여러 인스턴스 그룹에 분산하는 데 사용되는 리소스이며, 해당 리소스의 추가적인 설명은 아래 링크를 참고하시면 됩니다.
    • Google Cloud Armor security policy는 google_compute_security_policy로 생성할 수 있습니다.

예제 코드

  • 먼저, Google Cloud Armor security policy를 생성합니다.

      resource "google_compute_security_policy" "my_security_policy" {
        name = "my-security-policy"
    
        rule {
          action   = "allow"
          priority = 1000
          match {
            versioned_expr = "SRC_IPS_V1"
            config {
              src_ip_ranges = ["<YOUR_IP_RANGE>"]
            }
          }
        }
      }
    
    • <YOUR_IP_RANGE>는 허용하려는 IP 범위입니다.
    • 예를 들어, ["192.168.1.0/24"]처럼 설정할 수 있습니다.
    • 이 정책은 지정된 IP 범위에서의 접근만을 허용합니다.
  • 이제 google_compute_backend_service를 생성하고, 앞서 생성한 security policy를 적용합니다.

      resource "google_compute_backend_service" "my_backend_service" {
        name        = "my-backend-service"
        protocol    = "HTTP"
        port_name   = "http"
        timeout_sec = 10
    
        backend {
          group = google_compute_instance_group.my_instance_group.self_link
        }
    
        security_policy = google_compute_security_policy.my_security_policy.self_link
      }
    
    • google_compute_instance_group.my_instance_group.self_link 부분은 실제 인스턴스 그룹의 Terraform 리소스 ID로 교체해야 합니다.

참고

Comments