Skip to content

Commit 141e54a

Browse files
authored
feat(TPG>=5.12)!: Add option to create and failover a replica instance in Postgresql and MsSQL sub-module (#582)
1 parent fe974d2 commit 141e54a

File tree

29 files changed

+1550
-144
lines changed

29 files changed

+1550
-144
lines changed

README.md

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,19 @@ This module consists of the following submodules:
1010
See more details in each module's README.
1111

1212
## Compatibility
13-
This module is meant for use with Terraform 0.13+ and tested using Terraform 1.0+.
14-
If you find incompatibilities using Terraform `>=0.13`, please open an issue.
15-
16-
If you haven't [upgraded](https://www.terraform.io/upgrade-guides/0-13.html) and need a Terraform
17-
0.12.x-compatible version of this module, the last released version
18-
intended for Terraform 0.12.x is [v5.0.0](https://registry.terraform.io/modules/GoogleCloudPlatform/sql-db/google/5.0.0).
13+
This module is meant for use with Terraform 1.3+ and tested using Terraform 1.6+.
14+
If you find incompatibilities using Terraform `>=1.13`, please open an issue.
1915

2016
## Upgrading
2117

22-
The current version is 13.X. The following guides are available to assist with upgrades:
18+
The current version is 20.X. The following guides are available to assist with upgrades:
2319

2420
- [1.X -> 2.0](./docs/upgrading_to_sql_db_2.0.0.md)
2521
- [2.X -> 3.0](./docs/upgrading_to_sql_db_3.0.0.md)
2622
- [3.X -> 4.0](./docs/upgrading_to_sql_db_4.0.0.md)
2723
- [10.X -> 11.0](./docs/upgrading_to_sql_db_11.0.0.md)
2824
- [11.X -> 12.0](./docs/upgrading_to_sql_db_12.0.0.md)
25+
- [19.X -> 20.0](./docs/upgrading_to_sql_db_20.0.0.md)
2926

3027
## Root module
3128

@@ -36,11 +33,8 @@ The root module has been deprecated. Please switch to using one of the submodule
3633
### Installation Dependencies
3734

3835
- [Terraform](https://www.terraform.io/downloads.html) >= 1.3.0
39-
- [terraform-provider-google](https://github.com/terraform-providers/terraform-provider-google) plugin >= v4.45.0
40-
41-
The following dependency must be available for SQL Server module:
42-
43-
- [Terraform Provider Beta for GCP](https://github.com/terraform-providers/terraform-provider-google-beta) plugin >= v4.45.0
36+
- [terraform-provider-google](https://github.com/terraform-providers/terraform-provider-google) plugin v5.12+
37+
- [Terraform Provider Beta for GCP](https://github.com/terraform-providers/terraform-provider-google-beta) plugin v5.12+
4438

4539
### Configure a Service Account
4640

@@ -85,7 +79,7 @@ For MySQL :
8579
```
8680
module "sql-db" {
8781
source = "GoogleCloudPlatform/sql-db/google//modules/mysql"
88-
version = "8.0.0"
82+
version = "~> 20.0"
8983
}
9084
```
9185

@@ -94,7 +88,7 @@ or for PostgreSQL :
9488
```
9589
module "sql-db" {
9690
source = "GoogleCloudPlatform/sql-db/google//modules/postgresql"
97-
version = "8.0.0"
91+
version = "~> 20.0"
9892
}
9993
```
10094

@@ -103,7 +97,7 @@ or for MSSQL Server :
10397
```
10498
module "sql-db" {
10599
source = "GoogleCloudPlatform/sql-db/google//modules/mssql"
106-
version = "8.0.0"
100+
version = "~> 20.0"
107101
}
108102
```
109103

build/int.cloudbuild.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,36 @@ steps:
3939
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
4040
args: ['/bin/bash', '-c', 'cft test run all --stage init --verbose']
4141

42+
- id: apply postgresql-cross-region-failover
43+
waitFor: ["init-all", "wait for api activation"]
44+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
45+
args: ['/bin/bash', '-c', 'cft test run TestPostgreSqlCrossRegionFailover --stage apply --verbose']
46+
- id: verify postgresql-cross-region-failover
47+
waitFor:
48+
- apply postgresql-cross-region-failover
49+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
50+
args: ['/bin/bash', '-c', 'cft test run TestPostgreSqlCrossRegionFailover --stage verify --verbose']
51+
- id: teardown postgresql-cross-region-failover
52+
waitFor:
53+
- verify postgresql-cross-region-failover
54+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
55+
args: ['/bin/bash', '-c', 'cft test run TestPostgreSqlCrossRegionFailover --stage teardown --verbose']
56+
57+
- id: apply mssql-failover-replica
58+
waitFor: ["init-all", "wait for api activation"]
59+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
60+
args: ['/bin/bash', '-c', 'cft test run TestMsSqlFailoverReplica --stage apply --verbose']
61+
- id: verify mssql-failover-replica
62+
waitFor:
63+
- apply mssql-failover-replica
64+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
65+
args: ['/bin/bash', '-c', 'cft test run TestMsSqlFailoverReplica --stage verify --verbose']
66+
- id: teardown mssql-failover-replica
67+
waitFor:
68+
- verify mssql-failover-replica
69+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
70+
args: ['/bin/bash', '-c', 'cft test run TestMsSqlFailoverReplica --stage teardown --verbose']
71+
4272
- id: apply mssql-ha-local
4373
waitFor: ["init-all", "wait for api activation"]
4474
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'

docs/upgrading_to_sql_db_20.0.0.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Upgrading to SQL DB 20.0.0
2+
3+
The 20.0.0 release of SQL DB is a backward incompatible release.
4+
5+
This update requires upgrading the minimum provider version `5.12` and minimum Terraform version `1.3`
6+
7+
8+
In `mysql` and `postgresql` sub-module output `instance_server_ca_cert` and `replicas_instance_server_ca_certs` are also marked as `sensitive`
9+
10+
In `mysql` and `postgresql` sub-module default value for `zone` is changed from `"us-central1-a"` to `null`
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
# CloudSql MS SQL Server database Example with failover replication
2+
3+
This example shows how create private MS SQL Server database with cross region failover replica using the Terraform module. You can promote failover replica without losing state file sync.
4+
5+
- Set `enable_default_db` and `enable_default_user` to `null`
6+
- Dont set `additional_databases`, `user_name`, `user_password` and `additional_users`
7+
- `availability_type` in all replica should be set to `ZONAL`
8+
9+
## Run Terraform
10+
11+
```
12+
terraform init
13+
terraform plan
14+
terraform apply
15+
```
16+
17+
## Failover to Instance 2
18+
19+
Promote instance 2 as primary and change instance 1 as failover replica
20+
21+
1) remove `master_instance_name` from instance 2 and Execute `terraform apply`
22+
23+
```diff
24+
module "mssql2" {
25+
source = "terraform-google-modules/sql-db/google//modules/mssql"
26+
version = "~> 20.0"
27+
28+
- master_instance_name = module.mssql1.instance_name
29+
30+
...
31+
}
32+
```
33+
34+
2) Remove instance 1 by removing instance 1 code and Execute `terraform apply`
35+
36+
```diff
37+
- module "mssql1" {
38+
- source = "terraform-google-modules/sql-db/google//modules/mssql"
39+
- version = "~> 20.0"
40+
- region = local.region_1
41+
- name = "tf-mssql-public-1"
42+
- random_instance_name = true
43+
- project_id = var.project_id
44+
- ...
45+
- }
46+
- output "instance_name1" {
47+
- description = "The name for Cloud SQL instance"
48+
- value = module.mssql1.instance_name
49+
- }
50+
- output "mssql_connection" {
51+
- value = module.mssql1.instance_connection_name
52+
- description = "The connection name of the master instance to be used in connection strings"
53+
- }
54+
- output "public_ip_address" {
55+
- value = module.mssql1.instance_first_ip_address
56+
- description = "Public ip address"
57+
- }
58+
```
59+
60+
3) Create instance 1 as failover replica by adding instance 1 code with following additional line and Execute `terraform apply`
61+
62+
```diff
63+
module "mssql1" {
64+
source = "terraform-google-modules/sql-db/google//modules/mssql"
65+
version = "~> 20.0"
66+
67+
+ master_instance_name = module.mssql2.instance_name
68+
69+
...
70+
71+
}
72+
```
73+
74+
75+
## Cleanup
76+
77+
To remove all resources created by terraform:
78+
79+
```bash
80+
terraform destroy
81+
```
82+
83+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
84+
## Inputs
85+
86+
| Name | Description | Type | Default | Required |
87+
|------|-------------|------|---------|:--------:|
88+
| network\_name | The ID of the network in which to provision resources. | `string` | `"test-mssql-failover"` | no |
89+
| project\_id | The project to run tests against | `string` | n/a | yes |
90+
| sql\_server\_audit\_config | SQL server audit config settings. | `map(string)` | `{}` | no |
91+
92+
## Outputs
93+
94+
| Name | Description |
95+
|------|-------------|
96+
| instance\_name1 | The name for Cloud SQL instance |
97+
| instance\_name2 | The name for Cloud SQL instance 2 |
98+
| master\_instance\_name2 | n/a |
99+
| mssql\_connection | The connection name of the master instance to be used in connection strings |
100+
| project\_id | n/a |
101+
| public\_ip\_address | Public ip address |
102+
103+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
104+
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Copyright 2019 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+
locals {
19+
region_1 = "us-central1"
20+
region_2 = "us-east1"
21+
}
22+
23+
# Instance 1
24+
25+
module "mssql1" {
26+
source = "terraform-google-modules/sql-db/google//modules/mssql"
27+
version = "~> 20.0"
28+
29+
region = local.region_1
30+
31+
name = "tf-mssql-public-1"
32+
random_instance_name = true
33+
project_id = var.project_id
34+
35+
database_version = "SQLSERVER_2022_ENTERPRISE"
36+
37+
deletion_protection = false
38+
39+
tier = "db-custom-10-65536"
40+
41+
ip_configuration = {
42+
ipv4_enabled = false
43+
private_network = google_compute_network.default.self_link
44+
}
45+
46+
sql_server_audit_config = var.sql_server_audit_config
47+
enable_default_db = false
48+
enable_default_user = false
49+
50+
depends_on = [
51+
google_service_networking_connection.vpc_connection,
52+
]
53+
}
54+
55+
# instance 2
56+
57+
module "mssql2" {
58+
source = "terraform-google-modules/sql-db/google//modules/mssql"
59+
version = "~> 20.0"
60+
61+
master_instance_name = module.mssql1.instance_name
62+
63+
region = local.region_2
64+
65+
name = "tf-mssql-public-2"
66+
random_instance_name = true
67+
project_id = var.project_id
68+
69+
database_version = "SQLSERVER_2022_ENTERPRISE"
70+
71+
deletion_protection = false
72+
73+
tier = "db-custom-10-65536"
74+
75+
ip_configuration = {
76+
ipv4_enabled = false
77+
private_network = google_compute_network.default.self_link
78+
}
79+
80+
sql_server_audit_config = var.sql_server_audit_config
81+
enable_default_db = false
82+
enable_default_user = false
83+
84+
depends_on = [
85+
google_service_networking_connection.vpc_connection,
86+
]
87+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Copyright 2019 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+
# Create Network with a subnetwork and private service access for both netapp.servicenetworking.goog and servicenetworking.googleapis.com
19+
20+
resource "google_compute_network" "default" {
21+
name = var.network_name
22+
project = var.project_id
23+
auto_create_subnetworks = false
24+
description = "test network"
25+
}
26+
27+
resource "google_compute_subnetwork" "subnetwork1" {
28+
name = "subnet-${local.region_1}-mssql"
29+
ip_cidr_range = "10.0.0.0/24"
30+
region = local.region_1
31+
project = var.project_id
32+
network = google_compute_network.default.self_link
33+
private_ip_google_access = true
34+
}
35+
36+
resource "google_compute_subnetwork" "subnetwork_2" {
37+
name = "subnet-${local.region_2}-mssql"
38+
ip_cidr_range = "10.0.1.0/24"
39+
region = local.region_2
40+
project = var.project_id
41+
network = google_compute_network.default.self_link
42+
private_ip_google_access = true
43+
}
44+
45+
46+
resource "google_compute_global_address" "private_ip_alloc" {
47+
project = var.project_id
48+
name = "psa-mssql"
49+
address_type = "INTERNAL"
50+
purpose = "VPC_PEERING"
51+
address = "10.10.0.0"
52+
prefix_length = 16
53+
network = google_compute_network.default.id
54+
}
55+
56+
resource "google_service_networking_connection" "vpc_connection" {
57+
network = google_compute_network.default.id
58+
service = "servicenetworking.googleapis.com"
59+
reserved_peering_ranges = [
60+
google_compute_global_address.private_ip_alloc.name,
61+
]
62+
deletion_policy = "ABANDON"
63+
64+
depends_on = [
65+
google_compute_subnetwork.subnetwork1,
66+
google_compute_subnetwork.subnetwork_2
67+
]
68+
}

0 commit comments

Comments
 (0)