-
Notifications
You must be signed in to change notification settings - Fork 368
/
main.tf
125 lines (109 loc) · 4.02 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 2019 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.
*/
locals {
hostname = var.hostname == "" ? "default" : var.hostname
num_instances = length(var.static_ips) == 0 ? var.num_instances : length(var.static_ips)
# local.static_ips is the same as var.static_ips with a dummy element appended
# at the end of the list to work around "list does not have any elements so cannot
# determine type" error when var.static_ips is empty
static_ips = concat(var.static_ips, ["NOT_AN_IP"])
zones = length(var.zones) == 0 ? data.google_compute_zones.available.names : var.zones
instance_group_count = min(
local.num_instances,
length(local.zones),
)
}
###############
# Data Sources
###############
data "google_compute_zones" "available" {
project = var.project_id
region = var.region
status = "UP"
}
#############
# Instances
#############
resource "google_compute_instance_from_template" "compute_instance" {
provider = google
count = local.num_instances
name = format("%s%s%s", local.hostname, var.hostname_suffix_separator, format("%03d", count.index + 1))
project = var.project_id
zone = local.zones[count.index % length(local.zones)]
network_interface {
network = var.network
subnetwork = var.subnetwork
subnetwork_project = var.subnetwork_project
network_ip = length(var.static_ips) == 0 ? "" : element(local.static_ips, count.index)
dynamic "access_config" {
# convert to map to use lookup function with default value
for_each = lookup({ for k, v in var.access_config : k => v }, count.index, [])
content {
nat_ip = access_config.value.nat_ip
network_tier = access_config.value.network_tier
}
}
dynamic "ipv6_access_config" {
# convert to map to use lookup function with default value
for_each = lookup({ for k, v in var.ipv6_access_config : k => v }, count.index, [])
content {
network_tier = ipv6_access_config.value.network_tier
}
}
}
dynamic "network_interface" {
for_each = var.additional_networks
content {
network = network_interface.value.network
subnetwork = network_interface.value.subnetwork
subnetwork_project = network_interface.value.subnetwork_project
network_ip = length(network_interface.value.network_ip) > 0 ? network_interface.value.network_ip : null
dynamic "access_config" {
for_each = network_interface.value.access_config
content {
nat_ip = access_config.value.nat_ip
network_tier = access_config.value.network_tier
}
}
dynamic "ipv6_access_config" {
for_each = network_interface.value.ipv6_access_config
content {
network_tier = ipv6_access_config.value.network_tier
}
}
}
}
source_instance_template = var.instance_template
}
resource "google_compute_instance_group" "instance_group" {
provider = google
count = local.instance_group_count
name = "${local.hostname}-instance-group-${format("%03d", count.index + 1)}"
project = var.project_id
zone = element(local.zones, count.index)
instances = matchkeys(
google_compute_instance_from_template.compute_instance[*].self_link,
google_compute_instance_from_template.compute_instance[*].zone,
[local.zones[count.index]],
)
dynamic "named_port" {
for_each = var.named_ports
content {
name = named_port.value.name
port = named_port.value.port
}
}
}