Skip to content

Commit b0d2c19

Browse files
loeng2023jackwotherspoon
authored andcommitted
feat(cloud_sql): Add cloud sql psa+psc connectivity samples (terraform-google-modules#808)
* feat(cloud_sql): Add cloud sql psa+psc connectivity samples * fix region tags * Fix license year * remove unnecessary region tags * add short description at beginning * minor change * avoid use auto-subnet ip as static ip * fix lint --------- Co-authored-by: Jack Wotherspoon <[email protected]>
1 parent bf5a25f commit b0d2c19

File tree

9 files changed

+384
-1
lines changed

9 files changed

+384
-1
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
// Create a Cloud SQL MySQL instance with both Private IP and Private Service Connect enabled.
18+
# [START cloud_sql_mysql_instance_psa_psc_parent_tag]
19+
20+
resource "google_compute_network" "peering_network" {
21+
name = "private-network"
22+
auto_create_subnetworks = "false"
23+
}
24+
25+
resource "google_compute_global_address" "private_ip_address" {
26+
name = "private-ip-address"
27+
purpose = "VPC_PEERING"
28+
address_type = "INTERNAL"
29+
prefix_length = 16
30+
network = google_compute_network.peering_network.id
31+
}
32+
33+
resource "google_service_networking_connection" "default" {
34+
network = google_compute_network.peering_network.id
35+
service = "servicenetworking.googleapis.com"
36+
reserved_peering_ranges = [google_compute_global_address.private_ip_address.name]
37+
}
38+
39+
# [START cloud_sql_mysql_instance_psa_psc_instance]
40+
resource "google_sql_database_instance" "default" {
41+
name = "mysql-instance"
42+
region = "us-central1"
43+
database_version = "MYSQL_8_0"
44+
45+
depends_on = [google_service_networking_connection.default]
46+
47+
settings {
48+
tier = "db-f1-micro"
49+
ip_configuration {
50+
psc_config {
51+
psc_enabled = true
52+
allowed_consumer_projects = [] # Add consumer project IDs here.
53+
}
54+
ipv4_enabled = false
55+
private_network = google_compute_network.peering_network.id
56+
}
57+
}
58+
# set `deletion_protection` to true, will ensure that one cannot accidentally delete this instance by
59+
# use of Terraform whereas `deletion_protection_enabled` flag protects this instance at the GCP level.
60+
deletion_protection = false
61+
}
62+
# [END cloud_sql_mysql_instance_psa_psc_instance]
63+
64+
resource "google_compute_network_peering_routes_config" "peering_routes" {
65+
peering = google_service_networking_connection.default.peering
66+
network = google_compute_network.peering_network.name
67+
import_custom_routes = true
68+
export_custom_routes = true
69+
}
70+
71+
resource "google_compute_address" "default" {
72+
name = "psc-compute-address-${google_sql_database_instance.default.name}"
73+
region = "us-central1"
74+
address_type = "INTERNAL"
75+
subnetwork = "default" # Replace value with the name of the subnet here.
76+
address = "192.168.0.43" # Replace value with the IP address to reserve.
77+
}
78+
79+
data "google_sql_database_instance" "default" {
80+
name = resource.google_sql_database_instance.default.name
81+
}
82+
83+
resource "google_compute_forwarding_rule" "default" {
84+
name = "psc-forwarding-rule-${google_sql_database_instance.default.name}"
85+
region = "us-central1"
86+
network = "default"
87+
ip_address = google_compute_address.default.self_link
88+
load_balancing_scheme = ""
89+
target = data.google_sql_database_instance.default.psc_service_attachment_link
90+
}
91+
92+
# [END cloud_sql_mysql_instance_psa_psc_parent_tag]
93+
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: sql_mysql_instance_psa_psc
19+
spec:
20+
skip: true

cloud_sql/mysql_instance_psc/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ resource "google_sql_database_instance" "default" {
2929
ip_configuration {
3030
psc_config {
3131
psc_enabled = true
32-
allowed_consumer_projects = []
32+
allowed_consumer_projects = [] # Add consumer project IDs here.
3333
}
3434
ipv4_enabled = false
3535
}
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: sql_mysql_instance_psc
19+
spec:
20+
skip: true
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
// Create a Cloud SQL Postgres instance with both Private IP and Private Service Connect enabled.
18+
# [START cloud_sql_postgres_instance_psa_psc_parent_tag]
19+
20+
resource "google_compute_network" "peering_network" {
21+
name = "private-network"
22+
auto_create_subnetworks = "false"
23+
}
24+
25+
resource "google_compute_global_address" "private_ip_address" {
26+
name = "private-ip-address"
27+
purpose = "VPC_PEERING"
28+
address_type = "INTERNAL"
29+
prefix_length = 16
30+
network = google_compute_network.peering_network.id
31+
}
32+
33+
resource "google_service_networking_connection" "default" {
34+
network = google_compute_network.peering_network.id
35+
service = "servicenetworking.googleapis.com"
36+
reserved_peering_ranges = [google_compute_global_address.private_ip_address.name]
37+
}
38+
39+
# [START cloud_sql_postgres_instance_psa_psc_instance]
40+
resource "google_sql_database_instance" "default" {
41+
name = "postgres-instance"
42+
region = "us-central1"
43+
database_version = "POSTGRES_17"
44+
45+
depends_on = [google_service_networking_connection.default]
46+
47+
settings {
48+
tier = "db-custom-2-7680"
49+
availability_type = "REGIONAL"
50+
backup_configuration {
51+
enabled = true
52+
}
53+
ip_configuration {
54+
psc_config {
55+
psc_enabled = true
56+
allowed_consumer_projects = [] # Add consumer project IDs here.
57+
}
58+
ipv4_enabled = false
59+
private_network = google_compute_network.peering_network.id
60+
}
61+
}
62+
# set `deletion_protection` to true, will ensure that one cannot accidentally delete this instance by
63+
# use of Terraform whereas `deletion_protection_enabled` flag protects this instance at the GCP level.
64+
deletion_protection = false # Set to "true" to prevent destruction of the resource
65+
}
66+
# [END cloud_sql_postgres_instance_psa_psc_instance]
67+
68+
resource "google_compute_network_peering_routes_config" "peering_routes" {
69+
peering = google_service_networking_connection.default.peering
70+
network = google_compute_network.peering_network.name
71+
import_custom_routes = true
72+
export_custom_routes = true
73+
}
74+
75+
resource "google_compute_address" "default" {
76+
name = "psc-compute-address"
77+
region = "us-central1"
78+
address_type = "INTERNAL"
79+
subnetwork = "default" # Replace value with the name of the subnet here.
80+
address = "192.168.0.42" # Replace value with the IP address to reserve.
81+
}
82+
83+
data "google_sql_database_instance" "default" {
84+
name = resource.google_sql_database_instance.default.name
85+
}
86+
87+
resource "google_compute_forwarding_rule" "default" {
88+
name = "psc-forwarding-rule-${google_sql_database_instance.default.name}"
89+
region = "us-central1"
90+
network = "default"
91+
ip_address = google_compute_address.default.self_link
92+
load_balancing_scheme = ""
93+
target = data.google_sql_database_instance.default.psc_service_attachment_link
94+
}
95+
96+
# [END cloud_sql_postgres_instance_psa_psc_parent_tag]
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: sql_postgres_instance_psa_psc
19+
spec:
20+
skip: true
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: sql_postgres_instance_psc
19+
spec:
20+
skip: true
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
// Create a Cloud SQL SQL server instance with both Private IP and Private Service Connect enabled.
18+
# [START cloud_sql_sqlserver_instance_psa_psc_parent_tag]
19+
20+
resource "google_compute_network" "peering_network" {
21+
name = "private-network"
22+
auto_create_subnetworks = "false"
23+
}
24+
25+
resource "google_compute_global_address" "private_ip_address" {
26+
name = "private-ip-address"
27+
purpose = "VPC_PEERING"
28+
address_type = "INTERNAL"
29+
prefix_length = 16
30+
network = google_compute_network.peering_network.id
31+
}
32+
33+
resource "google_service_networking_connection" "default" {
34+
network = google_compute_network.peering_network.id
35+
service = "servicenetworking.googleapis.com"
36+
reserved_peering_ranges = [google_compute_global_address.private_ip_address.name]
37+
}
38+
39+
# [START cloud_sql_sqlserver_instance_psa_psc_instance]
40+
resource "google_sql_database_instance" "default" {
41+
name = "sqlserver-instance"
42+
region = "us-central1"
43+
database_version = "SQLSERVER_2019_STANDARD"
44+
root_password = "INSERT-PASSWORD-HERE"
45+
46+
depends_on = [google_service_networking_connection.default]
47+
48+
settings {
49+
tier = "db-custom-2-7680"
50+
ip_configuration {
51+
psc_config {
52+
psc_enabled = true
53+
allowed_consumer_projects = [] # Add consumer project IDs here.
54+
}
55+
ipv4_enabled = false
56+
private_network = google_compute_network.peering_network.id
57+
}
58+
}
59+
# set `deletion_protection` to true, will ensure that one cannot accidentally delete this instance by
60+
# use of Terraform whereas `deletion_protection_enabled` flag protects this instance at the GCP level.
61+
deletion_protection = false
62+
}
63+
# [END cloud_sql_sqlserver_instance_psa_psc_instance]
64+
65+
resource "google_compute_network_peering_routes_config" "peering_routes" {
66+
peering = google_service_networking_connection.default.peering
67+
network = google_compute_network.peering_network.name
68+
import_custom_routes = true
69+
export_custom_routes = true
70+
}
71+
72+
resource "google_compute_address" "default" {
73+
name = "psc-compute-address-${google_sql_database_instance.default.name}"
74+
region = "us-central1"
75+
address_type = "INTERNAL"
76+
subnetwork = "default" # Replace value with the name of the subnet here.
77+
address = "192.168.0.44" # Replace value with the IP address to reserve.
78+
}
79+
80+
data "google_sql_database_instance" "default" {
81+
name = resource.google_sql_database_instance.default.name
82+
}
83+
84+
resource "google_compute_forwarding_rule" "default" {
85+
name = "psc-forwarding-rule-${google_sql_database_instance.default.name}"
86+
region = "us-central1"
87+
network = "default"
88+
ip_address = google_compute_address.default.self_link
89+
load_balancing_scheme = ""
90+
target = data.google_sql_database_instance.default.psc_service_attachment_link
91+
}
92+
93+
# [END cloud_sql_sqlserver_instance_psa_psc_parent_tag]
94+
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: sqlserver_instance_psa_psc
19+
spec:
20+
skip: true

0 commit comments

Comments
 (0)