Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Switch to Postgres 13, add optional regional replicas #1172

Merged
merged 1 commit into from
Nov 18, 2020
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
1 change: 0 additions & 1 deletion terraform-e2e-ci/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ module "en" {

project = var.project
cloudsql_disk_size_gb = 500
db_name = "en-server-${random_string.suffix.result}"
kms_export_signing_key_ring_name = "export-signing-${random_string.suffix.result}"
kms_revision_tokens_key_ring_name = "revision-tokens-${random_string.suffix.result}"

Expand Down
54 changes: 43 additions & 11 deletions terraform/database.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ resource "google_sql_database_instance" "db-inst" {
project = data.google_project.project.project_id
region = var.db_location
database_version = var.db_version
name = var.db_name

settings {
tier = var.cloudsql_tier
Expand Down Expand Up @@ -51,26 +50,59 @@ resource "google_sql_database_instance" "db-inst" {
}
}

lifecycle {
# This prevents accidental deletion of the database.
prevent_destroy = true
depends_on = [
google_project_service.services["sql-component.googleapis.com"],
]
}

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

project = var.project
region = each.key
database_version = var.db_version

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.cloudsql_tier
disk_size = var.cloudsql_disk_size_gb
availability_type = "ZONAL"
pricing_plan = "PACKAGE"

# Earlier versions of the database had a different name, and its not
# possible to rename Cloud SQL instances.
ignore_changes = [
name,
]
database_flags {
name = "autovacuum"
value = "on"
}

database_flags {
name = "max_connections"
value = var.cloudsql_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 = data.google_project.project.project_id
instance = google_sql_database_instance.db-inst.name
name = "main"
name = "key"
}

resource "google_sql_ssl_cert" "db-cert" {
Expand All @@ -86,7 +118,7 @@ resource "random_password" "db-password" {

resource "google_sql_user" "user" {
instance = google_sql_database_instance.db-inst.name
name = "notification"
name = "key"
password = random_password.db-password.result
}

Expand Down
15 changes: 8 additions & 7 deletions terraform/vars.tf
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,20 @@ variable "db_location" {
default = "us-central1"
}

# The name of the database.
variable "db_name" {
type = string
default = "en-server"
}

variable "db_version" {
type = string
default = "POSTGRES_11"
default = "POSTGRES_13"

description = "Version of the database to use. Must be at least 11 or higher."
}

variable "db_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."
}

# The region for the networking components.
# https://cloud.google.com/compute/docs/regions-zones
variable "network_location" {
Expand Down