@@ -30,6 +30,26 @@ variable "enable_flow_logs" {
3030 description = " whether to turn on flow logs or not"
3131}
3232
33+ variable "nat_ip_allocate_option" {
34+ # https://cloud.google.com/nat/docs/overview#ip_address_allocation
35+ description = " AUTO_ONLY or MANUAL_ONLY"
36+ default = " AUTO_ONLY"
37+ }
38+
39+ variable "cloud_nat_address_count" {
40+ # https://cloud.google.com/nat/docs/overview#number_of_nat_ports_and_connections
41+ description = " the count of external ip address to assign to the cloud-nat object"
42+ default = 1
43+ }
44+
45+ locals {
46+ # # the following locals modify resource creation behavior depending on var.nat_ip_allocate_option
47+ cloud_nat_address_count = " ${ var . nat_ip_allocate_option == " AUTO_ONLY" ? 0 : var . cloud_nat_address_count } "
48+ nat_ips = " ${ var . nat_ip_allocate_option == " AUTO_ONLY" ? " " : join (" ," , google_compute_address. ip_address . * . self_link )} "
49+ manual_nat_router = " ${ var . nat_ip_allocate_option == " AUTO_ONLY" ? 0 : 1 } "
50+ auto_nat_router = " ${ var . nat_ip_allocate_option == " AUTO_ONLY" ? 1 : 0 } "
51+ }
52+
3353# ######################
3454# Create the network and subnetworks, including secondary IP ranges on subnetworks
3555# ######################
@@ -61,7 +81,7 @@ resource "google_compute_subnetwork" "subnetwork" {
6181 }
6282
6383 /* We ignore changes on secondary_ip_range because terraform doesn't list
64- them in the same order every time during runs. */
84+ them in the same order every time during runs. */
6585 lifecycle {
6686 ignore_changes = [" secondary_ip_range" ]
6787 }
@@ -73,11 +93,31 @@ resource "google_compute_router" "router" {
7393 region = " ${ var . region } "
7494}
7595
96+ resource "google_compute_address" "ip_address" {
97+ # resource only created if var.nat_allocate_option != AUTO_ONLY
98+ count = " ${ local . cloud_nat_address_count } "
99+ name = " nat-external-address-${ count . index } "
100+ region = " ${ var . region } "
101+ }
102+
76103resource "google_compute_router_nat" "nat_router" {
104+ # resource only created if local.auto_nat_router evaulates to TRUE
105+ count = " ${ local . auto_nat_router } "
106+ name = " ${ var . network_name } "
107+ router = " ${ google_compute_router . router . name } "
108+ region = " ${ var . region } "
109+ nat_ip_allocate_option = " ${ var . nat_ip_allocate_option } "
110+ source_subnetwork_ip_ranges_to_nat = " ALL_SUBNETWORKS_ALL_IP_RANGES"
111+ }
112+
113+ resource "google_compute_router_nat" "manual_nat_router" {
114+ # resource only created if local.manual_nat_router evaulates to TRUE
115+ count = " ${ local . manual_nat_router } "
77116 name = " ${ var . network_name } "
78117 router = " ${ google_compute_router . router . name } "
79118 region = " ${ var . region } "
80- nat_ip_allocate_option = " AUTO_ONLY"
119+ nat_ip_allocate_option = " ${ var . nat_ip_allocate_option } "
120+ nat_ips = [" ${ split (" ," , local. nat_ips )} " ]
81121 source_subnetwork_ip_ranges_to_nat = " ALL_SUBNETWORKS_ALL_IP_RANGES"
82122}
83123
0 commit comments