|
| 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 | + |
0 commit comments