Skip to content

Commit 426724a

Browse files
authored
feat: Add CMEK support for cross-region read replicas (#251)
BREAKING CHANGE: `var.read_replicas` now requires an encryption key name. Set `encryption_key_name = null` to preserve the old behavior.
1 parent e1a6fc7 commit 426724a

File tree

10 files changed

+73
-60
lines changed

10 files changed

+73
-60
lines changed

examples/mysql-ha/main.tf

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -80,37 +80,40 @@ module "mysql" {
8080
read_replica_name_suffix = "-test"
8181
read_replicas = [
8282
{
83-
name = "0"
84-
zone = "us-central1-a"
85-
tier = "db-n1-standard-1"
86-
ip_configuration = local.read_replica_ip_configuration
87-
database_flags = [{ name = "long_query_time", value = 1 }]
88-
disk_autoresize = null
89-
disk_size = null
90-
disk_type = "PD_HDD"
91-
user_labels = { bar = "baz" }
83+
name = "0"
84+
zone = "us-central1-a"
85+
tier = "db-n1-standard-1"
86+
ip_configuration = local.read_replica_ip_configuration
87+
database_flags = [{ name = "long_query_time", value = 1 }]
88+
disk_autoresize = null
89+
disk_size = null
90+
disk_type = "PD_HDD"
91+
user_labels = { bar = "baz" }
92+
encryption_key_name = null
9293
},
9394
{
94-
name = "1"
95-
zone = "us-central1-b"
96-
tier = "db-n1-standard-1"
97-
ip_configuration = local.read_replica_ip_configuration
98-
database_flags = [{ name = "long_query_time", value = 1 }]
99-
disk_autoresize = null
100-
disk_size = null
101-
disk_type = "PD_HDD"
102-
user_labels = { bar = "baz" }
95+
name = "1"
96+
zone = "us-central1-b"
97+
tier = "db-n1-standard-1"
98+
ip_configuration = local.read_replica_ip_configuration
99+
database_flags = [{ name = "long_query_time", value = 1 }]
100+
disk_autoresize = null
101+
disk_size = null
102+
disk_type = "PD_HDD"
103+
user_labels = { bar = "baz" }
104+
encryption_key_name = null
103105
},
104106
{
105-
name = "2"
106-
zone = "us-central1-c"
107-
tier = "db-n1-standard-1"
108-
ip_configuration = local.read_replica_ip_configuration
109-
database_flags = [{ name = "long_query_time", value = 1 }]
110-
disk_autoresize = null
111-
disk_size = null
112-
disk_type = "PD_HDD"
113-
user_labels = { bar = "baz" }
107+
name = "2"
108+
zone = "us-central1-c"
109+
tier = "db-n1-standard-1"
110+
ip_configuration = local.read_replica_ip_configuration
111+
database_flags = [{ name = "long_query_time", value = 1 }]
112+
disk_autoresize = null
113+
disk_size = null
114+
disk_type = "PD_HDD"
115+
user_labels = { bar = "baz" }
116+
encryption_key_name = null
114117
},
115118
]
116119

examples/postgresql-ha/main.tf

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -79,37 +79,40 @@ module "pg" {
7979
read_replica_name_suffix = "-test"
8080
read_replicas = [
8181
{
82-
name = "0"
83-
zone = "us-central1-a"
84-
tier = "db-custom-2-13312"
85-
ip_configuration = local.read_replica_ip_configuration
86-
database_flags = [{ name = "autovacuum", value = "off" }]
87-
disk_autoresize = null
88-
disk_size = null
89-
disk_type = "PD_HDD"
90-
user_labels = { bar = "baz" }
82+
name = "0"
83+
zone = "us-central1-a"
84+
tier = "db-custom-2-13312"
85+
ip_configuration = local.read_replica_ip_configuration
86+
database_flags = [{ name = "autovacuum", value = "off" }]
87+
disk_autoresize = null
88+
disk_size = null
89+
disk_type = "PD_HDD"
90+
user_labels = { bar = "baz" }
91+
encryption_key_name = null
9192
},
9293
{
93-
name = "1"
94-
zone = "us-central1-b"
95-
tier = "db-custom-2-13312"
96-
ip_configuration = local.read_replica_ip_configuration
97-
database_flags = [{ name = "autovacuum", value = "off" }]
98-
disk_autoresize = null
99-
disk_size = null
100-
disk_type = "PD_HDD"
101-
user_labels = { bar = "baz" }
94+
name = "1"
95+
zone = "us-central1-b"
96+
tier = "db-custom-2-13312"
97+
ip_configuration = local.read_replica_ip_configuration
98+
database_flags = [{ name = "autovacuum", value = "off" }]
99+
disk_autoresize = null
100+
disk_size = null
101+
disk_type = "PD_HDD"
102+
user_labels = { bar = "baz" }
103+
encryption_key_name = null
102104
},
103105
{
104-
name = "2"
105-
zone = "us-central1-c"
106-
tier = "db-custom-2-13312"
107-
ip_configuration = local.read_replica_ip_configuration
108-
database_flags = [{ name = "autovacuum", value = "off" }]
109-
disk_autoresize = null
110-
disk_size = null
111-
disk_type = "PD_HDD"
112-
user_labels = { bar = "baz" }
106+
name = "2"
107+
zone = "us-central1-c"
108+
tier = "db-custom-2-13312"
109+
ip_configuration = local.read_replica_ip_configuration
110+
database_flags = [{ name = "autovacuum", value = "off" }]
111+
disk_autoresize = null
112+
disk_size = null
113+
disk_type = "PD_HDD"
114+
user_labels = { bar = "baz" }
115+
encryption_key_name = null
113116
},
114117
]
115118

modules/mysql/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Note: CloudSQL provides [disk autoresize](https://cloud.google.com/sql/docs/mysq
3737
| random\_instance\_name | Sets random suffix at the end of the Cloud SQL resource name | `bool` | `false` | no |
3838
| read\_replica\_deletion\_protection | Used to block Terraform from deleting replica SQL Instances. | `bool` | `false` | no |
3939
| read\_replica\_name\_suffix | The optional suffix to add to the read instance name | `string` | `""` | no |
40-
| read\_replicas | List of read replicas to create | <pre>list(object({<br> name = string<br> tier = string<br> zone = string<br> disk_type = string<br> disk_autoresize = bool<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> })<br> }))</pre> | `[]` | no |
40+
| 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_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> })<br> encryption_key_name = string<br> }))</pre> | `[]` | no |
4141
| region | The region of the Cloud SQL resources | `string` | `"us-central1"` | no |
4242
| tier | The tier for the master instance. | `string` | `"db-n1-standard-1"` | no |
4343
| update\_timeout | The optional timout that is applied to limit long database updates. | `string` | `"10m"` | no |

modules/mysql/read_replica.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ locals {
2121
}
2222

2323
resource "google_sql_database_instance" "replicas" {
24+
provider = google-beta
2425
for_each = local.replicas
2526
project = var.project_id
2627
name = "${local.master_instance_name}-replica${var.read_replica_name_suffix}${each.value.name}"
2728
database_version = var.database_version
2829
region = join("-", slice(split("-", lookup(each.value, "zone", var.zone)), 0, 2))
2930
master_instance_name = google_sql_database_instance.default.name
3031
deletion_protection = var.read_replica_deletion_protection
32+
encryption_key_name = (join("-", slice(split("-", lookup(each.value, "zone", var.zone)), 0, 2))) == var.region ? null : each.value.encryption_key_name
3133

3234
replica_configuration {
3335
failover_target = false

modules/mysql/variables.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ variable "ip_configuration" {
165165

166166
// Read Replicas
167167
variable "read_replicas" {
168-
description = "List of read replicas to create"
168+
description = "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"
169169
type = list(object({
170170
name = string
171171
tier = string
@@ -184,6 +184,7 @@ variable "read_replicas" {
184184
private_network = string
185185
require_ssl = bool
186186
})
187+
encryption_key_name = string
187188
}))
188189
default = []
189190
}

modules/postgresql/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Note: CloudSQL provides [disk autoresize](https://cloud.google.com/sql/docs/mysq
3939
| random\_instance\_name | Sets random suffix at the end of the Cloud SQL resource name | `bool` | `false` | no |
4040
| read\_replica\_deletion\_protection | Used to block Terraform from deleting replica SQL Instances. | `bool` | `false` | no |
4141
| read\_replica\_name\_suffix | The optional suffix to add to the read instance name | `string` | `""` | no |
42-
| read\_replicas | List of read replicas to create | <pre>list(object({<br> name = string<br> tier = string<br> zone = string<br> disk_type = string<br> disk_autoresize = bool<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> })<br> }))</pre> | `[]` | no |
42+
| 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_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> })<br> encryption_key_name = string<br> }))</pre> | `[]` | no |
4343
| region | The region of the Cloud SQL resources | `string` | `"us-central1"` | no |
4444
| tier | The tier for the master instance. | `string` | `"db-f1-micro"` | no |
4545
| update\_timeout | The optional timout that is applied to limit long database updates. | `string` | `"15m"` | no |

modules/postgresql/read_replica.tf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ locals {
2121
}
2222

2323
resource "google_sql_database_instance" "replicas" {
24+
provider = google-beta
2425
for_each = local.replicas
2526
project = var.project_id
2627
name = "${local.master_instance_name}-replica${var.read_replica_name_suffix}${each.value.name}"
2728
database_version = var.database_version
2829
region = join("-", slice(split("-", lookup(each.value, "zone", var.zone)), 0, 2))
2930
master_instance_name = google_sql_database_instance.default.name
3031
deletion_protection = var.read_replica_deletion_protection
32+
encryption_key_name = (join("-", slice(split("-", lookup(each.value, "zone", var.zone)), 0, 2))) == var.region ? null : each.value.encryption_key_name
3133

3234
replica_configuration {
3335
failover_target = false

modules/postgresql/variables.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ variable "ip_configuration" {
172172

173173
// Read Replicas
174174
variable "read_replicas" {
175-
description = "List of read replicas to create"
175+
description = "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"
176176
type = list(object({
177177
name = string
178178
tier = string
@@ -191,6 +191,7 @@ variable "read_replicas" {
191191
private_network = string
192192
require_ssl = bool
193193
})
194+
encryption_key_name = string
194195
}))
195196
default = []
196197
}

modules/safer_mysql/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ mysql -S $HOME/mysql_sockets/myproject:region:instance -u user -p
192192
| random\_instance\_name | Sets random suffix at the end of the Cloud SQL resource name | `bool` | `false` | no |
193193
| read\_replica\_deletion\_protection | Used to block Terraform from deleting replica SQL Instances. | `bool` | `false` | no |
194194
| read\_replica\_name\_suffix | The optional suffix to add to the read instance name | `string` | `""` | no |
195-
| read\_replicas | List of read replicas to create | <pre>list(object({<br> name = string<br> tier = string<br> zone = string<br> disk_type = string<br> disk_autoresize = bool<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> })<br> }))</pre> | `[]` | no |
195+
| 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_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> })<br> encryption_key_name = string<br> }))</pre> | `[]` | no |
196196
| region | The region of the Cloud SQL resources | `string` | n/a | yes |
197197
| tier | The tier for the master instance. | `string` | `"db-n1-standard-1"` | no |
198198
| update\_timeout | The optional timout that is applied to limit long database updates. | `string` | `"15m"` | no |

modules/safer_mysql/variables.tf

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ variable "read_replica_name_suffix" {
165165
}
166166

167167
variable "read_replicas" {
168-
description = "List of read replicas to create"
168+
description = "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"
169169
type = list(object({
170170
name = string
171171
tier = string
@@ -184,6 +184,7 @@ variable "read_replicas" {
184184
private_network = string
185185
require_ssl = bool
186186
})
187+
encryption_key_name = string
187188
}))
188189
default = []
189190
}

0 commit comments

Comments
 (0)