Terraform 빠른 시작
사전 요건
- Terraform 개요의 사전 준비·Provider 설정을 마쳤어야 합니다.
ECI에서 가상머신을 실제로 실행하려면 두 단계 자원이 필요합니다.
eci_virtual_machine: 가상머신 자원 정의 (스펙·인증)eci_virtual_machine_allocation: 호스트에 할당해 실행
1. 전체 예제
아래 한 파일이면 네트워크·디스크·NIC·공인 IP를 포함해 가상머신을 끝까지 띄울 수 있습니다.
# main.tf
# 1) 데이터 소스: 인스턴스 타입·가격·이미지 조회
data "eci_instance_type" "m8" {
name = "M-8" # 메모리집약형 8 vCore
}
data "eci_pricing" "vm_pricing" {
name = "M-8"
pricing_type = "ondemand"
}
data "eci_pricing" "storage_pricing" {
name = "Block Storage"
pricing_type = "ondemand"
}
data "eci_pricing" "ip_pricing" {
name = "Public IP"
pricing_type = "ondemand"
}
data "eci_block_storage_image" "ubuntu" {
name = "Ubuntu 22.04 LTS (20250116)"
}
# 2) 네트워크: 가상 네트워크 + 서브넷
resource "eci_virtual_network" "vnet" {
name = "tf-vnet"
network_cidr = "192.168.0.0/16"
tags = { managed-by = "terraform" }
}
resource "eci_subnet" "subnet" {
attached_network_id = eci_virtual_network.vnet.id
name = "tf-subnet"
purpose = "virtual_machine"
network_gw = "192.168.0.1/24"
tags = { managed-by = "terraform" }
}
# 3) 가상머신 정의
resource "eci_virtual_machine" "vm" {
name = "tf-vm-01"
instance_type_id = data.eci_instance_type.m8.id
pricing_id = data.eci_pricing.vm_pricing.id
username = "elice"
password = "ChangeMe-Strong-Pa$$w0rd!"
on_init_script = "#!/bin/bash\necho 'Hello' > /home/elice/hello.txt"
always_on = false
dr = false
tags = { managed-by = "terraform" }
}
# 4) 블록 스토리지 (OS 디스크)
resource "eci_block_storage" "boot_disk" {
attached_machine_id = eci_virtual_machine.vm.id
name = "tf-vm-01-boot"
size_gib = 100
pricing_id = data.eci_pricing.storage_pricing.id
image_id = data.eci_block_storage_image.ubuntu.id # OS 부팅 이미지
dr = false
tags = { managed-by = "terraform" }
}
# 5) 네트워크 인터페이스
resource "eci_network_interface" "ni" {
attached_subnet_id = eci_subnet.subnet.id
attached_machine_id = eci_virtual_machine.vm.id
name = "tf-vm-01-ni"
dr = false
tags = { managed-by = "terraform" }
}
# 6) 공인 IP (선택)
resource "eci_public_ip" "ip" {
attached_network_interface_id = eci_network_interface.ni.id
pricing_id = data.eci_pricing.ip_pricing.id
dr = false
tags = { managed-by = "terraform" }
}
# 7) 할당: 실제로 가상머신을 실행시키는 자원
resource "eci_virtual_machine_allocation" "run" {
machine_id = eci_virtual_machine.vm.id
tags = { managed-by = "terraform" }
depends_on = [
eci_block_storage.boot_disk,
eci_network_interface.ni,
]
}
output "public_ip" {
value = eci_public_ip.ip.ip
}
2. 기본 워크플로
# 초기화 (provider 다운로드)
terraform init
# 변경 계획 확인
terraform plan
# 적용
terraform apply
# 가상머신만 중지 (allocation만 삭제하면 가상머신 자원과 디스크는 유지)
terraform destroy -target=eci_virtual_machine_allocation.run
# 전체 자원 삭제
terraform destroy
중지 vs 삭제
- 가상머신 중지:
eci_virtual_machine_allocation자원만 제거. 가상머신 자원·디스크·NIC·공인 IP는 유지되며 스토리지·IP 과금은 계속됩니다. - 가상머신 삭제:
eci_virtual_machine까지 제거. 연결된 자원도 함께 destroy 대상에 포함하세요.
다음 단계
- 치트시트 — 자주 쓰는 명령어와 패턴 모음
- 스팟 가상머신 — Spot 가격을 적용한 자동 프로비저닝
- 공식 provider 문서 — 자원·데이터 소스 전체 레퍼런스