Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement