forked from aws-solutions-library-samples/guidance-for-game-server-hosting-using-agones-and-open-match-on-amazon-eks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
338 lines (272 loc) · 8.06 KB
/
main.tf
File metadata and controls
338 lines (272 loc) · 8.06 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
## Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
## SPDX-License-Identifier: MIT-0
provider "aws" {
region = var.cluster_region
}
locals {
repo_root = "${path.module}/../.."
name = var.cluster_name
cluster_region = var.cluster_region
gameserver_minport = 7000
gameserver_maxport = 7029
}
provider "helm" {
kubernetes {
host = var.cluster_endpoint
cluster_ca_certificate = base64decode(var.cluster_certificate_authority_data)
# token = var.cluster_token
# config_path = "~/.kube/config"
exec {
api_version = "client.authentication.k8s.io/v1beta1"
command = "aws"
# This requires the awscli to be installed locally where Terraform is executed
args = ["eks", "get-token", "--cluster-name", var.cluster_name]
}
}
}
provider "kubernetes" {
host = var.cluster_endpoint
cluster_ca_certificate = base64decode(var.cluster_certificate_authority_data)
# token = var.cluster_token
exec {
api_version = "client.authentication.k8s.io/v1beta1"
command = "aws"
# This requires the awscli to be installed locally where Terraform is executed
args = ["eks", "get-token", "--cluster-name", var.cluster_name]
}
}
resource "kubernetes_namespace" "this" {
# provider = local.kubernetes_alias
for_each = toset(var.namespaces)
metadata {
name = each.key
}
provisioner "local-exec" {
when = destroy
command = "nohup ${path.module}/../../scripts/namespace-finalizer.sh ${each.key} 2>&1 &"
# command = "nohup ${path.module}/../../scripts/namespace-finalizer.sh ${var.cluster_name} ${each.key} 2>&1 &"
}
}
################################################################################
# Kubernetes Addons
################################################################################
module "eks_blueprints_addons" {
#checkov:skip=CKV_TF_1:Module registry does not support commit hashes for versions
source = "aws-ia/eks-blueprints-addons/aws"
version = "~> 1.0" #ensure to update this to the latest/desired version
cluster_name = var.cluster_name
cluster_endpoint = var.cluster_endpoint
cluster_version = var.cluster_version
oidc_provider_arn = var.oidc_provider_arn
eks_addons = {
aws-ebs-csi-driver = {
most_recent = true
}
coredns = {
most_recent = true
}
vpc-cni = {
most_recent = true
configuration_values = jsonencode({
env = {
AWS_VPC_K8S_CNI_EXTERNALSNAT = "true"
} })
}
kube-proxy = {
most_recent = true
}
}
# Opinions for our game servers cluster
aws_for_fluentbit = {
set = [{
name = "cloudWatchLogs.autoCreateGroup"
value = true
}]
}
enable_metrics_server = true
enable_aws_cloudwatch_metrics = true
enable_aws_for_fluentbit = true
enable_aws_load_balancer_controller = true
aws_load_balancer_controller = {
set = [{
name = "enableServiceMutatorWebhook"
value = "false"
}]
}
enable_cert_manager = true
}
################################################################################
# Agones Helm Chart
################################################################################
resource "helm_release" "agones" {
# provider = local.helm_alias
name = "agones"
chart = "agones"
repository = "https://agones.dev/chart/stable"
version = "1.36.0"
namespace = "agones-system"
create_namespace = false
timeout = 1800
wait = false
values = [templatefile("${path.root}/helm_values/agones-helm-values.yaml", {
expose_udp = true
gameserver_namespaces = "{${join(",", ["default"])}}"
gameserver_minport = local.gameserver_minport
gameserver_maxport = local.gameserver_maxport
})]
set {
name = "agones.allocator.service.annotations.service\\.beta\\.kubernetes\\.io/aws-load-balancer-name"
value = "${var.cluster_name}-allocator"
type = "string"
}
set {
name = "agones.ping.http.annotations.service\\.beta\\.kubernetes\\.io/aws-load-balancer-name"
value = "${var.cluster_name}-ping-http"
type = "string"
}
set {
name = "agones.ping.udp.annotations.service\\.beta\\.kubernetes\\.io/aws-load-balancer-name"
value = "${var.cluster_name}-ping-udp"
type = "string"
}
# set {
# name = "agones.controller.labels.agones-controller"
# value = "true"
# type = "string"
# }
set {
name = "agones.extensions.nodeSelector.agones\\.dev/agones-system"
value = "true"
type = "string"
}
set {
name = "agones.ping.nodeSelector.agones\\.dev/agones-system"
value = "true"
type = "string"
}
set {
name = "agones.allocator.nodeSelector.agones\\.dev/agones-system"
value = "true"
type = "string"
}
depends_on = [
module.eks_blueprints_addons,
kubernetes_namespace.this
# null_resource.generate_agones_certs
]
}
################################################################################
# OpenMatch Helm Chart
################################################################################
resource "helm_release" "open-match" {
# provider = local.helm_alias
count = var.configure_open_match ? 1 : 0
name = "open-match"
chart = "open-match"
repository = "https://open-match.dev/chart/stable"
version = "1.8.0"
namespace = "open-match"
create_namespace = false
timeout = 1800
wait = false
set {
name = "open-match-customize.enabled"
value = "true"
}
set {
name = "open-match-customize.evaluator.enabled"
value = "true"
}
set {
name = "open-match-override.enabled"
value = "true"
}
set {
name = "open-match-core.swaggerui.enabled"
value = "true"
}
set {
name = "global.tls.enabled"
value = "true"
}
set {
name = "global.kubernetes.nodeSelector.openmatch"
value = "system"
}
set {
name = "redis.master.nodeSelector"
value = "\\{\"openmatch\": \"system\"\\}"
}
set {
name = "redis.image.tag"
value = "latest"
}
set {
name = "redis.metrics.image.tag"
value = "latest"
}
set {
name = "redis.sysctl.image.tag"
value = "latest"
}
depends_on = [
module.eks_blueprints_addons,
null_resource.generate_agones_certs,
kubernetes_namespace.this
]
}
resource "null_resource" "agones_tls_configuration" {
# provider = local.kubernetes_alias
count = var.configure_agones ? 1 : 0
triggers = {
always_run = "${timestamp()}"
}
provisioner "local-exec" {
when = create
command = "nohup ${local.repo_root}/scripts/configure-agones-tls.sh ${local.name} ${local.repo_root}&"
}
depends_on = [
helm_release.agones,
null_resource.generate_agones_certs
]
}
resource "null_resource" "allocator_tls_files" {
triggers = {
always_run = "${timestamp()}"
}
provisioner "local-exec" {
when = create
command = "nohup ${local.repo_root}/scripts/generate-tls-files.sh ${var.cluster_name} ${local.repo_root} &"
}
depends_on = [
module.eks_blueprints_addons,
null_resource.agones_tls_configuration
]
}
resource "null_resource" "open_match_ingress_configuration" {
count = var.configure_open_match ? 1 : 0
triggers = {
always_run = "${timestamp()}"
}
provisioner "local-exec" {
when = create
command = "nohup ${local.repo_root}/scripts/configure-open-match-ingress.sh ${var.cluster_name} ${local.repo_root} &"
}
depends_on = [
helm_release.open-match,
null_resource.agones_tls_configuration
]
}
resource "null_resource" "generate_agones_certs" {
triggers = {
always_run = "${timestamp()}"
}
provisioner "local-exec" {
when = create
command = "nohup ${local.repo_root}/scripts/generate-agones-certs.sh ${var.cluster_name} ${local.repo_root} &"
}
depends_on = [
module.eks_blueprints_addons,
kubernetes_namespace.this
]
}