This repository has been archived by the owner on Dec 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 133
/
main.tf
124 lines (110 loc) · 4.34 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Copyright 2017 Google Inc.
*
* 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.
*/
data "template_file" "nat-startup-script" {
template = "${file("${format("%s/config/startup.sh", path.module)}")}"
vars {
squid_enabled = "${var.squid_enabled}"
squid_config = "${var.squid_config}"
module_path = "${path.module}"
}
}
data "google_compute_network" "network" {
name = "${var.network}"
project = "${var.network_project == "" ? var.project : var.network_project}"
}
data "google_compute_address" "default" {
count = "${var.ip_address_name == "" ? 0 : 1}"
name = "${var.ip_address_name}"
project = "${var.network_project == "" ? var.project : var.network_project}"
region = "${var.region}"
}
locals {
zone = "${var.zone == "" ? lookup(var.region_params["${var.region}"], "zone") : var.zone}"
name = "${var.name}nat-gateway-${local.zone}"
instance_tags = ["inst-${local.zonal_tag}", "inst-${local.regional_tag}"]
zonal_tag = "${var.name}nat-${local.zone}"
regional_tag = "${var.name}nat-${var.region}"
}
module "nat-gateway" {
source = "GoogleCloudPlatform/managed-instance-group/google"
version = "1.1.15"
module_enabled = "${var.module_enabled}"
project = "${var.project}"
region = "${var.region}"
zone = "${local.zone}"
network = "${var.network}"
subnetwork = "${var.subnetwork}"
target_tags = ["${local.instance_tags}"]
instance_labels = "${var.instance_labels}"
service_account_email = "${var.service_account_email}"
machine_type = "${var.machine_type}"
name = "${local.name}"
compute_image = "${var.compute_image}"
size = 1
network_ip = "${var.ip}"
can_ip_forward = "true"
service_port = "80"
service_port_name = "http"
startup_script = "${data.template_file.nat-startup-script.rendered}"
wait_for_instances = true
metadata = "${var.metadata}"
ssh_fw_rule = "${var.ssh_fw_rule}"
ssh_source_ranges = "${var.ssh_source_ranges}"
http_health_check = "${var.autohealing_enabled}"
update_strategy = "ROLLING_UPDATE"
rolling_update_policy = [
{
type = "PROACTIVE"
minimal_action = "REPLACE"
max_surge_fixed = 0
max_unavailable_fixed = 1
min_ready_sec = 30
},
]
access_config = [
{
nat_ip = "${element(concat(google_compute_address.default.*.address, data.google_compute_address.default.*.address, list("")), 0)}"
},
]
}
resource "google_compute_route" "nat-gateway" {
count = "${var.module_enabled ? 1 : 0}"
name = "${local.zonal_tag}"
project = "${var.project}"
dest_range = "${var.dest_range}"
network = "${data.google_compute_network.network.self_link}"
next_hop_instance = "${element(split("/", element(module.nat-gateway.instances[0], 0)), 10)}"
next_hop_instance_zone = "${local.zone}"
tags = ["${compact(concat(list("${local.regional_tag}", "${local.zonal_tag}"), var.tags))}"]
priority = "${var.route_priority}"
}
resource "google_compute_firewall" "nat-gateway" {
count = "${var.module_enabled ? 1 : 0}"
name = "${local.zonal_tag}"
network = "${var.network}"
project = "${var.project}"
allow {
protocol = "all"
}
source_tags = ["${compact(concat(list("${local.regional_tag}", "${local.zonal_tag}"), var.tags))}"]
target_tags = ["${compact(concat(local.instance_tags, var.tags))}"]
}
resource "google_compute_address" "default" {
count = "${var.module_enabled && var.ip_address_name == "" ? 1 : 0}"
name = "${local.zonal_tag}"
project = "${var.project}"
region = "${var.region}"
}