Support per-replica tier override in modules/cloudsql-instance (#4014)

* Support per-replica tier override in modules/cloudsql-instance

Replicas previously ignored any caller-provided tier and silently
inherited the primary's tier. Add an optional `tier` field to the
`replicas` map and use `coalesce(each.value.tier, var.tier)` in the
replica resource so per-replica overrides take effect while
preserving the inherit-from-primary default.

* Update README

---------

Co-authored-by: Julio Castillo <jccb@google.com>
This commit is contained in:
Pawan Roshan Gupta
2026-06-09 13:32:34 +05:30
committed by GitHub
parent 81e1a0c273
commit e753344bd2
4 changed files with 15 additions and 11 deletions

View File

@@ -2,7 +2,7 @@
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).
Note that this module assumes that some options are the same for both the primary instance and all the replicas (e.g. disks, labels, flags, etc). The `tier` can be overridden per-replica via `replicas.<name>.tier`; if unset, replicas inherit the primary's tier.
*Warning:* if you use the `users` field, you terraform state will contain each user's password in plain text.
@@ -109,7 +109,7 @@ module "db" {
replicas = {
replica1 = { region = "europe-west3" }
replica2 = { region = "us-central1" }
replica2 = { region = "us-central1", tier = "db-custom-2-7680" }
}
gcp_deletion_protection = false
terraform_deletion_protection = false
@@ -455,7 +455,7 @@ module "db" {
| [network_config](variables.tf#L218) | Network configuration for the instance. Only one between private_network and psc_config can be used. | <code>object&#40;&#123;&#8230;&#125;&#41;</code> | ✓ | |
| [project_id](variables.tf#L278) | The ID of the project where this instances will be created. | <code>string</code> | ✓ | |
| [region](variables.tf#L283) | Region of the primary instance. | <code>string</code> | ✓ | |
| [tier](variables.tf#L335) | The machine type to use for the instances. | <code>string</code> | ✓ | |
| [tier](variables.tf#L336) | The machine type to use for the instances. | <code>string</code> | ✓ | |
| [activation_policy](variables.tf#L17) | This variable specifies when the instance should be active. Can be either ALWAYS, NEVER or ON_DEMAND. Default is ALWAYS. | <code>string</code> | | <code>&#34;ALWAYS&#34;</code> |
| [availability_type](variables.tf#L28) | Availability type for the primary replica. Either `ZONAL` or `REGIONAL`. | <code>string</code> | | <code>&#34;ZONAL&#34;</code> |
| [backup_configuration](variables.tf#L34) | Backup settings for primary instance. Set to null to leave existing GCP backup settings unmanaged. When set, all fields are managed by Terraform including disabling backups when enabled=false. | <code>object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
@@ -478,12 +478,12 @@ module "db" {
| [managed_connection_pooling_config](variables.tf#L203) | Configuration for Managed Connection Pooling. NOTE: This feature is only available for PostgreSQL on Enterprise Plus edition instances. | <code>object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>&#123;&#125;</code> |
| [password_validation_policy](variables.tf#L254) | Password validation policy configuration for instances. | <code>object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [prefix](variables.tf#L268) | Optional prefix used to generate instance names. | <code>string</code> | | <code>null</code> |
| [replicas](variables.tf#L288) | Map of NAME=> {REGION, KMS_KEY, AVAILABILITY_TYPE} for additional read replicas. Set to null to disable replica creation. | <code>map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> |
| [root_password](variables.tf#L299) | Root password of the Cloud SQL instance, or flag to create a random password. Required for MS SQL Server. | <code>object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>&#123;&#125;</code> |
| [ssl](variables.tf#L313) | Setting to enable SSL, set config and certificates. | <code>object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>&#123;&#125;</code> |
| [terraform_deletion_protection](variables.tf#L328) | Prevent terraform from deleting instances. | <code>bool</code> | | <code>true</code> |
| [time_zone](variables.tf#L340) | The time_zone to be used by the database engine (supported only for SQL Server), in SQL Server timezone format. | <code>string</code> | | <code>null</code> |
| [users](variables.tf#L346) | 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'. | <code>map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> |
| [replicas](variables.tf#L288) | Map of NAME=> {REGION, KMS_KEY, AVAILABILITY_TYPE, TIER} for additional read replicas. Set TIER to override the primary's machine type per replica. Set to null to disable replica creation. | <code>map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> |
| [root_password](variables.tf#L300) | Root password of the Cloud SQL instance, or flag to create a random password. Required for MS SQL Server. | <code>object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>&#123;&#125;</code> |
| [ssl](variables.tf#L314) | Setting to enable SSL, set config and certificates. | <code>object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>&#123;&#125;</code> |
| [terraform_deletion_protection](variables.tf#L329) | Prevent terraform from deleting instances. | <code>bool</code> | | <code>true</code> |
| [time_zone](variables.tf#L341) | The time_zone to be used by the database engine (supported only for SQL Server), in SQL Server timezone format. | <code>string</code> | | <code>null</code> |
| [users](variables.tf#L347) | 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'. | <code>map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> |
## Outputs

View File

@@ -302,7 +302,7 @@ resource "google_sql_database_instance" "replicas" {
master_instance_name = google_sql_database_instance.primary.name
settings {
tier = var.tier
tier = coalesce(each.value.tier, var.tier)
edition = var.edition
deletion_protection_enabled = var.gcp_deletion_protection
disk_autoresize = var.disk_size == null

View File

@@ -286,11 +286,12 @@ variable "region" {
}
variable "replicas" {
description = "Map of NAME=> {REGION, KMS_KEY, AVAILABILITY_TYPE} for additional read replicas. Set to null to disable replica creation."
description = "Map of NAME=> {REGION, KMS_KEY, AVAILABILITY_TYPE, TIER} for additional read replicas. Set TIER to override the primary's machine type per replica. Set to null to disable replica creation."
type = map(object({
region = string
encryption_key_name = optional(string)
availability_type = optional(string)
tier = optional(string)
}))
default = {}
nullable = false

View File

@@ -21,6 +21,7 @@ values:
region: europe-west8
settings:
- edition: ENTERPRISE
tier: db-g1-small
module.db.google_sql_database_instance.replicas["replica1"]:
clone: []
@@ -31,6 +32,7 @@ values:
region: europe-west3
settings:
- edition: ENTERPRISE
tier: db-g1-small
module.db.google_sql_database_instance.replicas["replica2"]:
clone: []
@@ -41,6 +43,7 @@ values:
region: us-central1
settings:
- edition: ENTERPRISE
tier: db-custom-2-7680
counts:
google_sql_database_instance: 3