Skip to content

Commit 0bc022c

Browse files
sevietglasnt
authored andcommitted
feat(cloud_sql): Add samples for Advanced DR switchover for MySQL and PostgreSQL (terraform-google-modules#814)
* feat: Add MySQL & PostgreSQL samples for Advanced DR switchover * Fix region tag * Update test.yaml Fix tag name * Update test.yaml update tag name * Update test.yaml update tag name * lint: whitespace * lint: whitespace * Remove unnecessary comments * Remove unnecessary comments * Remove unnecessary comments * linting * lint --------- Co-authored-by: Katie McLaughlin <[email protected]>
1 parent fd9e7b7 commit 0bc022c

File tree

16 files changed

+768
-0
lines changed

16 files changed

+768
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
18+
# [START cloud_sql_mysql_instance_switchover_dr_replica_create]
19+
resource "google_sql_database_instance" "original-primary" {
20+
name = "mysql-original-primary-instance"
21+
region = "us-east1"
22+
# Specify a database version that supports Cloud SQL Enterprise Plus edition.
23+
database_version = "MYSQL_8_0"
24+
instance_type = "CLOUD_SQL_INSTANCE"
25+
26+
settings {
27+
# Specify a tier that supports Cloud SQL Enterprise Plus edition.
28+
tier = "db-perf-optimized-N-2"
29+
edition = "ENTERPRISE_PLUS"
30+
backup_configuration {
31+
# You must enable automated backups and binary logging.
32+
enabled = true
33+
binary_log_enabled = true
34+
}
35+
}
36+
# Set `deletion_protection` to true to ensure that one can't accidentally
37+
# delete this instance by use of Terraform whereas
38+
# `deletion_protection_enabled` flag protects this instance at the Google Cloud level.
39+
deletion_protection = false
40+
}
41+
42+
resource "google_sql_database_instance" "dr-replica" {
43+
name = "mysql-dr-replica-instance"
44+
# DR replica must be in a different region than the region of the primary instance.
45+
region = "us-west2"
46+
# DR replica must be the same database version as the primary instance.
47+
database_version = "MYSQL_8_0"
48+
instance_type = "READ_REPLICA_INSTANCE"
49+
# Specify the primary instance as the master instance.
50+
master_instance_name = google_sql_database_instance.original-primary.name
51+
52+
settings {
53+
# DR replica must be in the same tier as your primary instance and support Enterprise Plus edition.
54+
tier = "db-perf-optimized-N-2"
55+
edition = "ENTERPRISE_PLUS"
56+
}
57+
58+
# Set `deletion_protection` to true to ensure that one can't accidentally
59+
# delete this instance by use of Terraform whereas
60+
# `deletion_protection_enabled` flag protects this instance at the Google Cloud level.
61+
deletion_protection = false
62+
}
63+
# [END cloud_sql_mysql_instance_switchover_dr_replica_create]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
apiVersion: blueprints.cloud.google.com/v1alpha1
16+
kind: BlueprintTest
17+
metadata:
18+
name: mysql_instance_switchover_dr_replica_create
19+
spec:
20+
skip: true
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
# [START cloud_sql_mysql_instance_switchover_dr_replica_designate]
18+
data "google_project" "default" {
19+
}
20+
21+
resource "google_sql_database_instance" "original-primary" {
22+
name = "mysql-original-primary-instance"
23+
region = "us-east1"
24+
# Specify a database version that supports Cloud SQL Enterprise Plus edition.
25+
database_version = "MYSQL_8_0"
26+
instance_type = "CLOUD_SQL_INSTANCE"
27+
28+
replication_cluster {
29+
# Designate the DR replica.
30+
# The format for setting the DR replica is `project-id:dr-replica-name`.
31+
failover_dr_replica_name = "${data.google_project.default.project_id}:mysql-dr-replica-instance"
32+
}
33+
34+
settings {
35+
# Specify a tier that supports Cloud SQL Enterprise Plus edition.
36+
tier = "db-perf-optimized-N-2"
37+
edition = "ENTERPRISE_PLUS"
38+
backup_configuration {
39+
# You must enable automated backups and binary logging.
40+
enabled = true
41+
binary_log_enabled = true
42+
}
43+
}
44+
# Set `deletion_protection` to true to ensure that one can't accidentally
45+
# delete this instance by use of Terraform whereas
46+
# `deletion_protection_enabled` flag protects this instance at the Google Cloud level.
47+
deletion_protection = false
48+
# Optional. Add more settings.
49+
}
50+
51+
resource "google_sql_database_instance" "dr-replica" {
52+
name = "mysql-dr-replica-instance"
53+
# DR replica must be in a different region than the region of the primary instance.
54+
region = "us-west2"
55+
# DR replica must be the same database version as the primary instance.
56+
database_version = "MYSQL_8_0"
57+
instance_type = "READ_REPLICA_INSTANCE"
58+
# Specify the primary instance as the master instance.
59+
master_instance_name = google_sql_database_instance.original-primary.name
60+
61+
settings {
62+
# DR replica must be in the same tier as your primary instance and support Enterprise Plus edition.
63+
tier = "db-perf-optimized-N-2"
64+
edition = "ENTERPRISE_PLUS"
65+
}
66+
67+
# Set `deletion_protection` to true to ensure that one can't accidentally
68+
# delete this instance by use of Terraform whereas
69+
# `deletion_protection_enabled` flag protects this instance at the Google Cloud level.
70+
deletion_protection = false
71+
# Optional. Add more settings.
72+
}
73+
74+
# [END cloud_sql_mysql_instance_switchover_dr_replica_designate]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
apiVersion: blueprints.cloud.google.com/v1alpha1
16+
kind: BlueprintTest
17+
metadata:
18+
name: mysql_instance_switchover_dr_replica_designate
19+
spec:
20+
skip: true
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
# [START cloud_sql_mysql_instance_switchover_begin]
18+
#
19+
# This sample provides the first part of the switchover operation and turns the DR replica
20+
# into the new primary instance.
21+
data "google_project" "default" {
22+
}
23+
24+
resource "google_sql_database_instance" "original-primary" {
25+
name = "mysql-original-primary-instance"
26+
region = "us-east1"
27+
database_version = "MYSQL_8_0"
28+
instance_type = "CLOUD_SQL_INSTANCE"
29+
30+
replication_cluster {
31+
failover_dr_replica_name = "${data.google_project.default.project_id}:mysql-dr-replica-instance"
32+
}
33+
34+
settings {
35+
tier = "db-perf-optimized-N-2"
36+
edition = "ENTERPRISE_PLUS"
37+
backup_configuration {
38+
enabled = true
39+
binary_log_enabled = true
40+
}
41+
}
42+
# Set `deletion_protection` to true to ensure that one can't accidentally
43+
# delete this instance by use of Terraform whereas
44+
# `deletion_protection_enabled` flag protects this instance at the Google Cloud level.
45+
deletion_protection = false
46+
# Optional. Add more settings.
47+
}
48+
49+
resource "google_sql_database_instance" "dr-replica" {
50+
name = "mysql-dr-replica-instance"
51+
region = "us-west2"
52+
database_version = "MYSQL_8_0"
53+
# Change the instance type from "READ_REPLICA_INSTANCE" to "CLOUD_SQL_INSTANCE".
54+
instance_type = "CLOUD_SQL_INSTANCE"
55+
# Remove or comment out the master_instance_name from the DR replica.
56+
# master_instance_name = google_sql_database_instance.original-primary.name
57+
# Add the original primary instance to a list of replicas for the new primary.
58+
replica_names = [google_sql_database_instance.original-primary.name]
59+
60+
# Designate the original primary instance as the DR replica of the new primary instance.
61+
# The format for setting the DR replica is `project-id:dr-replica-name`.
62+
replication_cluster {
63+
failover_dr_replica_name = "${data.google_project.default.project_id}:${google_sql_database_instance.original-primary.name}"
64+
}
65+
66+
settings {
67+
tier = "db-perf-optimized-N-2"
68+
edition = "ENTERPRISE_PLUS"
69+
backup_configuration {
70+
# Add a backup configuration section to enable automated backups and binary logging for the new primary instance.
71+
enabled = true
72+
binary_log_enabled = true
73+
}
74+
}
75+
# Set `deletion_protection` to true to ensure that one can't accidentally
76+
# delete this instance by use of Terraform whereas
77+
# `deletion_protection_enabled` flag protects this instance at the Google Cloud level.
78+
deletion_protection = false
79+
# Optional. Add more settings.
80+
}
81+
82+
# [END cloud_sql_mysql_instance_switchover_begin]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
apiVersion: blueprints.cloud.google.com/v1alpha1
16+
kind: BlueprintTest
17+
metadata:
18+
name: mysql_instance_switchover_switchover_begin
19+
spec:
20+
skip: true
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
# [START cloud_sql_mysql_instance_switchover_finish]
18+
#
19+
# This sample provides the second part of the switchover operation and makes the original primary instance
20+
# a replica of the new primary instance. After you run `terraform apply` for this sample, you'll see
21+
# the following message:
22+
#
23+
# "No changes. Your infrastructure matches the configuration.
24+
#
25+
# Terraform has compared your real infrastructure against your configuration and found no differences,
26+
# so no changes are needed.
27+
#
28+
# Apply complete! Resources: 0 added, 0 changed, 0 destroyed."
29+
data "google_project" "default" {
30+
}
31+
32+
resource "google_sql_database_instance" "original-primary" {
33+
name = "mysql-original-primary-instance"
34+
region = "us-east1"
35+
database_version = "MYSQL_8_0"
36+
# Change instance type for the original primary from "CLOUD_SQL_INSTANCE" to "READ_REPLICA_INSTANCE".
37+
instance_type = "READ_REPLICA_INSTANCE"
38+
# Set master_instance_name to the the new primary instance, the old DR replica.
39+
master_instance_name = "mysql-dr-replica-instance"
40+
# replica_names = [] # If you previously defined a replica_names field in your template, then delete the DR replica
41+
# (new primary) from the list of replicas. Don't delete the entire replica_names field.
42+
# Instead set the field to an empty string. For example, replica_names = [""].
43+
44+
replication_cluster {
45+
# This instance no longer requires a designated DR replica since it's a replica.
46+
# Remove the DR replica designation by setting the field to an empty string.
47+
failover_dr_replica_name = ""
48+
}
49+
50+
settings {
51+
tier = "db-perf-optimized-N-2"
52+
edition = "ENTERPRISE_PLUS"
53+
backup_configuration {
54+
# Disable automated backups because this instance is now a replica.
55+
enabled = false
56+
binary_log_enabled = true
57+
}
58+
}
59+
deletion_protection = false
60+
}
61+
62+
resource "google_sql_database_instance" "dr-replica" {
63+
name = "mysql-dr-replica-instance"
64+
region = "us-west2"
65+
database_version = "MYSQL_8_0"
66+
instance_type = "CLOUD_SQL_INSTANCE"
67+
replica_names = [google_sql_database_instance.original-primary.name]
68+
69+
70+
replication_cluster {
71+
failover_dr_replica_name = "${data.google_project.default.project_id}:${google_sql_database_instance.original-primary.name}"
72+
}
73+
74+
settings {
75+
tier = "db-perf-optimized-N-2"
76+
edition = "ENTERPRISE_PLUS"
77+
backup_configuration {
78+
enabled = true
79+
binary_log_enabled = true
80+
}
81+
}
82+
deletion_protection = false
83+
}
84+
85+
# [END cloud_sql_mysql_instance_switchover_finish]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
apiVersion: blueprints.cloud.google.com/v1alpha1
16+
kind: BlueprintTest
17+
metadata:
18+
name: mysql_instance_switchover_switchover_finish
19+
spec:
20+
skip: true

0 commit comments

Comments
 (0)