GC-301e · Module 3
Terraform with Gemini CLI
4 min read
Terraform and Gemini CLI form a powerful combination for infrastructure development. Gemini can generate Terraform HCL from natural language descriptions, explain existing Terraform state, plan changes with dry-run analysis, and troubleshoot apply failures. The key insight: Gemini's code generation capabilities extend to infrastructure code. "Create a Terraform module for a Cloud Run service with VPC connector, custom domain, and Cloud SQL connection" produces a complete, valid HCL module.
The workflow pattern for Terraform with Gemini CLI: describe the infrastructure change in natural language, let Gemini generate or modify the HCL, run terraform plan through Gemini to review the change set, then terraform apply to execute. Gemini can interpret the plan output — it understands resource additions, modifications, and deletions, and can flag risky changes like security group modifications or database instance replacements. This conversational review layer catches infrastructure mistakes before they reach production.
# "Create a Cloud Run service with VPC connector and Cloud SQL"
resource "google_cloud_run_v2_service" "api" {
name = "my-api"
location = var.region
template {
containers {
image = var.container_image
env {
name = "DATABASE_URL"
value_source {
secret_key_ref {
secret = google_secret_manager_secret.db_url.secret_id
version = "latest"
}
}
}
resources {
limits = {
cpu = "1"
memory = "512Mi"
}
}
}
vpc_access {
connector = google_vpc_access_connector.connector.id
egress = "PRIVATE_RANGES_ONLY"
}
scaling {
min_instance_count = 1
max_instance_count = 10
}
}
}
resource "google_vpc_access_connector" "connector" {
name = "my-api-connector"
region = var.region
ip_cidr_range = "10.8.0.0/28"
network = "default"
}