πŸ—οΈ Lab 1: Terraform Fundamentals

λͺ©ν‘œ: Terraform μ„€μ • νŒŒμΌμ„ μž‘μ„±ν•˜κ³ , GCP VM μΈμŠ€ν„΄μŠ€λ₯Ό 생성/μˆ˜μ •/μ‚­μ œν•˜λŠ” κΈ°λ³Έ μ›Œν¬ν”Œλ‘œμš°(init β†’ plan β†’ apply β†’ destroy)λ₯Ό μ΅νž™λ‹ˆλ‹€.

1. main.tf μž‘μ„±

μž‘μ—… 디렉토리: terraform-labs/lab1/

terraform {
  required_providers {
    google = {
      source = "hashicorp/google"
      version = "4.51.0"
    }
  }
}
 
provider "google" {
  # gcloud config set project둜 μ„€μ •ν–ˆλ‹€λ©΄ μƒλž΅ κ°€λŠ₯ν•˜μ§€λ§Œ λͺ…μ‹œ ꢌμž₯
  # project = "[YOUR_PROJECT_ID]" 
  region  = "us-central1"
  zone    = "us-central1-a"
}
 
resource "google_compute_instance" "vm_instance" {
  name         = "terraform-instance"
  machine_type = "e2-micro"  # ✨ Free Tier (μ›λž˜ 랩: e2-medium)
  tags         = ["web", "dev"]
 
  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
    }
  }
 
  network_interface {
    network = "default"
    access_config {
      # Ephemeral public IP
    }
  }
}

2. μ‹€μŠ΅ μ§„ν–‰ (Workflow)

Step 1. μ΄ˆκΈ°ν™”

Terraform Provider ν”ŒλŸ¬κ·ΈμΈμ„ λ‹€μš΄λ‘œλ“œν•©λ‹ˆλ‹€.

terraform init

Step 2. κ³„νš 확인

μ–΄λ–€ λ¦¬μ†ŒμŠ€κ°€ 생성될지 미리 λ΄…λ‹ˆλ‹€.

terraform plan
  • + create ν‘œμ‹œ 확인.

Step 3. 적용 (생성)

μ‹€μ œλ‘œ GCP에 λ¦¬μ†ŒμŠ€λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.

terraform apply
# Enter a value: yes μž…λ ₯
  • μ™„λ£Œ ν›„ GCP μ½˜μ†” > Compute Engineμ—μ„œ terraform-instanceκ°€ μƒμ„±λ˜μ—ˆλŠ”μ§€ ν™•μΈν•˜μ„Έμš”.

Step 4. 인프라 μˆ˜μ • 및 검증

main.tfλ₯Ό μˆ˜μ •ν•˜μ—¬ νƒœκ·Έλ₯Ό λ³€κ²½ν•΄λ΄…λ‹ˆλ‹€.

  tags = ["web", "dev", "test"] # "test" μΆ”κ°€

4.1 μ½”λ“œ ν’ˆμ§ˆ 관리 (Best Practice)

μ‹€λ¬΄μ—μ„œλŠ” 적용 전에 항상 μ½”λ“œλ₯Ό μ •λ¦¬ν•˜κ³  κ²€μ‚¬ν•©λ‹ˆλ‹€.

# μ½”λ“œ ν¬λ§·νŒ… (λ“€μ—¬μ“°κΈ° μ •λ ¬)
terraform fmt
 
# 문법 μœ νš¨μ„± 검사
terraform validate

4.2 좜λ ₯ λ³€μˆ˜ (Outputs) μΆ”κ°€

μƒμ„±λœ VM의 λ‚΄λΆ€ IP 등을 ν™•μΈν•˜κ³  μ‹Άλ‹€λ©΄ outputs.tfλ₯Ό μž‘μ„±ν•©λ‹ˆλ‹€.

# outputs.tf
output "vm_internal_ip" {
  value = google_compute_instance.vm_instance.network_interface.0.network_ip
}

λ‹€μ‹œ μ μš©ν•˜λ©΄ IPκ°€ 좜λ ₯λ©λ‹ˆλ‹€.

terraform apply
# Apply complete! Outputs:
# vm_internal_ip = "10.128.0.2"

Step 5. μ‚­μ œ (μ€‘μš”!)

μ‹€μŠ΅μ΄ λλ‚˜λ©΄ λ¦¬μ†ŒμŠ€λ₯Ό μ‚­μ œν•˜μ—¬ κ³ΌκΈˆμ„ λ°©μ§€ν•©λ‹ˆλ‹€.

terraform destroy
# Enter a value: yes μž…λ ₯

3. πŸ€– Gemini Prompt Tip (정석 μš”μ²­λ²•)

Gemini Code Assistμ—κ²Œ λͺ…ν™•ν•œ 사양(Specification)을 μ œμ‹œν•˜μ—¬ μ½”λ“œλ₯Ό μƒμ„±ν•˜λŠ” 정석적인 λ°©λ²•μž…λ‹ˆλ‹€.

Prompt:

Generate the Terraform configuration for a Google Compute Engine virtual machine, saving it to main.tf, based on the following specifications:
 
*   Name: terraform-instance
*   Machine Type: e2-micro
*   Zone: us-central1-a
*   Boot Disk: Debian 11
*   Network: Default network
*   Tags: web, dev

Geminiκ°€ μœ„ 사양을 μ™„λ²½ν•˜κ²Œ λ°˜μ˜ν•œ resource "google_compute_instance" 블둝을 μž‘μ„±ν•΄μ€λ‹ˆλ‹€. e2-microλ₯Ό λͺ…μ‹œν•¨μœΌλ‘œμ¨ λΉ„μš© λ°œμƒμ„ μ›μ²œ μ°¨λ‹¨ν•˜λŠ” 것이 ν•΅μ‹¬μž…λ‹ˆλ‹€.


Supported by gemini-3.0-pro preview