Thursday, December 3, 2026 | London 10°C · Drizzle
DailyGlimpse

AI

Automating Google Cloud SQL Deployment with Terraform: A Step-by-Step Guide

Dreamset Services 2 min read

Google Cloud SQL is a fully managed relational database service that makes it easy to set up, maintain, and administer your databases in the cloud. In this walkthrough, we'll explore how to use Terraform, an infrastructure-as-code tool, to automate the deployment of a Cloud SQL instance on Google Cloud Platform (GCP).

Terraform allows you to define your cloud resources in declarative configuration files, which can be version-controlled and reused. This approach ensures consistency, reduces manual errors, and speeds up provisioning.

First, ensure you have a GCP project with billing enabled and the Cloud SQL Admin API activated. You'll also need the Google Cloud SDK installed and authenticated.

Create a directory for your Terraform configuration and add a main.tf file with the following content:

provider "google" {
project = "your-project-id"
region  = "us-central1"
}

resource "google_sql_database_instance" "default" {
name             = "my-cloudsql-instance"
database_version = "POSTGRES_14"
region           = "us-central1"
settings {
tier = "db-f1-micro"
ip_configuration {
ipv4_enabled = true
authorized_networks {
name  = "allow-all"
value = "0.0.0.0/0"
}
}
}
deletion_protection = false
}

Replace your-project-id with your actual GCP project ID. This configuration creates a PostgreSQL 14 instance with the smallest machine tier and permits all IP addresses for testing purposes. In production, you should restrict access.

Initialize Terraform with terraform init, then run terraform plan to preview changes, and finally terraform apply to create the instance. After a few minutes, your Cloud SQL instance will be ready.

Once the instance is up, you can connect to it using the provided IP address and credentials, or via Cloud SQL Proxy for enhanced security.

Terraform makes it easy to manage the entire lifecycle of your Cloud SQL instances, from creation to updates and destruction. By storing your configuration in a Git repository, you can collaborate with your team and maintain a single source of truth for your infrastructure.

For more advanced scenarios, consider using Terraform modules to encapsulate reusable configurations, and integrate with CI/CD pipelines for automated deployments.

React