π Lab 2: Infrastructure as Code (리μμ€ μμ‘΄μ±)
λͺ©ν: VPC λ€νΈμν¬μ μλΈλ·μ μ§μ μ μνκ³ , VM μΈμ€ν΄μ€κ° ν΄λΉ λ€νΈμν¬λ₯Ό μ°Έμ‘°(reference)νλλ‘ κ΅¬μ±νμ¬ λ¦¬μμ€ κ°μ **μμμ μμ‘΄μ±(Implicit Dependency)**μ μ΄ν΄ν©λλ€.
1. main.tf μμ±
μμ
λλ ν 리: terraform-labs/lab2/
provider "google" {
region = "us-central1"
}
# 1. VPC λ€νΈμν¬ μμ±
resource "google_compute_network" "vpc_network" {
name = "terraform-network"
auto_create_subnetworks = false # 컀μ€ν
μλΈλ· μ¬μ©
}
# 2. μλΈλ· μμ±
resource "google_compute_subnetwork" "subnetwork" {
name = "terraform-subnetwork"
ip_cidr_range = "10.20.0.0/16"
region = "us-central1"
network = google_compute_network.vpc_network.id # π μ°Έμ‘° (μμ‘΄μ± λ°μ)
}
# 3. λ°©νλ²½ κ·μΉ μμ± (SSH νμ©)
resource "google_compute_firewall" "allow_ssh" {
name = "allow-ssh"
network = google_compute_network.vpc_network.id
allow {
protocol = "tcp"
ports = ["22"]
}
source_ranges = ["0.0.0.0/0"] # μ£Όμ: μ€μ΅μ©μΌλ‘λ§ μ 체 νμ©
}
# 4. VM μΈμ€ν΄μ€ μμ± (μλΈλ· μμ λ°°μΉ)
resource "google_compute_instance" "vm_instance" {
name = "terraform-instance-2"
machine_type = "e2-micro" # β¨ Free Tier
zone = "us-central1-a"
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
}
}
network_interface {
subnetwork = google_compute_subnetwork.subnetwork.id # π μ°Έμ‘°
access_config {
# Public IP λΆμ¬
}
}
}2. νμ΅ ν¬μΈνΈ (Dependency Graph)
Terraformμ μ μ½λλ₯Ό λ³΄κ³ μ€ν μμλ₯Ό μλμΌλ‘ κ²°μ ν©λλ€.
vpc_networkμμ± (λ 립μ )subnetworkμμ± (vpc_networkIDκ° νμνλ―λ‘ 1λ² ν μ€ν)vm_instanceμμ± (subnetworkIDκ° νμνλ―λ‘ 2λ² ν μ€ν)
3. μ€μ΅ κ°μ΄λ
terraform initterraform apply- νμΈ: GCP μ½μ > VPC λ€νΈμν¬μμ
terraform-networkμ10.20.0.0/16λμ νμΈ. terraform destroy(νμ)
4. π€ Gemini Prompt Tip (μ μ μμ²λ²)
볡μ‘ν μμ‘΄μ± κ΄κ³λ μ¬μ λͺ©λ‘(List)μΌλ‘ λͺ νν μ λ¬νμΈμ.
Prompt:
Generate Terraform configuration for a custom VPC network and a VM instance based on the following specifications: * VPC Name: terraform-network * Subnet Name: terraform-subnetwork * Subnet Region: us-central1 * Subnet Range: 10.20.0.0/16 * VM Name: terraform-instance-2 * VM Machine Type: e2-micro * VM Zone: us-central1-a * Dependency: The VM must use the custom subnet created above.
Geminiλ depends_onμ λͺ
μνκ±°λ 리μμ€ ID μ°Έμ‘°λ₯Ό ν΅ν΄ μμ‘΄μ±μ μ¬λ°λ₯΄κ² μ€μ ν μ½λλ₯Ό μμ±ν©λλ€.
Supported by gemini-3.0-pro preview