-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f514921
commit 92245b6
Showing
9 changed files
with
241 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# Copyright 2024 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. | ||
|
||
|
||
## Create network | ||
resource "google_compute_network" "network" { | ||
project = var.project_id | ||
name = var.network_name | ||
auto_create_subnetworks = var.auto_create_subnetworks | ||
routing_mode = var.routing_mode | ||
description = var.description | ||
} | ||
|
||
locals { | ||
subnets = { | ||
for x in var.subnets : | ||
"${x.subnet_region}/${x.subnet_name}" => x | ||
} | ||
} | ||
|
||
## Create subnetwork | ||
resource "google_compute_subnetwork" "subnetwork" { | ||
for_each = local.subnets | ||
name = each.value.subnet_name | ||
ip_cidr_range = each.value.subnet_ip | ||
region = each.value.subnet_region | ||
private_ip_google_access = lookup(each.value, "subnet_private_access", "false") | ||
private_ipv6_google_access = lookup(each.value, "subnet_private_ipv6_access", null) | ||
dynamic "log_config" { | ||
for_each = coalesce(lookup(each.value, "subnet_flow_logs", null), false) ? [{ | ||
aggregation_interval = each.value.subnet_flow_logs_interval | ||
flow_sampling = each.value.subnet_flow_logs_sampling | ||
metadata = each.value.subnet_flow_logs_metadata | ||
filter_expr = each.value.subnet_flow_logs_filter | ||
metadata_fields = each.value.subnet_flow_logs_metadata_fields | ||
}] : [] | ||
content { | ||
aggregation_interval = log_config.value.aggregation_interval | ||
flow_sampling = log_config.value.flow_sampling | ||
metadata = log_config.value.metadata | ||
filter_expr = log_config.value.filter_expr | ||
metadata_fields = log_config.value.metadata == "CUSTOM_METADATA" ? log_config.value.metadata_fields : null | ||
} | ||
} | ||
network = google_compute_network.network.name | ||
project = var.project_id | ||
description = lookup(each.value, "description", null) | ||
dynamic "secondary_ip_range" { | ||
for_each = contains(keys(var.secondary_ranges), each.value.subnet_name) == true ? var.secondary_ranges[each.value.subnet_name] : [] | ||
|
||
content { | ||
range_name = secondary_ip_range.value.range_name | ||
ip_cidr_range = secondary_ip_range.value.ip_cidr_range | ||
} | ||
} | ||
|
||
purpose = lookup(each.value, "purpose", null) | ||
role = lookup(each.value, "role", null) | ||
stack_type = lookup(each.value, "stack_type", null) | ||
ipv6_access_type = lookup(each.value, "ipv6_access_type", null) | ||
|
||
lifecycle { | ||
ignore_changes = [ secondary_ip_range ] | ||
} | ||
} | ||
|
||
// TODO: Migrate to terraform-google-modules/sql-db/google//modules/private_service_access | ||
// once https://github.com/terraform-google-modules/terraform-google-sql-db/issues/585 is resolved. | ||
// We define a VPC peering subnet that will be peered with the | ||
// Cloud SQL instance network. The Cloud SQL instance will | ||
// have a private IP within the provided range. | ||
// https://cloud.google.com/vpc/docs/configure-private-services-access | ||
|
||
resource "google_compute_global_address" "google-managed-services-range" { | ||
count = var.create_psa ? 1 : 0 | ||
project = var.project_id | ||
name = "google-managed-services-${var.network_name}" | ||
purpose = "VPC_PEERING" | ||
address_type = "INTERNAL" | ||
prefix_length = 16 | ||
network = google_compute_network.network.self_link | ||
} | ||
|
||
# Creates the peering with the producer network. | ||
resource "google_service_networking_connection" "private_service_access" { | ||
count = var.create_psa ? 1 : 0 | ||
network = google_compute_network.network.self_link | ||
service = "servicenetworking.googleapis.com" | ||
reserved_peering_ranges = [google_compute_global_address.google-managed-services-range[0].name] | ||
# This will enable a successful terraform destroy when destroying CloudSQL instances | ||
deletion_policy = "ABANDON" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Copyright 2024 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. | ||
|
||
output "network_name" { | ||
value = google_compute_network.network.name | ||
} | ||
|
||
output "subnets_names" { | ||
value = [ for sb in google_compute_subnetwork.subnetwork : sb.name ] | ||
} | ||
|
||
output "subnets_ips" { | ||
value = [ for sb in google_compute_subnetwork.subnetwork : sb.ip_cidr_range ] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Copyright 2024 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. | ||
|
||
variable "project_id" { | ||
description = "The ID of the project where this VPC will be created" | ||
type = string | ||
} | ||
|
||
variable "network_name" { | ||
description = "The name of the network being created" | ||
type = string | ||
} | ||
|
||
variable "routing_mode" { | ||
type = string | ||
default = "GLOBAL" | ||
description = "The network routing mode (default 'GLOBAL')" | ||
} | ||
|
||
variable "shared_vpc_host" { | ||
type = bool | ||
description = "Makes this project a Shared VPC host if 'true' (default 'false')" | ||
default = false | ||
} | ||
|
||
variable "description" { | ||
type = string | ||
description = "An optional description of this resource. The resource must be recreated to modify this field." | ||
default = "" | ||
} | ||
|
||
variable "auto_create_subnetworks" { | ||
type = bool | ||
description = "When set to true, the network is created in 'auto subnet mode' and it will create a subnet for each region automatically across the 10.128.0.0/9 address range. When set to false, the network is created in 'custom subnet mode' so the user can explicitly connect subnetwork resources." | ||
default = false | ||
} | ||
|
||
variable "subnets" { | ||
type = list(object({ | ||
subnet_name = string | ||
subnet_ip = string | ||
subnet_region = string | ||
subnet_private_access = optional(string, "false") | ||
subnet_private_ipv6_access = optional(string) | ||
subnet_flow_logs = optional(string, "false") | ||
subnet_flow_logs_interval = optional(string, "INTERVAL_5_SEC") | ||
subnet_flow_logs_sampling = optional(string, "0.5") | ||
subnet_flow_logs_metadata = optional(string, "INCLUDE_ALL_METADATA") | ||
subnet_flow_logs_filter = optional(string, "true") | ||
subnet_flow_logs_metadata_fields = optional(list(string), []) | ||
description = optional(string) | ||
purpose = optional(string) | ||
role = optional(string) | ||
stack_type = optional(string) | ||
ipv6_access_type = optional(string) | ||
})) | ||
description = "The list of subnets being created" | ||
} | ||
|
||
variable "secondary_ranges" { | ||
type = map(list(object({ range_name = string, ip_cidr_range = string }))) | ||
description = "Secondary ranges that will be used in some of the subnets" | ||
default = {} | ||
} | ||
|
||
variable "create_psa" { | ||
type = bool | ||
description = "Enable PSA for the network" | ||
} |
Oops, something went wrong.