Skip to content

Instantly share code, notes, and snippets.

@Rukeith
Last active August 14, 2019 07:38

Revisions

  1. Rukeith revised this gist Aug 14, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion main.tf
    Original file line number Diff line number Diff line change
    @@ -150,7 +150,7 @@ resource "google_sql_database_instance" "read-replica" {
    "google_sql_database_instance.production"
    ]

    read_replica_configuration {
    replica_configuration {
    failover_target = false
    }

  2. Rukeith renamed this gist Aug 14, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. Rukeith created this gist Aug 14, 2019.
    38 changes: 38 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@

    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"
    }
    }
    193 changes: 193 additions & 0 deletions main.tf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,193 @@
    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"
    ]

    read_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
    }
    }