Skip to content
This repository was archived by the owner on Jul 12, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions terraform/database.tf
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,51 @@ resource "google_sql_database_instance" "db-inst" {
]
}

resource "google_sql_database_instance" "replicas" {
for_each = toset(var.database_failover_replica_regions)

project = var.project
region = each.key
database_version = "POSTGRES_12"
name = "${var.database_name}-${each.key}"

master_instance_name = google_sql_database_instance.db-inst.name

// These are REGIONAL replicas, which cannot auto-failover. The default
// configuration has auto-failover in zones. This is for super disaster
// recovery in which an entire region is down for an extended period of time.
replica_configuration {
failover_target = false
}

settings {
tier = var.database_tier
disk_size = var.database_disk_size_gb
availability_type = "ZONAL"
pricing_plan = "PACKAGE"

database_flags {
name = "autovacuum"
value = "on"
}

database_flags {
name = "max_connections"
value = var.database_max_connections
}

ip_configuration {
require_ssl = true
private_network = google_service_networking_connection.private_vpc_connection.network
}
}

depends_on = [
google_project_service.services["sqladmin.googleapis.com"],
google_project_service.services["sql-component.googleapis.com"],
]
}

resource "google_sql_database" "db" {
project = var.project
instance = google_sql_database_instance.db-inst.name
Expand Down
7 changes: 7 additions & 0 deletions terraform/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ variable "database_backup_schedule" {
description = "Cron schedule in which to do a full backup of the database to Cloud Storage."
}

variable "database_failover_replica_regions" {
type = list(string)
default = []

description = "List of regions in which to create failover replicas. The default configuration is resistant to zonal outages. This will increase costs."
}

variable "storage_location" {
type = string
default = "US"
Expand Down