DEV Community

Cover image for GCP Cloud SQL + Terraform: Quick Start Guide
TerraformMonkey
TerraformMonkey

Posted on

GCP Cloud SQL + Terraform: Quick Start Guide

So you want to spin up a Cloud SQL instance on GCP but avoid endless ClickOps? Terraform has your back. With just a few lines of HCL, you can go from nothing → fully working database in minutes.

This guide walks you through the essentials and gives you a safe, production-ready starting point.


🏗️ Why use Terraform for Cloud SQL?

Sure, you could create your database via the GCP console... but that’s fragile and error-prone. Terraform gives you:

  • Version control – every DB change tracked in Git
  • Repeatability – no more “it works on my account” setups
  • Collaboration – teammates share the same IaC definitions
  • Safety – drift detection, plan previews, and easier rollbacks

👉 For broader advice, check out Terraform GCP Provider Best Practices.


⚡ Minimal Example: Postgres 15 on GCP

Here’s the simplest setup to get you started:

resource "google_sql_database_instance" "default" { name = "example-sql" database_version = "POSTGRES_15" region = "us-central1" settings { tier = "db-f1-micro" } } resource "google_sql_user" "users" { name = "example-user" instance = google_sql_database_instance.default.name password = var.db_password } resource "google_sql_database" "database" { name = "example-db" instance = google_sql_database_instance.default.name } 
Enter fullscreen mode Exit fullscreen mode

📖 See the Terraform Google Provider docs for all available options.


🔒 Handling Passwords Safely

⚠️ Never hardcode DB passwords in your .tf files. Instead:

export TF_VAR_db_password="super-secret" 
Enter fullscreen mode Exit fullscreen mode

Terraform automatically picks this up when you run terraform apply.


🛠️ Hardening for Production

The above works fine for demos or dev environments. For production, consider adding:

  • Automated backups + Point-in-Time Recovery (PITR)
  • Maintenance windows for predictable updates
  • Private IPs to keep DB traffic off the public internet
  • High availability replicas

These settings can all be added inside the settings {} block of your instance.


🚀 Next Steps

If you want to go beyond basics, you can modularize this setup and reuse it across projects. A Terraform module helps you:

  • Standardize DB configurations
  • Apply org-wide policies (naming, networking, backups)
  • Scale faster without duplicating boilerplate

👉 Learn more about scaling with Terraform at scale.


💬 Over to you!

How are you managing Cloud SQL on GCP today? Do you keep it simple with raw resources, or wrap things into reusable modules?

Drop your thoughts in the comments 👇

👉 Want the full version? Read the complete GCP Cloud SQL Terraform Quick Start Guide.

Top comments (0)