-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.tf
More file actions
182 lines (153 loc) · 4.66 KB
/
main.tf
File metadata and controls
182 lines (153 loc) · 4.66 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
provider "google" {
project = var.gcp_project
region = var.gcp_region
}
# the zone variable must be within the region
# hence this weird setup
locals {
zone = "${var.gcp_region}-${var.gcp_zone}"
}
# this data item gives us the latest available image
data "google_compute_image" "host-image" {
family = var.image_family
project = var.image_project
}
# we want our instances to be able to talk to each other directly
# hence we add them all to a dedicated network
resource "google_compute_network" "enoki-network" {
name = "enoki-network"
description = "connections between hosts"
auto_create_subnetworks = false
}
# within our network, we need a subnet for this region that has the correct
# IP address range
resource "google_compute_subnetwork" "enoki-subnet" {
name = "enoki-subnetwork"
ip_cidr_range = "192.168.10.0/24"
region = var.gcp_region
network = google_compute_network.enoki-network.id
}
# # we need to explicitly enable communication between instances in that network
# # as google cloud doesn't add any rules by default
# resource "google_compute_firewall" "enoki-net-firewall-internal" {
# name = "enoki-net-firewall-internal"
# description = "This firewall allows internal communication in the network."
# direction = "INGRESS"
# network = google_compute_network.enoki-network.id
# source_ranges = ["${google_compute_subnetwork.enoki-subnet.ip_cidr_range}"]
# allow {
# protocol = "all"
# }
# }
# we also need to enable ssh ingress to our machines
resource "google_compute_firewall" "enoki-net-firewall-ssh" {
name = "enoki-net-firewall-ssh"
description = "This firewall allows ssh connections to our instances."
network = google_compute_network.enoki-network.id
direction = "INGRESS"
source_ranges = ["0.0.0.0/0"]
allow {
protocol = "tcp"
# ports:
## 22: ssh
## 2379: etcd
## 8000: tinyfaas rproxy http
## 8080: tinyfaas manager
## 9001: fred
## 5555: fred peering
ports = ["22", "2379", "8000", "8080", "9001", "5555"]
}
}
# the cloud instance runs Ubuntu 22.04 and has Docker installed
resource "google_compute_instance" "enoki-cloud" {
name = "enoki-cloud"
machine_type = var.cloud_host_type
zone = local.zone
boot_disk {
initialize_params {
image = data.google_compute_image.host-image.self_link
size = var.boot_disk_size_gb
}
}
# adapter for internal network
network_interface {
subnetwork = google_compute_subnetwork.enoki-subnet.self_link
network_ip = "192.168.10.2"
# put this empty block in to get a public IP
access_config {
}
}
service_account {
scopes = ["cloud-platform"]
}
}
# the edge instance runs Ubuntu 22.04 and has Docker installed
resource "google_compute_instance" "enoki-edge" {
name = "enoki-edge"
machine_type = var.edge_host_type
zone = local.zone
boot_disk {
initialize_params {
image = data.google_compute_image.host-image.self_link
size = var.boot_disk_size_gb
}
}
# adapter for internal network
network_interface {
subnetwork = google_compute_subnetwork.enoki-subnet.self_link
network_ip = "192.168.10.3"
# put this empty block in to get a public IP
access_config {
}
}
service_account {
scopes = ["cloud-platform"]
}
}
resource "google_compute_instance" "enoki-edge2" {
name = "enoki-edge2"
machine_type = var.edge_host_type
zone = local.zone
# only create this instance if the variable is set to true
count = var.second_edge_host ? 1 : 0
boot_disk {
initialize_params {
image = data.google_compute_image.host-image.self_link
size = var.boot_disk_size_gb
}
}
# adapter for internal network
network_interface {
subnetwork = google_compute_subnetwork.enoki-subnet.self_link
network_ip = "192.168.10.5"
# put this empty block in to get a public IP
access_config {
}
}
service_account {
scopes = ["cloud-platform"]
}
}
# the edge instance runs Ubuntu 22.04 and has nothing installed
resource "google_compute_instance" "enoki-client" {
name = "enoki-client"
machine_type = var.client_host_type
zone = local.zone
boot_disk {
initialize_params {
image = data.google_compute_image.host-image.self_link
size = var.boot_disk_size_gb
}
}
# adapter for internal network
network_interface {
subnetwork = google_compute_subnetwork.enoki-subnet.self_link
network_ip = "192.168.10.4"
# put this empty block in to get a public IP
access_config {
}
}
service_account {
scopes = ["cloud-platform"]
}
}