JohanCalle
7/30/2019 - 3:59 PM

SQL gcp

variable "stage" {
  type = "string"
}
variable "tier" {
  type = "string"
}
variable "zone" {
  type = "string"
}
variable "activation_policy" {
  type = "string"
}
variable "disk_autoresize" {
  type = "string"
}
variable "disk_size" {
  type = "string"
}
variable "disk_type" {
  type = "string"
}
variable "pricing_plan" {
  type = "string"
}
variable "storage" {
  type = "string"
}
variable "providerCredentials" {
  type= "string"
}
variable "db_name" {
  type = "string"
}
variable "providerProject" {
  type = "string"
}
variable "providerRegion" {
  type = "string"
}
variable "database_version" {
  type = "string"
}
variable "user_name" {
  type = "string"
}
variable "user_password" {
  type = "string"
}
variable "user_host" {
  type = "string"
}
variable "db_charset" {
  type = "string"
}
variable "db_collation" {
  type = "string"
}
variable "prefix" {
  type = "string"    
}
variable "name_instancia" {
  type = "string"
}

provider google {
credentials = "${var.providerCredentials}"
project = "${var.providerProject}"
region = "${var.providerRegion}"
}

resource "google_sql_database_instance" "db" {
  name             = "${var.stage}-${var.name_instancia}"
  project          = "${var.providerProject}"
  region           = "${var.providerRegion}"
  database_version = "${var.database_version}"

  settings {
    tier                        = "${var.tier}"
    activation_policy           = "${var.activation_policy}"
    disk_autoresize             = "${var.disk_autoresize}"
    disk_size                   = "${var.disk_size}"
    disk_type                   = "${var.disk_type}"
    pricing_plan                = "${var.pricing_plan}"
    ip_configuration {
        ipv4_enabled = "true"
        require_ssl = "false"
        private_network = "https://www.googleapis.com/compute/v1/projects/${var.providerProject}/global/networks/${var.stage}-vpc"
        }
    backup_configuration {
        enabled = "true"
        start_time = "23:00"
        }
    location_preference {
        zone = "${var.zone}"
        }
    maintenance_window {
        day = 7
        hour = 23
        update_track = "stable"
        }
    }
}

resource "google_sql_database" "db" {
  name      = "${var.db_name}"
  project   = "${var.providerProject}"
  instance  = "${google_sql_database_instance.db.name}"
  charset   = "${var.db_charset}"
  collation = "${var.db_collation}"
}

resource "google_sql_user" "db_user" {
  name     = "${var.user_name}"
  project  = "${var.providerProject}"
  instance = "${google_sql_database_instance.db.name}"
  host     = "${var.user_host}"
  password = "${var.user_password}"
}

output "instance_address" {
  value = "${google_sql_database_instance.db.ip_address.0.ip_address}"
}