# Cloud SQL instance with read replicas This module manages the creation of Cloud SQL instances with potential read replicas in other regions. It can also create an initial set of users and databases via the `users` and `databases` parameters. Note that this module assumes that some options are the same for both the primary instance and all the replicas (e.g. tier, disks, labels, flags, etc). *Warning:* if you use the `users` field, you terraform state will contain each user's password in plain text. ## Simple example This example shows how to setup a project, VPC and a standalone Cloud SQL instance. ```hcl module "project" { source = "./fabric/modules/project" billing_account = var.billing_account_id parent = var.organization_id name = "my-db-project" services = [ "servicenetworking.googleapis.com" ] } module "vpc" { source = "./fabric/modules/net-vpc" project_id = module.project.project_id name = "my-network" psa_config = { ranges = { cloud-sql = "10.60.0.0/16" } } } module "db" { source = "./fabric/modules/cloudsql-instance" project_id = module.project.project_id network_config = { connectivity = { psa_config = { private_network = module.vpc.self_link } } } name = "db" region = "europe-west1" database_version = "POSTGRES_13" tier = "db-g1-small" } # tftest modules=3 resources=11 inventory=simple.yaml ``` ## Cross-regional read replica ```hcl module "db" { source = "./fabric/modules/cloudsql-instance" project_id = var.project_id network_config = { connectivity = { psa_config = { private_network = var.vpc.self_link } } } prefix = "myprefix" name = "db" region = "europe-west1" database_version = "POSTGRES_13" tier = "db-g1-small" replicas = { replica1 = { region = "europe-west3", encryption_key_name = null } replica2 = { region = "us-central1", encryption_key_name = null } } } # tftest modules=1 resources=3 inventory=replicas.yaml ``` ## Custom flags, databases and users ```hcl module "db" { source = "./fabric/modules/cloudsql-instance" project_id = var.project_id network_config = { connectivity = { psa_config = { private_network = var.vpc.self_link } } } name = "db" region = "europe-west1" database_version = "MYSQL_8_0" tier = "db-g1-small" flags = { disconnect_on_expired_password = "on" } databases = [ "people", "departments" ] users = { # generatea password for user1 user1 = { password = null } # assign a password to user2 user2 = { password = "mypassword" } } } # tftest modules=1 resources=6 inventory=custom.yaml ``` ### CMEK encryption ```hcl module "project" { source = "./fabric/modules/project" billing_account = var.billing_account_id parent = var.organization_id name = "my-db-project" services = [ "servicenetworking.googleapis.com", "sqladmin.googleapis.com", ] } module "kms" { source = "./fabric/modules/kms" project_id = module.project.project_id keyring = { name = "keyring" location = var.region } keys = { key-sql = { iam = { "roles/cloudkms.cryptoKeyEncrypterDecrypter" = [ "serviceAccount:${module.project.service_accounts.robots.sqladmin}" ] } } } } module "db" { source = "./fabric/modules/cloudsql-instance" project_id = module.project.project_id encryption_key_name = module.kms.keys["key-sql"].id network_config = { connectivity = { psa_config = { private_network = var.vpc.self_link } } } name = "db" region = var.region database_version = "POSTGRES_13" tier = "db-g1-small" } # tftest modules=3 resources=10 ``` ### Instance with PSC enabled ```hcl module "db" { source = "./fabric/modules/cloudsql-instance" project_id = var.project_id network_config = { connectivity = { psc_allowed_consumer_projects = ["my-project-id"] } } prefix = "myprefix" name = "db" region = "europe-west1" availability_type = "REGIONAL" database_version = "POSTGRES_13" tier = "db-g1-small" } # tftest modules=1 resources=1 ``` ### Enable public IP Use `ipv_enabled` to create instances with a public IP. ```hcl module "db" { source = "./fabric/modules/cloudsql-instance" project_id = var.project_id network_config = { connectivity = { public_ipv4 = true psa_config = { private_network = var.vpc.self_link } } } name = "db" region = "europe-west1" tier = "db-g1-small" database_version = "MYSQL_8_0" replicas = { replica1 = { region = "europe-west3", encryption_key_name = null } } } # tftest modules=1 resources=2 inventory=public-ip.yaml ``` ### Query Insights Provide `insights_config` (can be just empty `{}`) to enable [Query Insights](https://cloud.google.com/sql/docs/postgres/using-query-insights) ```hcl module "db" { source = "./fabric/modules/cloudsql-instance" project_id = var.project_id network_config = { connectivity = { psa_config = { private_network = var.vpc.self_link } } } name = "db" region = "europe-west1" database_version = "POSTGRES_13" tier = "db-g1-small" insights_config = { query_string_length = 2048 } } # tftest modules=1 resources=1 inventory=insights.yaml ``` ### Maintenance Config Provide `maintenance_config` (can be just empty `{}`) to enable [Maintenance](https://cloud.google.com/sql/docs/postgres/maintenance) ```hcl module "db" { source = "./fabric/modules/cloudsql-instance" project_id = var.project_id network_config = { connectivity = { psa_config = { private_network = var.vpc.self_link } } } name = "db" region = "europe-west1" database_version = "POSTGRES_13" tier = "db-g1-small" maintenance_config = {} } # tftest modules=1 resources=1 ``` ### SSL Config Provide `ssl` (can be just empty `{}`) to enable [SSL](https://cloud.google.com/sql/docs/postgres/configure-ssl-instance) ```hcl module "db" { source = "./fabric/modules/cloudsql-instance" project_id = var.project_id network_config = { connectivity = { psa_config = { private_network = var.vpc.self_link } } } name = "db" region = "europe-west1" database_version = "POSTGRES_13" tier = "db-g1-small" ssl = {} } # tftest modules=1 resources=1 ``` ## Variables | name | description | type | required | default | |---|---|:---:|:---:|:---:| | [database_version](variables.tf#L68) | Database type and version to create. | string | ✓ | | | [name](variables.tf#L172) | Name of primary instance. | string | ✓ | | | [network_config](variables.tf#L177) | Network configuration for the instance. Only one between private_network and psc_config can be used. | object({…}) | ✓ | | | [project_id](variables.tf#L210) | The ID of the project where this instances will be created. | string | ✓ | | | [region](variables.tf#L215) | Region of the primary instance. | string | ✓ | | | [tier](variables.tf#L258) | The machine type to use for the instances. | string | ✓ | | | [activation_policy](variables.tf#L16) | This variable specifies when the instance should be active. Can be either ALWAYS, NEVER or ON_DEMAND. Default is ALWAYS. | string | | "ALWAYS" | | [availability_type](variables.tf#L27) | Availability type for the primary replica. Either `ZONAL` or `REGIONAL`. | string | | "ZONAL" | | [backup_configuration](variables.tf#L33) | Backup settings for primary instance. Will be automatically enabled if using MySQL with one or more replicas. | object({…}) | | {…} | | [collation](variables.tf#L56) | The name of server instance collation. | string | | null | | [connector_enforcement](variables.tf#L62) | Specifies if connections must use Cloud SQL connectors. | string | | null | | [databases](variables.tf#L73) | Databases to create once the primary instance is created. | list(string) | | null | | [disk_autoresize_limit](variables.tf#L79) | The maximum size to which storage capacity can be automatically increased. The default value is 0, which specifies that there is no limit. | number | | 0 | | [disk_size](variables.tf#L85) | Disk size in GB. Set to null to enable autoresize. | number | | null | | [disk_type](variables.tf#L91) | The type of data disk: `PD_SSD` or `PD_HDD`. | string | | "PD_SSD" | | [edition](variables.tf#L97) | The edition of the instance, can be ENTERPRISE or ENTERPRISE_PLUS. | string | | "ENTERPRISE" | | [encryption_key_name](variables.tf#L103) | The full path to the encryption key used for the CMEK disk encryption of the primary instance. | string | | null | | [flags](variables.tf#L109) | Map FLAG_NAME=>VALUE for database-specific tuning. | map(string) | | null | | [gcp_deletion_protection](variables.tf#L115) | Set Google's deletion protection attribute which applies across all surfaces (UI, API, & Terraform). | bool | | true | | [insights_config](variables.tf#L122) | Query Insights configuration. Defaults to null which disables Query Insights. | object({…}) | | null | | [labels](variables.tf#L133) | Labels to be attached to all instances. | map(string) | | null | | [maintenance_config](variables.tf#L139) | Set maintenance window configuration and maintenance deny period (up to 90 days). Date format: 'yyyy-mm-dd'. | object({…}) | | {} | | [prefix](variables.tf#L200) | Optional prefix used to generate instance names. | string | | null | | [replicas](variables.tf#L220) | Map of NAME=> {REGION, KMS_KEY} for additional read replicas. Set to null to disable replica creation. | map(object({…})) | | {} | | [root_password](variables.tf#L229) | Root password of the Cloud SQL instance. Required for MS SQL Server. | string | | null | | [ssl](variables.tf#L235) | Setting to enable SSL, set config and certificates. | object({…}) | | {} | | [terraform_deletion_protection](variables.tf#L251) | Prevent terraform from deleting instances. | bool | | true | | [time_zone](variables.tf#L263) | The time_zone to be used by the database engine (supported only for SQL Server), in SQL Server timezone format. | string | | null | | [users](variables.tf#L269) | Map of users to create in the primary instance (and replicated to other replicas). For MySQL, anything after the first `@` (if present) will be used as the user's host. Set PASSWORD to null if you want to get an autogenerated password. The user types available are: 'BUILT_IN', 'CLOUD_IAM_USER' or 'CLOUD_IAM_SERVICE_ACCOUNT'. | map(object({…})) | | null | ## Outputs | name | description | sensitive | |---|---|:---:| | [client_certificates](outputs.tf#L24) | The CA Certificate used to connect to the SQL Instance via SSL. | ✓ | | [connection_name](outputs.tf#L30) | Connection name of the primary instance. | | | [connection_names](outputs.tf#L35) | Connection names of all instances. | | | [dns_name](outputs.tf#L43) | The dns name of the instance. | | | [dns_names](outputs.tf#L48) | Dns names of all instances. | | | [id](outputs.tf#L56) | Fully qualified primary instance id. | | | [ids](outputs.tf#L61) | Fully qualified ids of all instances. | | | [instances](outputs.tf#L69) | Cloud SQL instance resources. | ✓ | | [ip](outputs.tf#L75) | IP address of the primary instance. | | | [ips](outputs.tf#L80) | IP addresses of all instances. | | | [name](outputs.tf#L88) | Name of the primary instance. | | | [names](outputs.tf#L93) | Names of all instances. | | | [psc_service_attachment_link](outputs.tf#L101) | The link to service attachment of PSC instance. | | | [psc_service_attachment_links](outputs.tf#L106) | Links to service attachment of PSC instances. | | | [self_link](outputs.tf#L114) | Self link of the primary instance. | | | [self_links](outputs.tf#L119) | Self links of all instances. | | | [user_passwords](outputs.tf#L127) | Map of containing the password of all users created through terraform. | ✓ |