Skip to content

Commit 4993891

Browse files
Add cloud nat submodule (#22)
* Add cloud nat submodule * Formating, allow bring existing router * terraform-docs: automated action --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 634cb28 commit 4993891

File tree

8 files changed

+69
-20
lines changed

8 files changed

+69
-20
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ No requirements.
8181

8282
| Name | Source | Version |
8383
|------|--------|---------|
84-
| <a name="module_cloud_nat"></a> [cloud\_nat](#module\_cloud\_nat) | registry.terraform.io/terraform-google-modules/cloud-nat/google | 5.1.0 |
84+
| <a name="module_cloud_nat"></a> [cloud\_nat](#module\_cloud\_nat) | ./modules/cloud-nat | n/a |
8585
| <a name="module_network"></a> [network](#module\_network) | registry.terraform.io/terraform-google-modules/network/google | 9.1.0 |
8686
| <a name="module_project"></a> [project](#module\_project) | registry.terraform.io/terraform-google-modules/project-factory/google | 15.0.1 |
8787
| <a name="module_project_services"></a> [project\_services](#module\_project\_services) | terraform-google-modules/project-factory/google//modules/project_services | 15.0.1 |
@@ -92,7 +92,6 @@ No requirements.
9292
|------|------|
9393
| [google-beta_google_container_node_pool.pools](https://registry.terraform.io/providers/hashicorp/google-beta/latest/docs/resources/google_container_node_pool) | resource |
9494
| [google_artifact_registry_repository.my-repo](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/artifact_registry_repository) | resource |
95-
| [google_compute_address.cloud_nat_address](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/compute_address) | resource |
9695
| [google_container_cluster.gke](https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/container_cluster) | resource |
9796

9897
## Inputs

examples/existing-project/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module "gke" {
1010
regional = false
1111
zones = ["europe-central2-a"]
1212
cluster_deletion_protection = false
13+
enable_private_nodes = true
1314
node_pools = {
1415
default-pool = {
1516
disk_size_gb = 50

main.tf

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,14 @@ module "network" {
4848
}
4949
}
5050

51-
resource "google_compute_address" "cloud_nat_address" {
52-
name = local.cloud_nat_name
53-
project = local.project_id
54-
region = var.region
55-
count = var.enable_private_nodes ? 1 : 0
56-
}
57-
5851
module "cloud_nat" {
59-
source = "registry.terraform.io/terraform-google-modules/cloud-nat/google"
60-
version = "5.1.0"
61-
project_id = local.project_id
62-
region = var.region
63-
network = module.network.network_name
64-
create_router = true
65-
router = local.router
66-
name = local.cloud_nat_name
67-
nat_ips = [google_compute_address.cloud_nat_address.0.self_link]
68-
count = var.enable_private_nodes ? 1 : 0
52+
source = "./modules/cloud-nat"
53+
project_id = local.project_id
54+
region = var.region
55+
network = module.network.network_name
56+
router = local.router
57+
name = local.cloud_nat_name
58+
count = var.enable_private_nodes ? 1 : 0
6959
}
7060

7161
resource "google_container_cluster" "gke" {

modules/cloud-nat/locals.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
locals {
2+
router = var.create_router ? google_compute_router.router[0].name : var.router
3+
}

modules/cloud-nat/main.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
resource "google_compute_router" "router" {
2+
count = var.create_router ? 1 : 0
3+
name = var.router
4+
project = var.project_id
5+
region = var.region
6+
network = var.network
7+
}
8+
9+
resource "google_compute_address" "cloud_nat_address" {
10+
name = var.name
11+
project = var.project_id
12+
region = var.region
13+
}
14+
15+
resource "google_compute_router_nat" "main" {
16+
project = var.project_id
17+
region = var.region
18+
name = var.name
19+
router = local.router
20+
nat_ip_allocate_option = "MANUAL_ONLY"
21+
nat_ips = [google_compute_address.cloud_nat_address.self_link]
22+
source_subnetwork_ip_ranges_to_nat = var.source_subnetwork_ip_ranges_to_nat
23+
}

modules/cloud-nat/outputs.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "cloud_nat_ip" {
2+
value = google_compute_address.cloud_nat_address.address
3+
}

modules/cloud-nat/variables.tf

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
variable "create_router" {
2+
type = bool
3+
default = true
4+
description = "Whether to create router or use the existing one."
5+
}
6+
variable "router" {
7+
type = string
8+
description = "The name of the router to create or the existing one."
9+
}
10+
variable "project_id" {
11+
type = string
12+
description = "GCP project where to create the resources."
13+
}
14+
variable "region" {
15+
type = string
16+
description = "The GCP region."
17+
}
18+
variable "network" {
19+
type = string
20+
description = "The VPC name."
21+
}
22+
variable "name" {
23+
type = string
24+
description = "The name of the NAT router."
25+
}
26+
variable "source_subnetwork_ip_ranges_to_nat" {
27+
type = string
28+
description = "How NAT should be configured per Subnetwork. Valid values include: ALL_SUBNETWORKS_ALL_IP_RANGES, ALL_SUBNETWORKS_ALL_PRIMARY_IP_RANGES, LIST_OF_SUBNETWORKS. Changing this forces a new NAT to be created."
29+
default = "ALL_SUBNETWORKS_ALL_IP_RANGES"
30+
}

outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ output "gke_zones" {
4343
description = "List of zones where the cluster lives"
4444
}
4545
output "nat_ip" {
46-
value = google_compute_address.cloud_nat_address.*.address
46+
value = module.cloud_nat.*.cloud_nat_ip
4747
description = "The IP address allocated for NAT"
4848
}
4949
output "subnetwork_name" {

0 commit comments

Comments
 (0)