본문 바로가기

Cloud/AZURE

Terraform on Azure 정리 (6) - Azure 배포 테스트 : NSG

배포 계획

구조: Hub-Spoke 네트워크 모델
- Hub: 공통 인프라 구성 (VPN Gateway, Firewall 등)
- Spoke: 업무별 구분된 네트워크 (Web, WAS, DB 등 리소스 배치)

보안 구성
- NSG (Network Security Group): 각 Spoke VNet 및 서브넷에 할당
- VPN Gateway: Hub VNet에 배치
- Firewall: Hub VNet에 배치 및 UDR과 연동

리소스 그룹 구조
- RG-NETWORK – 가상 네트워크 및 서브넷 등 네트워크 리소스
- RG-SECURITY – NSG, Firewall, VPN 등 보안 리소스
- RG-COMPUTE – 가상 머신 등 컴퓨트 리소스
- RG-STORAGE – Storage Account 등 저장소 리소스

Tag 구성
- Key : Value 예시
- ResourceName : vm-web-prd-01 등
- ServiceType : WEB, WAS, DB 등
- Environment : PRD, QA, DEV
- DeploymentDate : 2025-05-07 등

 


 

NSG.tf

- 앞서 배포해본 VM 1대에 대핸 NSG 생성

- azurerm_linux_virtual_machine 리소스는 NSG를 자동으로 생성하지 않음. 포탈에서 VM을 만들 때만 NSG가 자동으로 생성되므로, NSG.tf와 충돌하지는 않는다고함

# NSG 생성
resource "azurerm_network_security_group" "web_prd_nsg" {
  name                = "nsg-prd-web-01"
  location            = var.location
  resource_group_name = azurerm_resource_group.compute_rg.name

  security_rule {
    name                       = "Allow-HTTP"
    priority                   = 100
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "80"
    source_address_prefix      = "*"
    destination_address_prefix = "*"
  }

  security_rule {
    name                       = "Allow-HTTPS"
    priority                   = 110
    direction                  = "Inbound"
    access                     = "Allow"
    protocol                   = "Tcp"
    source_port_range          = "*"
    destination_port_range     = "443"
    source_address_prefix      = "*"
    destination_address_prefix = "*"
  }

  tags = {
    ResourceName    = "nsg-prd-web-01"
    ServiceType     = "WEB"
    Environment     = "PRD"
    DeploymentDate  = "2025-05-07"
  }
}

# NIC에 NSG 연결
resource "azurerm_network_interface_security_group_association" "web_prd_nsg_assoc" {
  network_interface_id      = azurerm_network_interface.web_prd_nic.id
  network_security_group_id = azurerm_network_security_group.web_prd_nsg.id
}

 

- az account show로 현재 구독 위치 확인
- terraform validate를 통해 설정 유효성 검사

- terraform plan을 통해 리소스 생성 계획 확인

- terraform apply로 적용 (yes 입력)

 

- 결과 확인

지정 리소스그룹에 VM, IP, NIC, Disk, NSG 모두 생성
지정한 태그 및 설정값대로 NSG 규칙 생성