Why Infrastructure as Code?
Managing infrastructure manually is error-prone and hard to replicate. Terraform lets you define infrastructure in code, enabling version control, peer review, and reproducible environments.
Getting Started
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "~> 5.0"
}
}
}
provider "google" {
project = "my-project"
region = "asia-southeast1"
}
resource "google_cloud_run_service" "app" {
name = "my-app"
location = "asia-southeast1"
template {
spec {
containers {
image = "gcr.io/my-project/app:latest"
}
}
}
}State Management
Store Terraform state remotely in Google Cloud Storage for team collaboration:
terraform {
backend "gcs" {
bucket = "my-terraform-state"
prefix = "terraform/state"
}
}Best Practices
- Use modules for reusable components
- Separate environments with workspaces or directories
- Always run
terraform planbeforeapply - Use remote state with locking