Terraform on Azure 정리 (5) - Azure 배포 테스트 : VM
배포 계획
구조: 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 등
compute.tf
- VM 1대만 배포 테스트, DB 및 storage account는 생략
- DeploymentDate : 하드코딩보다는 변수처리가 권장됨(variable.tf에서 선언), 다만 Terraform에서 직접적으로 오늘 날짜를 입력하는 것은 불가.
** 비밀번호 관리: Terraform 파일 내에 하드코딩하지 말고 secrets manager나 환경 변수로 처리하는 것이 좋음.
# 리소스 그룹 생성
resource "azurerm_resource_group" "compute_rg" {
name = var.resource_groups.compute
location = var.location
}
# 고정 공인 IP 생성
resource "azurerm_public_ip" "web_prd_public_ip" {
name = "prd-web-01-pip"
location = var.location
resource_group_name = azurerm_resource_group.compute_rg.name
allocation_method = "Static"
sku = "Basic"
tags = {
ResourceName = "prd-web-01-pip"
ServiceType = "WEB"
Environment = "PRD"
DeploymentDate = "2025-05-07"
}
}
# 네트워크 인터페이스 생성
resource "azurerm_network_interface" "web_prd_nic" {
name = "prd-web-01-nic"
location = var.location
resource_group_name = azurerm_resource_group.compute_rg.name
ip_configuration {
name = "internal"
subnet_id = azurerm_subnet.prd_subnet_web.id
private_ip_address_allocation = "Static"
private_ip_address = "10.1.1.10" # 원하는 고정 사설 IP
public_ip_address_id = azurerm_public_ip.web_prd_public_ip.id
}
tags = {
ResourceName = "prd-web-01-nic"
ServiceType = "WEB"
Environment = "PRD"
DeploymentDate = "2025-05-07"
}
}
# 리눅스 VM 생성
resource "azurerm_linux_virtual_machine" "web_prd" {
name = "prd-web-01"
resource_group_name = azurerm_resource_group.compute_rg.name
location = var.location
size = "Standard_B1s"
admin_username = "azureuser"
network_interface_ids = [
azurerm_network_interface.web_prd_nic.id
]
admin_password = "Azure123456!" # 테스트용 비밀번호
disable_password_authentication = false
os_disk {
name = "disk-web-prd-01"
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "18.04-LTS"
version = "latest"
}
tags = {
ResourceName = "prd-web-01"
ServiceType = "WEB"
Environment = "PRD"
DeploymentDate = "2025-05-07"
}
}
- cat 명령어로 내용 확인
- terraform validate를 통해 설정 유효성 검사
- terraform plan을 통해 리소스 생성 계획 확인
중략
- terraform apply로 적용 (yes 입력)
중략
- Azure 콘솔에서 Network 리소스 배포 확인