diff --git a/modules/mysql/README.md b/modules/mysql/README.md index 7686730..eaad1fe 100644 --- a/modules/mysql/README.md +++ b/modules/mysql/README.md @@ -1,7 +1,9 @@ -# Cloud SQL Module +# MySQL Module -This module creates a [Google Cloud SQL](https://cloud.google.com/sql/) cluster. The cluster is managed by Google, -automating backups, replication, patches, and updates. +This module creates a [Google Cloud SQL](https://cloud.google.com/sql/) [MySQL](https://cloud.google.com/sql/docs/mysql/) cluster. +The cluster is managed by Google, automating backups, replication, patches, and updates. + +TODO: Figure out documentation format for separate modules. You can use Cloud SQL with either [MySQL](https://cloud.google.com/sql/docs/mysql/) or [PostgreSQL](https://cloud.google.com/sql/docs/postgres/). @@ -26,10 +28,8 @@ You can also use the [Cloud SQL Proxy](https://cloud.google.com/sql/docs/mysql/c This module provides the connection details as [Terraform output variables](https://www.terraform.io/intro/getting-started/outputs.html): -**TODO**: Connectivity and outputs below - -1. **Public IP** `private_ip`: The public endpoint for the cluster. +1. TODO: **Private IP** `private_ip`: The public endpoint for the cluster. 1. **Public IP** `public_ip`: The public endpoint for the cluster. 1. **Connection name** `connection_name`: The private endpoint for the cluster. 1. **Replica endpoints** `replica_endpoints`: A comma-separated list of all DB instance URLs in the cluster, including the primary and all diff --git a/modules/mysql/main.tf b/modules/mysql/main.tf index 0bc76c8..4334b91 100644 --- a/modules/mysql/main.tf +++ b/modules/mysql/main.tf @@ -1,41 +1,43 @@ # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # DEPLOY A CLOUD SQL CLUSTER -# This module deploys an Cloud SQL cluster. The cluster is managed by Google and automatically handles leader +# This module deploys a Cloud SQL MySQL cluster. The cluster is managed by Google and automatically handles leader # election, replication, failover, backups, patching, and encryption. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # ------------------------------------------------------------------------------ -# CREATE THE CLOUD SQL CLUSTER +# CREATE THE CLOUD SQL MYSQL CLUSTER # # NOTE: We have multiple google_sql_database_instance resources, based on # HA, encryption and replication configuration options. # ------------------------------------------------------------------------------ -resource "google_sql_database_instance" "default" { +resource "google_sql_database_instance" "master" { name = "${var.name}" project = "${var.project}" region = "${var.region}" database_version = "${var.engine}" - master_instance_name = "${var.master_instance_name}" settings { tier = "${var.machine_type}" activation_policy = "${var.activation_policy}" authorized_gae_applications = ["${var.authorized_gae_applications}"] disk_autoresize = "${var.disk_autoresize}" - backup_configuration = ["${var.backup_configuration}"] - ip_configuration = ["${var.ip_configuration}"] - location_preference = ["${var.location_preference}"] - maintenance_window = ["${var.maintenance_window}"] + + ip_configuration { + authorized_networks = ["${var.authorized_networks}"], + ipv4_enabled = "${var.publicly_accessible}" + } + + location_preference { + follow_gae_application = "${var.follow_gae_application}" + zone = "${var.zone}" + } + disk_size = "${var.disk_size}" disk_type = "${var.disk_type}" - pricing_plan = "${var.pricing_plan}" - replication_type = "${var.replication_type}" - database_flags = ["${var.flags}"] + database_flags = ["${var.database_flags}"] availability_type = "${var.availability_type}" } - - replica_configuration = ["${var.replica_configuration}"] } # ------------------------------------------------------------------------------ @@ -43,19 +45,17 @@ resource "google_sql_database_instance" "default" { # ------------------------------------------------------------------------------ resource "google_sql_database" "default" { - count = "${var.master_instance_name == "" ? 1 : 0}" name = "${var.db_name}" project = "${var.project}" - instance = "${google_sql_database_instance.default.name}" + instance = "${google_sql_database_instance.master.name}" charset = "${var.db_charset}" collation = "${var.db_collation}" } resource "google_sql_user" "default" { - count = "${var.master_instance_name == "" ? 1 : 0}" - name = "${var.db_name}" + name = "${var.master_username}" project = "${var.project}" - instance = "${google_sql_database_instance.default.name}" - host = "${var.db_user_host}" - password = "${var.db_password}" + instance = "${google_sql_database_instance.master.name}" + host = "${var.master_host}" + password = "${var.master_password}" } \ No newline at end of file diff --git a/modules/mysql/outputs.tf b/modules/mysql/outputs.tf index 925b47e..26971f0 100644 --- a/modules/mysql/outputs.tf +++ b/modules/mysql/outputs.tf @@ -1,19 +1,29 @@ -output instance_name { +output "instance_name" { description = "The name of the database instance" - value = "${google_sql_database_instance.default.name}" + value = "${google_sql_database_instance.master.name}" } -output instance_address { +output "public_ip" { description = "The IPv4 address of the master database instance" - value = "${google_sql_database_instance.default.ip_address.0.ip_address}" + value = "${var.publicly_accessible ? google_sql_database_instance.master.ip_address.0.ip_address : ""}" } -output instance_address_time_to_retire { - description = "The time the master instance IP address will be reitred. RFC 3339 format." - value = "${google_sql_database_instance.default.ip_address.0.time_to_retire}" -} - -output self_link { +output "instance_self_link" { description = "Self link to the master instance" - value = "${google_sql_database_instance.default.self_link}" + value = "${google_sql_database_instance.master.self_link}" } + +output "db_name" { + description = "Name of the default database" + value = "${google_sql_database.default.name}" +} + +output "proxy_connection" { + value = "${var.project}:${var.region}:${google_sql_database_instance.master.name}" +} + +output "db_self_link" { + description = "Self link to the default database" + value = "${google_sql_database.default.self_link}" +} + diff --git a/modules/mysql/variables.tf b/modules/mysql/variables.tf index 49e3603..0f70f3f 100644 --- a/modules/mysql/variables.tf +++ b/modules/mysql/variables.tf @@ -12,21 +12,64 @@ variable "region" { } variable "name" { - description = "The name of the database instance." + description = "The name of the database instance. Note, after a name is used, it cannot be reused for up to one week. Use lowercase letters, numbers, and hyphens. Start with a letter." } variable "engine" { - description = "The engine version of the database, e.g. `MYSQL_5_7` or `POSTGRES_9_6`." + description = "The engine version of the database, e.g. `MYSQL_5_6` or `MYSQL_5_7`." } -variable "master_instance_name" { - description = "The name of the instance that will act as the master in the replication setup. Note, this requires the master to have binary_log_enabled set, as well as existing backups." +# TODO: Depending on how the replicas are set up, tweak this. +#variable "master_instance_name" { +# description = "The name of the instance that will act as the master in the replication setup. Note, this requires the master to have binary_log_enabled set, as well as existing backups." +# default = "" +#} + +variable "machine_type" { + description = "The machine type for the instance. See this page for supported tiers and pricing: https://cloud.google.com/sql/pricing" +} + +variable "db_name" { + description = "Name of for your database of up to 8 alpha-numeric characters." default = "" } -variable "machine_type" { - description = "The machine tier (First Generation) or type (Second Generation). See this page for supported tiers and pricing: https://cloud.google.com/sql/pricing" - default = "db-f1-micro" +variable "master_username" { + description = "The username for the master user." +} + +variable "master_password" { + description = "The password for the master user." +} + +# --------------------------------------------------------------------------------------------------------------------- +# OPTIONAL PARAMETERS +# Generally, these values won't need to be changed. +# --------------------------------------------------------------------------------------------------------------------- + +variable "activation_policy" { + description = "This specifies when the instance should be active. Can be either `ALWAYS`, `NEVER` or `ON_DEMAND`." + default = "ALWAYS" +} + +variable "authorized_networks" { + description = "A list of authorized CIDR-formatted IP address ranges that can connect to this DB." + type = "list" + default = [] + # Example: + # + # authorized_networks = [ + # { + # name = "all-inbound" # optional + # value = "0.0.0.0/0" + # } + # ] +} + +variable "authorized_gae_applications" { + description = "A list of Google App Engine (GAE) project names that are allowed to access this instance." + type = "list" + default = [] } variable "availability_type" { @@ -34,45 +77,33 @@ variable "availability_type" { default = "ZONAL" } -variable "db_name" { - description = "Name of the default database to create" - default = "default" -} - variable "db_charset" { - description = "The charset for the default database" + description = "The charset for the default database." default = "" } variable "db_collation" { - description = "The collation for the default database. Example for MySQL databases: 'utf8_general_ci', and Postgres: 'en_US.UTF8'" + description = "The collation for the default database. Example for MySQL databases: 'utf8_general_ci'." default = "" } -variable "db_user" { - description = "The name of the default user" - default = "default" -} - -variable "db_user_host" { - description = "The host for the default user" - default = "%" -} - -variable "db_password" { - description = "The password for the default user." - default = "" -} - -variable "activation_policy" { - description = "This specifies when the instance should be active. Can be either `ALWAYS`, `NEVER` or `ON_DEMAND`." - default = "ALWAYS" -} - -variable "authorized_gae_applications" { - description = "A list of Google App Engine (GAE) project names that are allowed to access this instance." - type = "list" +variable "database_flags" { + description = "List of Cloud SQL flags that are applied to the database server" + type = "list" default = [] + + # Example: + # + # database_flags = [ + # { + # name = "auto_increment_increment" + # value = "10" + # }, + # { + # name = "auto_increment_offset" + # value = "5" + # }, + #] } variable "disk_autoresize" { @@ -86,60 +117,27 @@ variable "disk_size" { } variable "disk_type" { - description = "Second generation only. The type of data disk: `PD_SSD` or `PD_HDD`." - default = "PD_SSD" + description = "The type of storage to use. Must be one of `PD_SSD` or `PD_HDD`." + default = "PD_HDD" } -variable "pricing_plan" { - description = "First generation only. Pricing plan for this instance, can be one of `PER_USE` or `PACKAGE`." - default = "PER_USE" +variable "follow_gae_application" { + description = "A GAE application whose zone to remain in. Must be in the same region as this instance." + default = "" } -variable "replication_type" { - description = "Replication type for this instance, can be one of `ASYNCHRONOUS` or `SYNCHRONOUS`." - default = "SYNCHRONOUS" +variable "zone" { + description = "Preferred zone for the instance." + default = "" } -variable "flags" { - description = "List of Cloud SQL flags that are applied to the database server" - default = [] - type = "list" +variable "master_host" { + description = "The host for the default user" + default = "%" } -# IGNORE EVERYTHING BELOW - -variable backup_configuration { - description = "The backup_configuration settings subblock for the database setings" - type = "map" - default = {} -} - -variable ip_configuration { - description = "The ip_configuration settings subblock" - type = "list" - default = [{}] -} - -variable location_preference { - description = "The location_preference settings subblock" - type = "list" - default = [] -} - -variable maintenance_window { - description = "The maintenance_window settings subblock" - type = "list" - default = [] -} - -variable replica_configuration { - description = "The optional replica_configuration block for the database instance" - type = "list" - default = [] -} - -# --------------------------------------------------------------------------------------------------------------------- -# OPTIONAL PARAMETERS -# Generally, these values won't need to be changed. -# --------------------------------------------------------------------------------------------------------------------- -# TODO: \ No newline at end of file +# In nearly all cases, databases should NOT be publicly accessible, however if you're migrating from a PAAS provider like Heroku to AWS, this needs to remain open to the internet. +variable "publicly_accessible" { + description = "WARNING: - In nearly all cases a database should NOT be publicly accessible. Only set this to true if you want the database open to the internet." + default = false +} \ No newline at end of file