diff --git a/cloud_sql/mysql_instance_switchover/dr_replica_create/main.tf b/cloud_sql/mysql_instance_switchover/dr_replica_create/main.tf new file mode 100644 index 000000000..66bbd93fe --- /dev/null +++ b/cloud_sql/mysql_instance_switchover/dr_replica_create/main.tf @@ -0,0 +1,63 @@ +/** + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +# [START cloud_sql_mysql_instance_switchover_dr_replica_create] +resource "google_sql_database_instance" "original-primary" { + name = "mysql-original-primary-instance" + region = "us-east1" + # Specify a database version that supports Cloud SQL Enterprise Plus edition. + database_version = "MYSQL_8_0" + instance_type = "CLOUD_SQL_INSTANCE" + + settings { + # Specify a tier that supports Cloud SQL Enterprise Plus edition. + tier = "db-perf-optimized-N-2" + edition = "ENTERPRISE_PLUS" + backup_configuration { + # You must enable automated backups and binary logging. + enabled = true + binary_log_enabled = true + } + } + # Set `deletion_protection` to true to ensure that one can't accidentally + # delete this instance by use of Terraform whereas + # `deletion_protection_enabled` flag protects this instance at the Google Cloud level. + deletion_protection = false +} + +resource "google_sql_database_instance" "dr-replica" { + name = "mysql-dr-replica-instance" + # DR replica must be in a different region than the region of the primary instance. + region = "us-west2" + # DR replica must be the same database version as the primary instance. + database_version = "MYSQL_8_0" + instance_type = "READ_REPLICA_INSTANCE" + # Specify the primary instance as the master instance. + master_instance_name = google_sql_database_instance.original-primary.name + + settings { + # DR replica must be in the same tier as your primary instance and support Enterprise Plus edition. + tier = "db-perf-optimized-N-2" + edition = "ENTERPRISE_PLUS" + } + + # Set `deletion_protection` to true to ensure that one can't accidentally + # delete this instance by use of Terraform whereas + # `deletion_protection_enabled` flag protects this instance at the Google Cloud level. + deletion_protection = false +} +# [END cloud_sql_mysql_instance_switchover_dr_replica_create] diff --git a/cloud_sql/mysql_instance_switchover/dr_replica_create/test.yaml b/cloud_sql/mysql_instance_switchover/dr_replica_create/test.yaml new file mode 100644 index 000000000..21d13246d --- /dev/null +++ b/cloud_sql/mysql_instance_switchover/dr_replica_create/test.yaml @@ -0,0 +1,20 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +apiVersion: blueprints.cloud.google.com/v1alpha1 +kind: BlueprintTest +metadata: + name: mysql_instance_switchover_dr_replica_create +spec: + skip: true diff --git a/cloud_sql/mysql_instance_switchover/dr_replica_designate/main.tf b/cloud_sql/mysql_instance_switchover/dr_replica_designate/main.tf new file mode 100644 index 000000000..0de8e4645 --- /dev/null +++ b/cloud_sql/mysql_instance_switchover/dr_replica_designate/main.tf @@ -0,0 +1,74 @@ +/** + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +# [START cloud_sql_mysql_instance_switchover_dr_replica_designate] +data "google_project" "default" { +} + +resource "google_sql_database_instance" "original-primary" { + name = "mysql-original-primary-instance" + region = "us-east1" + # Specify a database version that supports Cloud SQL Enterprise Plus edition. + database_version = "MYSQL_8_0" + instance_type = "CLOUD_SQL_INSTANCE" + + replication_cluster { + # Designate the DR replica. + # The format for setting the DR replica is `project-id:dr-replica-name`. + failover_dr_replica_name = "${data.google_project.default.project_id}:mysql-dr-replica-instance" + } + + settings { + # Specify a tier that supports Cloud SQL Enterprise Plus edition. + tier = "db-perf-optimized-N-2" + edition = "ENTERPRISE_PLUS" + backup_configuration { + # You must enable automated backups and binary logging. + enabled = true + binary_log_enabled = true + } + } + # Set `deletion_protection` to true to ensure that one can't accidentally + # delete this instance by use of Terraform whereas + # `deletion_protection_enabled` flag protects this instance at the Google Cloud level. + deletion_protection = false + # Optional. Add more settings. +} + +resource "google_sql_database_instance" "dr-replica" { + name = "mysql-dr-replica-instance" + # DR replica must be in a different region than the region of the primary instance. + region = "us-west2" + # DR replica must be the same database version as the primary instance. + database_version = "MYSQL_8_0" + instance_type = "READ_REPLICA_INSTANCE" + # Specify the primary instance as the master instance. + master_instance_name = google_sql_database_instance.original-primary.name + + settings { + # DR replica must be in the same tier as your primary instance and support Enterprise Plus edition. + tier = "db-perf-optimized-N-2" + edition = "ENTERPRISE_PLUS" + } + + # Set `deletion_protection` to true to ensure that one can't accidentally + # delete this instance by use of Terraform whereas + # `deletion_protection_enabled` flag protects this instance at the Google Cloud level. + deletion_protection = false + # Optional. Add more settings. +} + +# [END cloud_sql_mysql_instance_switchover_dr_replica_designate] diff --git a/cloud_sql/mysql_instance_switchover/dr_replica_designate/test.yaml b/cloud_sql/mysql_instance_switchover/dr_replica_designate/test.yaml new file mode 100644 index 000000000..9c545ea13 --- /dev/null +++ b/cloud_sql/mysql_instance_switchover/dr_replica_designate/test.yaml @@ -0,0 +1,20 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +apiVersion: blueprints.cloud.google.com/v1alpha1 +kind: BlueprintTest +metadata: + name: mysql_instance_switchover_dr_replica_designate +spec: + skip: true diff --git a/cloud_sql/mysql_instance_switchover/switchover_begin/main.tf b/cloud_sql/mysql_instance_switchover/switchover_begin/main.tf new file mode 100644 index 000000000..b741a8154 --- /dev/null +++ b/cloud_sql/mysql_instance_switchover/switchover_begin/main.tf @@ -0,0 +1,82 @@ +/** + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +# [START cloud_sql_mysql_instance_switchover_begin] +# +# This sample provides the first part of the switchover operation and turns the DR replica +# into the new primary instance. +data "google_project" "default" { +} + +resource "google_sql_database_instance" "original-primary" { + name = "mysql-original-primary-instance" + region = "us-east1" + database_version = "MYSQL_8_0" + instance_type = "CLOUD_SQL_INSTANCE" + + replication_cluster { + failover_dr_replica_name = "${data.google_project.default.project_id}:mysql-dr-replica-instance" + } + + settings { + tier = "db-perf-optimized-N-2" + edition = "ENTERPRISE_PLUS" + backup_configuration { + enabled = true + binary_log_enabled = true + } + } + # Set `deletion_protection` to true to ensure that one can't accidentally + # delete this instance by use of Terraform whereas + # `deletion_protection_enabled` flag protects this instance at the Google Cloud level. + deletion_protection = false + # Optional. Add more settings. +} + +resource "google_sql_database_instance" "dr-replica" { + name = "mysql-dr-replica-instance" + region = "us-west2" + database_version = "MYSQL_8_0" + # Change the instance type from "READ_REPLICA_INSTANCE" to "CLOUD_SQL_INSTANCE". + instance_type = "CLOUD_SQL_INSTANCE" + # Remove or comment out the master_instance_name from the DR replica. + # master_instance_name = google_sql_database_instance.original-primary.name + # Add the original primary instance to a list of replicas for the new primary. + replica_names = [google_sql_database_instance.original-primary.name] + + # Designate the original primary instance as the DR replica of the new primary instance. + # The format for setting the DR replica is `project-id:dr-replica-name`. + replication_cluster { + failover_dr_replica_name = "${data.google_project.default.project_id}:${google_sql_database_instance.original-primary.name}" + } + + settings { + tier = "db-perf-optimized-N-2" + edition = "ENTERPRISE_PLUS" + backup_configuration { + # Add a backup configuration section to enable automated backups and binary logging for the new primary instance. + enabled = true + binary_log_enabled = true + } + } + # Set `deletion_protection` to true to ensure that one can't accidentally + # delete this instance by use of Terraform whereas + # `deletion_protection_enabled` flag protects this instance at the Google Cloud level. + deletion_protection = false + # Optional. Add more settings. +} + +# [END cloud_sql_mysql_instance_switchover_begin] diff --git a/cloud_sql/mysql_instance_switchover/switchover_begin/test.yaml b/cloud_sql/mysql_instance_switchover/switchover_begin/test.yaml new file mode 100644 index 000000000..09b6b9bcb --- /dev/null +++ b/cloud_sql/mysql_instance_switchover/switchover_begin/test.yaml @@ -0,0 +1,20 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +apiVersion: blueprints.cloud.google.com/v1alpha1 +kind: BlueprintTest +metadata: + name: mysql_instance_switchover_switchover_begin +spec: + skip: true diff --git a/cloud_sql/mysql_instance_switchover/switchover_finish/main.tf b/cloud_sql/mysql_instance_switchover/switchover_finish/main.tf new file mode 100644 index 000000000..81da8fbbd --- /dev/null +++ b/cloud_sql/mysql_instance_switchover/switchover_finish/main.tf @@ -0,0 +1,85 @@ +/** + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +# [START cloud_sql_mysql_instance_switchover_finish] +# +# This sample provides the second part of the switchover operation and makes the original primary instance +# a replica of the new primary instance. After you run `terraform apply` for this sample, you'll see +# the following message: +# +# "No changes. Your infrastructure matches the configuration. +# +# Terraform has compared your real infrastructure against your configuration and found no differences, +# so no changes are needed. +# +# Apply complete! Resources: 0 added, 0 changed, 0 destroyed." +data "google_project" "default" { +} + +resource "google_sql_database_instance" "original-primary" { + name = "mysql-original-primary-instance" + region = "us-east1" + database_version = "MYSQL_8_0" + # Change instance type for the original primary from "CLOUD_SQL_INSTANCE" to "READ_REPLICA_INSTANCE". + instance_type = "READ_REPLICA_INSTANCE" + # Set master_instance_name to the the new primary instance, the old DR replica. + master_instance_name = "mysql-dr-replica-instance" + # replica_names = [] # If you previously defined a replica_names field in your template, then delete the DR replica + # (new primary) from the list of replicas. Don't delete the entire replica_names field. + # Instead set the field to an empty string. For example, replica_names = [""]. + + replication_cluster { + # This instance no longer requires a designated DR replica since it's a replica. + # Remove the DR replica designation by setting the field to an empty string. + failover_dr_replica_name = "" + } + + settings { + tier = "db-perf-optimized-N-2" + edition = "ENTERPRISE_PLUS" + backup_configuration { + # Disable automated backups because this instance is now a replica. + enabled = false + binary_log_enabled = true + } + } + deletion_protection = false +} + +resource "google_sql_database_instance" "dr-replica" { + name = "mysql-dr-replica-instance" + region = "us-west2" + database_version = "MYSQL_8_0" + instance_type = "CLOUD_SQL_INSTANCE" + replica_names = [google_sql_database_instance.original-primary.name] + + + replication_cluster { + failover_dr_replica_name = "${data.google_project.default.project_id}:${google_sql_database_instance.original-primary.name}" + } + + settings { + tier = "db-perf-optimized-N-2" + edition = "ENTERPRISE_PLUS" + backup_configuration { + enabled = true + binary_log_enabled = true + } + } + deletion_protection = false +} + +# [END cloud_sql_mysql_instance_switchover_finish] diff --git a/cloud_sql/mysql_instance_switchover/switchover_finish/test.yaml b/cloud_sql/mysql_instance_switchover/switchover_finish/test.yaml new file mode 100644 index 000000000..8cf300e23 --- /dev/null +++ b/cloud_sql/mysql_instance_switchover/switchover_finish/test.yaml @@ -0,0 +1,20 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +apiVersion: blueprints.cloud.google.com/v1alpha1 +kind: BlueprintTest +metadata: + name: mysql_instance_switchover_switchover_finish +spec: + skip: true diff --git a/cloud_sql/postgres_instance_switchover/dr_replica_create/main.tf b/cloud_sql/postgres_instance_switchover/dr_replica_create/main.tf new file mode 100644 index 000000000..9902cb232 --- /dev/null +++ b/cloud_sql/postgres_instance_switchover/dr_replica_create/main.tf @@ -0,0 +1,63 @@ +/** + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +# [START cloud_sql_postgres_instance_switchover_dr_replica_create] +resource "google_sql_database_instance" "original-primary" { + name = "postgres-original-primary-instance" + region = "us-east1" + # Specify a database version that supports Cloud SQL Enterprise Plus edition. + database_version = "POSTGRES_12" + instance_type = "CLOUD_SQL_INSTANCE" + + settings { + # Specify a tier that supports Cloud SQL Enterprise Plus edition. + tier = "db-perf-optimized-N-2" + edition = "ENTERPRISE_PLUS" + backup_configuration { + # You must enable automated backups and point-in-time-recovery (PITR). + enabled = true + point_in_time_recovery_enabled = true + } + } + # Set `deletion_protection` to true to ensure that one can't accidentally + # delete this instance by use of Terraform whereas + # `deletion_protection_enabled` flag protects this instance at the Google Cloud level. + deletion_protection = false +} + +resource "google_sql_database_instance" "dr-replica" { + name = "postgres-dr-replica-instance" + # DR replica must be in a different region than the region of the primary instance. + region = "us-west2" + # DR replica must be the same database version as the primary instance. + database_version = "POSTGRES_12" + instance_type = "READ_REPLICA_INSTANCE" + # Specify the primary instance as the master instance. + master_instance_name = google_sql_database_instance.original-primary.name + + settings { + # DR replica must be in the same tier as your primary instance and support Enterprise Plus edition. + tier = "db-perf-optimized-N-2" + edition = "ENTERPRISE_PLUS" + } + + # Set `deletion_protection` to true to ensure that one can't accidentally + # delete this instance by use of Terraform whereas + # `deletion_protection_enabled` flag protects this instance at the Google Cloud level. + deletion_protection = false +} +# [END cloud_sql_postgres_instance_switchover_dr_replica_create] diff --git a/cloud_sql/postgres_instance_switchover/dr_replica_create/test.yaml b/cloud_sql/postgres_instance_switchover/dr_replica_create/test.yaml new file mode 100644 index 000000000..36cfc45f0 --- /dev/null +++ b/cloud_sql/postgres_instance_switchover/dr_replica_create/test.yaml @@ -0,0 +1,20 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +apiVersion: blueprints.cloud.google.com/v1alpha1 +kind: BlueprintTest +metadata: + name: postgres_instance_switchover_dr_replica_create +spec: + skip: true diff --git a/cloud_sql/postgres_instance_switchover/dr_replica_designate/main.tf b/cloud_sql/postgres_instance_switchover/dr_replica_designate/main.tf new file mode 100644 index 000000000..d8a48a4d7 --- /dev/null +++ b/cloud_sql/postgres_instance_switchover/dr_replica_designate/main.tf @@ -0,0 +1,67 @@ +/** + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +# [START cloud_sql_postgres_instance_switchover_dr_replica_designate] +data "google_project" "default" { +} + +resource "google_sql_database_instance" "original-primary" { + name = "postgres-original-primary-instance" + region = "us-east1" + database_version = "POSTGRES_12" + instance_type = "CLOUD_SQL_INSTANCE" + + replication_cluster { + # Designate the DR replica. + # The format for setting the DR replica is `project-id:dr-replica-name`. + failover_dr_replica_name = "${data.google_project.default.project_id}:postgres-dr-replica-instance" + } + + settings { + tier = "db-perf-optimized-N-2" + edition = "ENTERPRISE_PLUS" + backup_configuration { + enabled = true + point_in_time_recovery_enabled = true + } + } + # Set `deletion_protection` to true to ensure that one can't accidentally + # delete this instance by use of Terraform whereas + # `deletion_protection_enabled` flag protects this instance at the Google Cloud level. + deletion_protection = false + # Optional. Add more settings. +} + +resource "google_sql_database_instance" "dr-replica" { + name = "postgres-dr-replica-instance" + region = "us-west2" + database_version = "POSTGRES_12" + instance_type = "READ_REPLICA_INSTANCE" + master_instance_name = google_sql_database_instance.original-primary.name + + settings { + tier = "db-perf-optimized-N-2" + edition = "ENTERPRISE_PLUS" + } + + # Set `deletion_protection` to true to ensure that one can't accidentally + # delete this instance by use of Terraform whereas + # `deletion_protection_enabled` flag protects this instance at the Google Cloud level. + deletion_protection = false + # Optional. Add more settings. +} + +# [END cloud_sql_postgres_instance_switchover_dr_replica_designate] diff --git a/cloud_sql/postgres_instance_switchover/dr_replica_designate/test.yaml b/cloud_sql/postgres_instance_switchover/dr_replica_designate/test.yaml new file mode 100644 index 000000000..16368db20 --- /dev/null +++ b/cloud_sql/postgres_instance_switchover/dr_replica_designate/test.yaml @@ -0,0 +1,20 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +apiVersion: blueprints.cloud.google.com/v1alpha1 +kind: BlueprintTest +metadata: + name: postgres_instance_switchover_dr_replica_designate +spec: + skip: true diff --git a/cloud_sql/postgres_instance_switchover/switchover_begin/main.tf b/cloud_sql/postgres_instance_switchover/switchover_begin/main.tf new file mode 100644 index 000000000..161e87adf --- /dev/null +++ b/cloud_sql/postgres_instance_switchover/switchover_begin/main.tf @@ -0,0 +1,82 @@ +/** + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +# [START cloud_sql_postgres_instance_switchover_begin] +# +# This sample provides the first part of the switchover operation and turns the DR replica +# into the new primary instance. +data "google_project" "default" { +} + +resource "google_sql_database_instance" "original-primary" { + name = "postgres-original-primary-instance" + region = "us-east1" + database_version = "POSTGRES_12" + instance_type = "CLOUD_SQL_INSTANCE" + + replication_cluster { + failover_dr_replica_name = "${data.google_project.default.project_id}:postgres-dr-replica-instance" + } + + settings { + tier = "db-perf-optimized-N-2" + edition = "ENTERPRISE_PLUS" + backup_configuration { + enabled = true + point_in_time_recovery_enabled = true + } + } + # Set `deletion_protection` to true to ensure that one can't accidentally + # delete this instance by use of Terraform whereas + # `deletion_protection_enabled` flag protects this instance at the Google Cloud level. + deletion_protection = false + # Optional. Add more settings. +} + +resource "google_sql_database_instance" "dr-replica" { + name = "postgres-dr-replica-instance" + region = "us-west2" + database_version = "POSTGRES_12" + # Change the instance type from "READ_REPLICA_INSTANCE" to "CLOUD_SQL_INSTANCE". + instance_type = "CLOUD_SQL_INSTANCE" + # Remove or comment out the master_instance_name from the DR replica. + # master_instance_name = google_sql_database_instance.original-primary.name + # Add the original primary instance to a list of replicas for the new primary. + replica_names = [google_sql_database_instance.original-primary.name] + + # Designate the original primary instance as the DR replica of the new primary instance. + # The format for setting the DR replica is `project-id:dr-replica-name`. + replication_cluster { + failover_dr_replica_name = "${data.google_project.default.project_id}:${google_sql_database_instance.original-primary.name}" + } + + settings { + tier = "db-perf-optimized-N-2" + edition = "ENTERPRISE_PLUS" + backup_configuration { + # Add a backup configuration section to enable automated backups and point-in-time-recovery (PITR) for the new primary instance. + enabled = true + point_in_time_recovery_enabled = true + } + } + # Set `deletion_protection` to true to ensure that one can't accidentally + # delete this instance by use of Terraform whereas + # `deletion_protection_enabled` flag protects this instance at the Google Cloud level. + deletion_protection = false + # Optional. Add more settings. +} + +# [END cloud_sql_postgres_instance_switchover_begin] diff --git a/cloud_sql/postgres_instance_switchover/switchover_begin/test.yaml b/cloud_sql/postgres_instance_switchover/switchover_begin/test.yaml new file mode 100644 index 000000000..418641c18 --- /dev/null +++ b/cloud_sql/postgres_instance_switchover/switchover_begin/test.yaml @@ -0,0 +1,20 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +apiVersion: blueprints.cloud.google.com/v1alpha1 +kind: BlueprintTest +metadata: + name: postgres_instance_switchover_switchover_begin +spec: + skip: true diff --git a/cloud_sql/postgres_instance_switchover/switchover_finish/main.tf b/cloud_sql/postgres_instance_switchover/switchover_finish/main.tf new file mode 100644 index 000000000..6c6e89cd1 --- /dev/null +++ b/cloud_sql/postgres_instance_switchover/switchover_finish/main.tf @@ -0,0 +1,92 @@ +/** + * Copyright 2025 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +# [START cloud_sql_postgres_instance_switchover_finish] +# This sample provides the second part of the switchover operation and makes the original primary instance +# a replica of the new primary instance. After you run `terraform apply` for this sample, you'll see +# the following message: +# +# "No changes. Your infrastructure matches the configuration. +# +# Terraform has compared your real infrastructure against your configuration and found no differences, +# so no changes are needed. +# +# Apply complete! Resources: 0 added, 0 changed, 0 destroyed." +data "google_project" "default" { +} + +resource "google_sql_database_instance" "original-primary" { + name = "postgres-original-primary-instance" + region = "us-east1" + database_version = "POSTGRES_12" + # Change instance type for the original primary from "CLOUD_SQL_INSTANCE" to "READ_REPLICA_INSTANCE". + instance_type = "READ_REPLICA_INSTANCE" + # Set master_instance_name to the the new primary instance, the old DR replica. + master_instance_name = "postgres-dr-replica-instance" + # replica_names = [] # If you previously defined a replica_names field in your template, then delete the DR replica + # (new primary) from the list of replicas. Don't delete the entire replica_names field. + # Instead set the field to an empty string. For example, replica_names = [""]. + + replication_cluster { + # This instance no longer requires a designated DR replica since it's a replica. + # Remove the DR replica designation by setting the field to an empty string. + failover_dr_replica_name = "" + } + + settings { + tier = "db-perf-optimized-N-2" + edition = "ENTERPRISE_PLUS" + backup_configuration { + # Disable automated backups and PITR because this instance is now a replica. + enabled = false + point_in_time_recovery_enabled = false + } + } + # Set `deletion_protection` to true to ensure that one can't accidentally + # delete this instance by use of Terraform whereas + # `deletion_protection_enabled` flag protects this instance at the Google Cloud level. + deletion_protection = false + # Optional. Add more settings. +} + +resource "google_sql_database_instance" "dr-replica" { + name = "postgres-dr-replica-instance" + region = "us-west2" + database_version = "POSTGRES_12" + instance_type = "CLOUD_SQL_INSTANCE" + replica_names = [google_sql_database_instance.original-primary.name] + + + replication_cluster { + failover_dr_replica_name = "${data.google_project.default.project_id}:${google_sql_database_instance.original-primary.name}" + } + + settings { + tier = "db-perf-optimized-N-2" + edition = "ENTERPRISE_PLUS" + backup_configuration { + enabled = true + point_in_time_recovery_enabled = true + } + } + # Set `deletion_protection` to true to ensure that one can't accidentally + # delete this instance by use of Terraform whereas + # `deletion_protection_enabled` flag protects this instance at the Google Cloud level. + deletion_protection = false + # Optional. Add more settings. +} + +# [END cloud_sql_postgres_instance_switchover_finish] diff --git a/cloud_sql/postgres_instance_switchover/switchover_finish/test.yaml b/cloud_sql/postgres_instance_switchover/switchover_finish/test.yaml new file mode 100644 index 000000000..e062be1da --- /dev/null +++ b/cloud_sql/postgres_instance_switchover/switchover_finish/test.yaml @@ -0,0 +1,20 @@ +# Copyright 2025 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +apiVersion: blueprints.cloud.google.com/v1alpha1 +kind: BlueprintTest +metadata: + name: postgres_instance_switchover_switchover_finish +spec: + skip: true