클라우드/GCP 자원 테라폼으로 관리해보기
(14) GCP VM 인스턴스 등록
Fanic
2024. 6. 9. 02:40
반응형
GCP에서 VM 인스턴스를 테라폼 모듈로 관리하고 등록하는 방법을 설명하려고 한다.
Jenkins를 VM 인스턴스를 등록한다고 가정하고 생성하였다.
1.vm_instance.tf 파일 정보
부팅 디스크 및 인스턴스 설정에 대해 작성하고 있는데 추후에는 모듈화된 디스크로 적용해볼 생각이다.
module "jenkins_instance" {
source = "./modules/compute_instance"
#공통 설정
zone = "asia-northeast3-a"
project = "pjt-an3-dev-vm-2"
#부팅 디스크 설정
boot_disk_name = "jenkins-disk"
image = "rocky-linux-8-optimized-gcp-v20240611"
disk_type = "pd-standard"
disk_size_gb = 50
# 인스턴스 설정
instance_name = "jenkins"
machine_type = "e2-medium"
tags = ["sgtag-test-80"]
subnetwork = module.sbn-an3-net-1.google_compute_subnetwork
nat_ip = "35.216.10.89"
labels = {
name = "jenkins"
}
}
2. VM 인스턴스 생성시 사용되는 모듈 정보이다.
1) main.tf
인스턴스가 생성되는 부분으로 boot_disk를 참조하고 1.에서 전달 받은 정보로 VM을 구성하였다.
주석 처리된 부분은 추후 사용할 기능으로 일단 설정하지 않았다.
resource "google_compute_instance" "instance" {
name = var.instance_name
machine_type = var.machine_type
zone = var.zone
project = var.project
boot_disk {
source = google_compute_disk.boot_disk.self_link
auto_delete = false
}
tags = var.tags
network_interface {
subnetwork = var.subnetwork
# access_config {
# nat_ip = var.nat_ip
# network_tier = "STANDARD"
# }
}
labels = var.labels
lifecycle {
ignore_changes = [metadata]
}
# service_account {
# # Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles.
# email = google_service_account.default.email
# scopes = ["cloud-platform"]
# }
}
2)variables.tf
1.에서 전달 받은 값을 저장 받는다.
variable "instance_name" {
description = "인스턴스 이름"
type = string
}
variable "machine_type" {
description = "머신 타입"
type = string
}
variable "zone" {
description = "존"
type = string
}
variable "project" {
description = "프로젝트 ID"
type = string
}
variable "image" {
description = "이미지"
type = string
}
variable "disk_size_gb" {
description = "디스크 크기 (GB)"
type = number
}
variable "tags" {
description = "태그 목록"
type = list(string)
}
variable "subnetwork" {
description = "서브넷워크"
type = string
}
variable "nat_ip" {
description = "NAT IP"
type = string
}
variable "labels" {
description = "라벨"
type = map(string)
default = {}
}
variable "disk_type" {
description = "디스크 타입"
type = string
}
variable "boot_disk_name" {
description = "부팅 디스크 이름"
type = string
}
3)boot_disk.tf
부팅 디스크 정보를 받아와 디스크를 생성하도록 하였다.
resource "google_compute_disk" "boot_disk" {
name = var.boot_disk_name
type = var.disk_type
zone = var.zone
image = var.image
size = var.disk_size_gb
project = var.project
labels = {
environment = "dev"
}
physical_block_size_bytes = 4096
}
3. 테라폼 명령어 실행
terraform apply -target module.jenkins_instance


4. GCP 콘솔에서 생성된 자원 확인

반응형