πΎ Lab 4: Managing Terraform State (μ격 μ μ₯μ)
λͺ©ν: λ‘컬(terraform.tfstate) νμΌμ΄ μλ, GCS λ²ν·μ λ°±μλ(Backend)λ‘ μ¬μ©νμ¬ μν νμΌμ μμ μ±, μ κΈ(Locking), νμ
κΈ°λ₯μ ν보ν©λλ€.
1. μ¬μ μ€λΉ: GCS λ²ν· μμ±
Terraformμ΄ μνλ₯Ό μ μ₯ν λ²ν·μ TerraformμΌλ‘ λ§λ€κΈ°λ³΄λ€, 미리 λ§λ€μ΄λλ κ²μ΄ μΌλ°μ μ λλ€. (Chicken-and-Egg λ¬Έμ λ°©μ§)
# μ λν¬ν λ²ν· μ΄λ¦ μμ± (μ: tf-state-λ΄μ΄λ¦-2026)
gsutil mb -l us-central1 gs://[YOUR_UNIQUE_BUCKET_NAME]
# μ: gsutil mb -l us-central1 gs://tf-state-a1234-lab2. backend.tf μμ±
μμ
λλ ν 리: terraform-labs/lab4/
terraform {
backend "gcs" {
bucket = "[YOUR_UNIQUE_BUCKET_NAME]" # μμμ λ§λ λ²ν· μ΄λ¦
prefix = "terraform/state"
}
}
provider "google" {
region = "us-central1"
}
resource "google_compute_instance" "vm_state_demo" {
name = "vm-state-demo"
machine_type = "e2-micro"
zone = "us-central1-a"
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
}
}
network_interface {
network = "default"
}
}3. μ€μ΅ κ°μ΄λ (Migration)
terraform init:- Terraformμ΄ βλ°±μλλ₯Ό GCSλ‘ μ€μ νμκ² μ΅λκΉ?βλΌκ³ 묻μ΅λλ€.
yesμ λ ₯. - μ΄μ λΆν°
terraform applyλ₯Ό νλ©΄ μν μ λ³΄κ° λ΄ μ»΄ν¨ν°κ° μλ GCS λ²ν·μ μ μ₯λ©λλ€.
- Terraformμ΄ βλ°±μλλ₯Ό GCSλ‘ μ€μ νμκ² μ΅λκΉ?βλΌκ³ 묻μ΅λλ€.
terraform plan:- μμ±λ 리μμ€μ λ³κ²½ μ¬νμ 미리 νμΈν©λλ€.
- Stateκ° λΉμ΄μλ€λ©΄
+ createκ° λ° κ²μ΄κ³ , κΈ°μ‘΄ Stateλ₯Ό λ§μ΄κ·Έλ μ΄μ νλ€λ©΄No changesκ° λ° μ μμ΅λλ€.
terraform apply: 리μμ€ μμ±.- νμΈ: GCP μ½μ > Cloud Storage > λ²ν· >
terraform/state/default.tfstateνμΌμ΄ μκ²Όλμ§ νμΈν©λλ€.
4. μ¬ν μ€μ΅: Terraform Import (κΈ°μ‘΄ 리μμ€ κ°μ Έμ€κΈ°)
μ΄λ―Έ λ§λ€μ΄μ§ 리μμ€λ₯Ό Terraform κ΄λ¦¬νλ‘ κ°μ Έμ€λ λ°©λ²μ λλ€.
- μλ μμ±: GCP μ½μμμ VM(
manual-vm)μ νλ λ§λλλ€. (e2-micro) - μ½λ μμ±:
main.tfμ ν΄λΉ VMμ κ»λ°κΈ° μ½λλ₯Ό μμ±ν©λλ€.resource "google_compute_instance" "manual_vm" { # λ΄μ©μ λΉμλκ±°λ νμκ°λ§ μ±μ name = "manual-vm" machine_type = "e2-micro" zone = "us-central1-a" boot_disk {} network_interface {} } - κ°μ Έμ€κΈ° (Import):
# terraform import [RESOURCE_ADDRESS] [RESOURCE_ID] terraform import google_compute_instance.manual_vm projects/[PROJECT_ID]/zones/us-central1-a/instances/manual-vm - λκΈ°ν:
terraform planμ μ€ννλ©΄, Terraformμ΄ μν νμΌκ³Ό μ€μ 리μμ€μ μ°¨μ΄λ₯Ό 보μ¬μ€λλ€. μ½λλ₯Ό μ€μ μ€μ μ λ§κ² μμ νμ¬No changesκ° λ¨κ² λ§λλλ€.
5. μ€μ΅ μ’ λ£
terraform destroy: 리μμ€ μμ .- μμ νμλ GCS λ²ν·μ
tfstateνμΌμ λ¨μμμ΄ μ΄λ ₯μ 보쑴ν©λλ€.
- μμ νμλ GCS λ²ν·μ
4. π€ Gemini Prompt Tip (μ μ μμ²λ²)
λ°±μλ μ€μ λ μ¬μ λͺ©λ‘μΌλ‘ μμ²νλ©΄ μ€μλ₯Ό μ€μΌ μ μμ΅λλ€.
Prompt:
Generate Terraform backend configuration based on the following specifications: * Backend Type: gcs * Bucket Name: [YOUR_UNIQUE_BUCKET_NAME] * Prefix: terraform/state * Action: Configure this backend in the terraform block.
Geminiκ° μ νν backend "gcs" λΈλ‘μ μμ±ν΄μ€λλ€.
Supported by gemini-3.0-pro preview