Skip to content

Instantly share code, notes, and snippets.

@ellisio
Created April 25, 2019 20:36
Show Gist options
  • Save ellisio/935d1e697dc3aac7902482c8518049c2 to your computer and use it in GitHub Desktop.
Save ellisio/935d1e697dc3aac7902482c8518049c2 to your computer and use it in GitHub Desktop.
resource "google_sql_database_instance" "master" {
provider = "google-beta"
name = "${local.name}-master"
database_version = "${local.workspace_config["sql_version"]}"
settings {
tier = "${local.workspace_config["sql_tier"]}"
disk_type = "PD_SSD"
disk_size = "${local.workspace_config["sql_disk_size"]}"
disk_autoresize = true
activation_policy = "ALWAYS"
availability_type = "ZONAL"
replication_type = "SYNCHRONOUS"
ip_configuration {
require_ssl = false
ipv4_enabled = true
private_network = "${google_compute_network.network.self_link}"
}
location_preference {
zone = "${local.zone}-${local.workspace_config["sql_master_zone"]}"
}
backup_configuration {
enabled = true
binary_log_enabled = true
start_time = "02:30" // 2:30 AM UTC
}
maintenance_window {
day = 7 // Sunday
hour = 23 // 11 PM UTC
update_track = "stable"
}
}
timeouts {
create = "20m"
update = "20m"
delete = "20m"
}
depends_on = [
"google_service_networking_connection.private_vpc",
]
}
resource "google_sql_database_instance" "failover" {
provider = "google-beta"
name = "${local.name}-failover"
database_version = "${local.workspace_config["sql_version"]}"
master_instance_name = "${google_sql_database_instance.master.name}"
replica_configuration {
failover_target = true
}
settings {
tier = "${local.workspace_config["sql_tier"]}"
disk_type = "PD_SSD"
disk_size = "${local.workspace_config["sql_disk_size"]}"
disk_autoresize = true
availability_type = "ZONAL"
replication_type = "SYNCHRONOUS"
ip_configuration {
require_ssl = false
ipv4_enabled = true
private_network = "${google_compute_network.network.self_link}"
}
location_preference {
zone = "${local.zone}-${local.workspace_config["sql_failover_zone"]}"
}
}
}
resource "random_string" "apps_password" {
length = 32
special = true
}
resource "google_sql_user" "apps" {
name = "apps"
instance = "${google_sql_database_instance.master.name}"
host = "%"
password = "${random_string.apps_password.result}"
}
output "cloudsql_apps_master_public_ip" {
value = "${google_sql_database_instance.master.public_ip_address}"
}
# output "cloudsql_apps_master_private_ip" {
# value = "${google_sql_database_instance.master.private_ip_address}"
# }
output "cloudsql_apps_username" {
value = "${google_sql_user.apps.name}"
}
output "cloudsql_apps_password" {
value = "${google_sql_user.apps.password}"
sensitive = true
}
resource "google_compute_network" "network" {
provider = "google-beta"
name = "${local.name}"
auto_create_subnetworks = false
routing_mode = "REGIONAL"
}
resource "google_compute_subnetwork" "subnetwork" {
provider = "google-beta"
name = "${local.name}"
network = "${google_compute_network.network.name}"
ip_cidr_range = "10.10.10.0/24"
depends_on = [
"google_compute_network.network",
]
}
resource "google_compute_global_address" "private_ip" {
provider = "google-beta"
name = "${local.name}"
purpose = "VPC_PEERING"
address_type = "INTERNAL"
prefix_length = 16
network = "${google_compute_network.network.self_link}"
}
resource "google_service_networking_connection" "private_vpc" {
provider = "google-beta"
network = "${google_compute_network.network.self_link}"
service = "servicenetworking.googleapis.com"
reserved_peering_ranges = ["${google_compute_global_address.private_ip.name}"]
}
provider "google" {
version = "~> 2.5"
project = "arcadia-apps-238517"
region = "us-central1"
zone = "us-central1"
}
provider "google-beta" {
version = "~> 2.5"
project = "arcadia-apps-238517"
region = "us-central1"
zone = "us-central1"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment