Skip to content

Commit e26861e

Browse files
authored
feat!: adds availability_type for read_replicas (#329)
This is a breaking change! Adds support for setting availability_type at the read_replica level for mysql and postgresql
1 parent 445e7ac commit e26861e

File tree

16 files changed

+143
-7
lines changed

16 files changed

+143
-7
lines changed

build/int.cloudbuild.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,4 @@ tags:
192192
substitutions:
193193
_DOCKER_IMAGE_DEVELOPER_TOOLS: 'cft/developer-tools'
194194
_DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '1.3'
195-
_API_ACTIVATION_SECONDS_WAIT: '450'
195+
_API_ACTIVATION_SECONDS_WAIT: '600'

docs/upgrading_to_sql_db_12.0.0.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Upgrading to SQL DB 12.0.0
2+
3+
The 12.0.0 release of SQL DB is a backward incompatible release. This incompatibility affects configuration of read replicas for `mysql`, `postgresql` and `safer_mysql` submodules.
4+
5+
## Migration Instructions
6+
7+
### Add support for setting availability_type
8+
9+
Prior to the 12.0.0 release, all read replicas were created using the default availability type set to `ZONAL`. The addition of `availability_type` allows explicit setting of availability type for each read replica.
10+
11+
```hcl
12+
module "pg" {
13+
source = "GoogleCloudPlatform/sql-db/google//modules/postgresql"
14+
version = "~> 11.0"
15+
16+
project_id = var.project_id
17+
region = "europe-west1"
18+
zone = "europe-west1-d"
19+
name = "test"
20+
random_instance_name = true
21+
availability_type = "ZONAL"
22+
database_version = "POSTGRES_14"
23+
disk_type = "PD_HDD"
24+
disk_size = 10
25+
disk_autoresize = true
26+
create_timeout = "30m"
27+
28+
read_replicas = [
29+
{
30+
name = "0"
31+
zone = "europe-west1-d"
32+
tier = "db-f1-micro"
33+
disk_type = "PD_HDD"
34+
disk_size = 10
35+
disk_autoresize = true
36+
disk_autoresize_limit = 0
37+
encryption_key_name = null
38+
database_flags = []
39+
user_labels = {}
40+
41+
ip_configuration = {
42+
allocated_ip_range = null
43+
authorized_networks = []
44+
ipv4_enabled = true
45+
private_network = null
46+
require_ssl = false
47+
}
48+
},
49+
]
50+
}
51+
```
52+
53+
With the 12.0.0 release, the `availability_type` string variable is presented which allows users to set the availability type of their read replicas as `ZONAL` or `REGIONAL`.
54+
55+
```diff
56+
module "pg" {
57+
source = "GoogleCloudPlatform/sql-db/google//modules/postgresql"
58+
- version = "~> 11.0"
59+
+ version = "~> 12.0"
60+
61+
project_id = var.project_id
62+
region = "europe-west1"
63+
zone = "europe-west1-d"
64+
name = "test"
65+
random_instance_name = true
66+
availability_type = "ZONAL"
67+
database_version = "POSTGRES_14"
68+
disk_type = "PD_HDD"
69+
disk_size = 10
70+
disk_autoresize = true
71+
create_timeout = "30m"
72+
73+
read_replicas = [
74+
{
75+
name = "0"
76+
zone = "europe-west1-d"
77+
tier = "db-f1-micro"
78+
disk_type = "PD_HDD"
79+
disk_size = 10
80+
disk_autoresize = true
81+
+ availability_type = "ZONAL"
82+
disk_autoresize_limit = 0
83+
encryption_key_name = null
84+
database_flags = []
85+
user_labels = {}
86+
87+
ip_configuration = {
88+
allocated_ip_range = null
89+
authorized_networks = []
90+
ipv4_enabled = true
91+
private_network = null
92+
require_ssl = false
93+
}
94+
},
95+
]
96+
}
97+
```

examples/mysql-ha/main.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ module "mysql" {
8484
{
8585
name = "0"
8686
zone = "us-central1-a"
87+
availability_type = "ZONAL"
8788
tier = "db-n1-standard-1"
8889
ip_configuration = local.read_replica_ip_configuration
8990
database_flags = [{ name = "long_query_time", value = 1 }]
@@ -97,6 +98,7 @@ module "mysql" {
9798
{
9899
name = "1"
99100
zone = "us-central1-b"
101+
availability_type = "ZONAL"
100102
tier = "db-n1-standard-1"
101103
ip_configuration = local.read_replica_ip_configuration
102104
database_flags = [{ name = "long_query_time", value = 1 }]
@@ -110,6 +112,7 @@ module "mysql" {
110112
{
111113
name = "2"
112114
zone = "us-central1-c"
115+
availability_type = "ZONAL"
113116
tier = "db-n1-standard-1"
114117
ip_configuration = local.read_replica_ip_configuration
115118
database_flags = [{ name = "long_query_time", value = 1 }]

examples/postgresql-ha/main.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ module "pg" {
8383
{
8484
name = "0"
8585
zone = "us-central1-a"
86+
availability_type = "REGIONAL"
8687
tier = "db-custom-1-3840"
8788
ip_configuration = local.read_replica_ip_configuration
8889
database_flags = [{ name = "autovacuum", value = "off" }]
@@ -96,6 +97,7 @@ module "pg" {
9697
{
9798
name = "1"
9899
zone = "us-central1-b"
100+
availability_type = "REGIONAL"
99101
tier = "db-custom-1-3840"
100102
ip_configuration = local.read_replica_ip_configuration
101103
database_flags = [{ name = "autovacuum", value = "off" }]
@@ -109,6 +111,7 @@ module "pg" {
109111
{
110112
name = "2"
111113
zone = "us-central1-c"
114+
availability_type = "REGIONAL"
112115
tier = "db-custom-1-3840"
113116
ip_configuration = local.read_replica_ip_configuration
114117
database_flags = [{ name = "autovacuum", value = "off" }]

modules/mysql/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Note: CloudSQL provides [disk autoresize](https://cloud.google.com/sql/docs/mysq
3838
| random\_instance\_name | Sets random suffix at the end of the Cloud SQL resource name | `bool` | `false` | no |
3939
| read\_replica\_deletion\_protection | Used to block Terraform from deleting replica SQL Instances. | `bool` | `false` | no |
4040
| read\_replica\_name\_suffix | The optional suffix to add to the read instance name | `string` | `""` | no |
41-
| read\_replicas | List of read replicas to create. Encryption key is required for replica in different region. For replica in same region as master set encryption\_key\_name = null | <pre>list(object({<br> name = string<br> tier = string<br> zone = string<br> disk_type = string<br> disk_autoresize = bool<br> disk_autoresize_limit = number<br> disk_size = string<br> user_labels = map(string)<br> database_flags = list(object({<br> name = string<br> value = string<br> }))<br> ip_configuration = object({<br> authorized_networks = list(map(string))<br> ipv4_enabled = bool<br> private_network = string<br> require_ssl = bool<br> allocated_ip_range = string<br> })<br> encryption_key_name = string<br> }))</pre> | `[]` | no |
41+
| read\_replicas | List of read replicas to create. Encryption key is required for replica in different region. For replica in same region as master set encryption\_key\_name = null | <pre>list(object({<br> name = string<br> tier = string<br> zone = string<br> availability_type = string<br> disk_type = string<br> disk_autoresize = bool<br> disk_autoresize_limit = number<br> disk_size = string<br> user_labels = map(string)<br> database_flags = list(object({<br> name = string<br> value = string<br> }))<br> ip_configuration = object({<br> authorized_networks = list(map(string))<br> ipv4_enabled = bool<br> private_network = string<br> require_ssl = bool<br> allocated_ip_range = string<br> })<br> encryption_key_name = string<br> }))</pre> | `[]` | no |
4242
| region | The region of the Cloud SQL resources | `string` | `"us-central1"` | no |
4343
| tier | The tier for the master instance. | `string` | `"db-n1-standard-1"` | no |
4444
| update\_timeout | The optional timout that is applied to limit long database updates. | `string` | `"10m"` | no |

modules/mysql/read_replica.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ resource "google_sql_database_instance" "replicas" {
3838
settings {
3939
tier = lookup(each.value, "tier", var.tier)
4040
activation_policy = "ALWAYS"
41+
availability_type = lookup(each.value, "availability_type", var.availability_type)
4142

4243
dynamic "ip_configuration" {
4344
for_each = [lookup(each.value, "ip_configuration", {})]

modules/mysql/variables.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ variable "read_replicas" {
178178
name = string
179179
tier = string
180180
zone = string
181+
availability_type = string
181182
disk_type = string
182183
disk_autoresize = bool
183184
disk_autoresize_limit = number

modules/postgresql/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Note: CloudSQL provides [disk autoresize](https://cloud.google.com/sql/docs/mysq
4040
| random\_instance\_name | Sets random suffix at the end of the Cloud SQL resource name | `bool` | `false` | no |
4141
| read\_replica\_deletion\_protection | Used to block Terraform from deleting replica SQL Instances. | `bool` | `false` | no |
4242
| read\_replica\_name\_suffix | The optional suffix to add to the read instance name | `string` | `""` | no |
43-
| read\_replicas | List of read replicas to create. Encryption key is required for replica in different region. For replica in same region as master set encryption\_key\_name = null | <pre>list(object({<br> name = string<br> tier = string<br> zone = string<br> disk_type = string<br> disk_autoresize = bool<br> disk_autoresize_limit = number<br> disk_size = string<br> user_labels = map(string)<br> database_flags = list(object({<br> name = string<br> value = string<br> }))<br> ip_configuration = object({<br> authorized_networks = list(map(string))<br> ipv4_enabled = bool<br> private_network = string<br> require_ssl = bool<br> allocated_ip_range = string<br> })<br> encryption_key_name = string<br> }))</pre> | `[]` | no |
43+
| read\_replicas | List of read replicas to create. Encryption key is required for replica in different region. For replica in same region as master set encryption\_key\_name = null | <pre>list(object({<br> name = string<br> tier = string<br> availability_type = string<br> zone = string<br> disk_type = string<br> disk_autoresize = bool<br> disk_autoresize_limit = number<br> disk_size = string<br> user_labels = map(string)<br> database_flags = list(object({<br> name = string<br> value = string<br> }))<br> ip_configuration = object({<br> authorized_networks = list(map(string))<br> ipv4_enabled = bool<br> private_network = string<br> require_ssl = bool<br> allocated_ip_range = string<br> })<br> encryption_key_name = string<br> }))</pre> | `[]` | no |
4444
| region | The region of the Cloud SQL resources | `string` | `"us-central1"` | no |
4545
| tier | The tier for the master instance. | `string` | `"db-f1-micro"` | no |
4646
| update\_timeout | The optional timout that is applied to limit long database updates. | `string` | `"15m"` | no |

modules/postgresql/main.tf

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ locals {
3131
is_account_sa = trimsuffix(iu, "gserviceaccount.com") == iu ? false : true
3232
}]
3333

34+
// HA method using REGIONAL availability_type requires point in time recovery to be enabled
35+
point_in_time_recovery_enabled = var.availability_type == "REGIONAL" ? true : lookup(var.backup_configuration, "point_in_time_recovery_enabled", false)
36+
backups_enabled = var.availability_type == "REGIONAL" ? true : lookup(var.backup_configuration, "enabled", false)
37+
3438
retained_backups = lookup(var.backup_configuration, "retained_backups", null)
3539
retention_unit = lookup(var.backup_configuration, "retention_unit", null)
3640
}
@@ -59,10 +63,10 @@ resource "google_sql_database_instance" "default" {
5963
for_each = [var.backup_configuration]
6064
content {
6165
binary_log_enabled = false
62-
enabled = lookup(backup_configuration.value, "enabled", null)
66+
enabled = local.backups_enabled
6367
start_time = lookup(backup_configuration.value, "start_time", null)
6468
location = lookup(backup_configuration.value, "location", null)
65-
point_in_time_recovery_enabled = lookup(backup_configuration.value, "point_in_time_recovery_enabled", false)
69+
point_in_time_recovery_enabled = local.point_in_time_recovery_enabled
6670
transaction_log_retention_days = lookup(backup_configuration.value, "transaction_log_retention_days", null)
6771

6872
dynamic "backup_retention_settings" {

modules/postgresql/read_replica.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ resource "google_sql_database_instance" "replicas" {
3838
settings {
3939
tier = lookup(each.value, "tier", var.tier)
4040
activation_policy = "ALWAYS"
41+
availability_type = lookup(each.value, "availability_type", var.availability_type)
4142

4243
dynamic "ip_configuration" {
4344
for_each = [lookup(each.value, "ip_configuration", {})]

0 commit comments

Comments
 (0)