Skip to content

Instantly share code, notes, and snippets.

@Rukeith Rukeith/main.tf
Last active Aug 14, 2019

Embed
What would you like to do?
Terraform for Cloud SQL HA
terraform {
backend "gcs" {
credentials = "credential.json"
bucket = "demo"
prefix = "terraform/state"
}
}
provider "google-beta" {
credentials = "${file("credential.json")}"
project = "${var.project}"
region = "${var.region}"
zone = "${var.zone}"
}
resource "random_id" "db_name_suffix" {
byte_length = 4
}
resource "google_service_networking_connection" "vpc_connection" {
provider = "google-beta"
network = "${var.private_network.network}"
reserved_peering_ranges = ["${var.private_network.peering_ranges}"]
service = "${var.private_network.vpc_service}"
lifecycle {
prevent_destroy = false
}
}
variable "database_version" {
type = "string"
default = "MYSQL_5_7"
}
resource "google_sql_database_instance" "production" {
provider = "google-beta"
name = "demo-${random_id.db_name_suffix.hex}"
database_version = "${var.database_version}"
depends_on = [
"google_service_networking_connection.vpc_connection"
]
settings {
activation_policy = "ALWAYS"
disk_autoresize = true
disk_size = 10
disk_type = "PD_SSD"
tier = "db-n1-standard-1"
user_labels = {
project = "demo"
role = "production"
service = "database"
}
database_flags {
name = "default_time_zone"
value = "+00:00"
}
backup_configuration {
binary_log_enabled = true
enabled = true
start_time = "18:00"
}
ip_configuration {
ipv4_enabled = true
private_network = "${var.private_network.network}"
require_ssl = true
authorized_networks {
name = "Public"
value = "0.0.0.0/0"
}
}
maintenance_window {
day = 7
update_track = "stable"
}
}
lifecycle {
prevent_destroy = false
}
}
##########
##########
resource "google_sql_database" "production" {
provider = "google-beta"
name = "demo"
instance = "${google_sql_database_instance.production.name}"
charset = "${var.db_setting.charset}"
collation = "${var.db_setting.collation}"
depends_on = [
"google_sql_database_instance.production"
]
lifecycle {
prevent_destroy = false
}
}
######
######
resource "google_sql_user" "root" {
provider = "google-beta"
name = "root"
instance = "${google_sql_database_instance.production.name}"
password = "123456"
host = "%"
depends_on = [
"google_sql_database_instance.production"
]
lifecycle {
prevent_destroy = false
}
}
resource "google_sql_ssl_cert" "root_cert" {
provider = "google-beta"
common_name = "root"
instance = "${google_sql_database_instance.production.name}"
depends_on = [
"google_sql_database_instance.production"
]
lifecycle {
prevent_destroy = false
}
}
resource "google_sql_database_instance" "read-replica" {
provider = "google-beta"
name = "demo-replica-${random_id.db_name_suffix.hex}"
database_version = "${var.database_version}"
replication_type = "ASYNCHRONOUS"
master_instance_name = "${google_sql_database_instance.production.name}"
depends_on = [
"google_sql_database_instance.production"
]
replica_configuration {
failover_target = false
}
settings {
activation_policy = "ALWAYS"
crash_safe_replication = true
replication_type = "ASYNCHRONOUS"
tier = "db-n1-standard-1"
user_labels = {
project = "demo"
role = "read-replica"
service = "database"
}
database_flags {
name = "default_time_zone"
value = "+00:00"
}
backup_configuration {
binary_log_enabled = true
}
ip_configuration {
ipv4_enabled = true
private_network = "${var.private_network.network}"
require_ssl = true
authorized_networks {
name = "Public"
value = "0.0.0.0/0"
}
}
}
lifecycle {
prevent_destroy = false
}
}
variable "project" {
type = "string"
}
variable "region" {
type = "string"
default = "asia-east1"
}
variable "zone" {
type = "string"
default = "asia-east1-b"
}
variable "database_version" {
type = "string"
default = "MYSQL_5_7"
}
variable "private_network" {
type = "map"
default = {
network = "https://www.googleapis.com/compute/v1/projects/demo/global/networks/default"
peering_ranges = "google-managed-services-default"
vpc_service = "servicenetworking.googleapis.com"
}
}
variable "db_setting" {
type = "map"
default = {
charset = "utf8mb4"
collation = "utf8mb4_unicode_ci"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.