-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
main.tf
418 lines (364 loc) · 9.32 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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
provider "eksctl" {}
provider "helmfile" {}
provider "kubectl" {}
terraform {
required_providers {
eksctl = {
source = "mumoshu/eksctl"
version = "0.0.1"
}
helmfile = {
source = "mumoshu/helmfile"
version = "0.0.1"
}
kubectl = {
source = "mumoshu/kubectl"
version = "0.0.1"
}
}
}
variable "region" {
default = "us-east-2"
description = "AWS region"
}
variable "role_arn" {
}
variable "myip" {
}
provider "aws" {
version = ">= 2.70.0"
region = "us-east-2"
ignore_tags {
key_prefixes = [
"kubernetes.io/cluster/"]
}
}
data "aws_availability_zones" "available" {}
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "2.6.0"
name = "training-vpc"
cidr = "192.168.0.0/16"
azs = data.aws_availability_zones.available.names
private_subnets = [
"192.168.96.0/19",
"192.168.128.0/19",
"192.168.160.0/19"]
public_subnets = [
"192.168.0.0/19",
"192.168.32.0/19",
"192.168.64.0/19"]
enable_nat_gateway = true
single_nat_gateway = true
enable_dns_hostnames = true
public_subnet_tags = {
// https://docs.aws.amazon.com/eks/latest/userguide/network_reqs.html#vpc-subnet-tagging
"kubernetes.io/role/elb" = "1"
}
private_subnet_tags = {
// https://docs.aws.amazon.com/eks/latest/userguide/network_reqs.html#vpc-subnet-tagging
"kubernetes.io/role/internal-elb" = "1"
}
// The following tags are created/deleted by the provider:
//
// tags = {
// "kubernetes.io/cluster/${local.cluster_name}" = "shared"
// }
//
// public_subnet_tags = {
// "kubernetes.io/cluster/${local.cluster_name}" = "shared"
// "kubernetes.io/role/elb" = "1"
// }
//
// private_subnet_tags = {
// "kubernetes.io/cluster/${local.cluster_name}" = "shared"
// "kubernetes.io/role/internal-elb" = "1"
// }
// Wd wanna prevent provider-added tags to result in detected changes on plan.
// But terraform doesn't allow injecting `lifecycle` block into module-managed resources.
//
//lifecycle {
// ignore_changes = ["tags.\"kubernetes.io/cluster/\".%"]
//}
//
// Instead, we use ignore_tags provided by the aws provider. See:
// - https://github.com/terraform-aws-modules/terraform-aws-vpc/issues/188#issuecomment-558627861
// - https://www.terraform.io/docs/providers/aws/#ignore_tags-configuration-block
}
locals {
myip_cidr = "${var.myip}/32"
podinfo_nodeport = 30080
}
resource "aws_security_group" "allow_http_from_me" {
name = "allow_http_from_me"
description = "Allow inbound traffic"
vpc_id = "${module.vpc.vpc_id}"
ingress {
description = "HTTTP from me"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = [
local.myip_cidr]
}
tags = {
Name = "allow_http_from_me"
}
}
resource "aws_security_group" "public_alb" {
name = "public_alb"
description = "Allow healthcheck against private and public nodes"
vpc_id = "${module.vpc.vpc_id}"
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = [
module.vpc.vpc_cidr_block]
}
tags = {
Name = "public_alb"
}
}
resource "aws_security_group" "public_alb_private_backend" {
name = "public_alb_private_backend"
description = "Allow healthcheck against nodes"
vpc_id = "${module.vpc.vpc_id}"
ingress {
description = "Allow healthchecking and forwarding from alb"
from_port = 0
to_port = 0
protocol = "-1"
security_groups = [
aws_security_group.public_alb.id]
}
tags = {
Name = "public_alb_private_backend"
}
}
resource "aws_alb" "alb" {
name = "existingvpc2"
security_groups = [
aws_security_group.public_alb.id,
aws_security_group.allow_http_from_me.id]
subnets = module.vpc.public_subnets
internal = false
enable_deletion_protection = false
}
resource "aws_alb_listener" "podinfo" {
port = 80
protocol = "HTTP"
load_balancer_arn = aws_alb.alb.arn
default_action {
type = "fixed-response"
fixed_response {
content_type = "text/plain"
status_code = "404"
message_body = "Nothing here"
}
}
}
resource "aws_lb_target_group" "tg1" {
name = "tg1"
port = 30080
protocol = "HTTP"
vpc_id = module.vpc.vpc_id
}
resource "aws_lb_target_group" "tg2" {
name = "tg2"
port = 30080
protocol = "HTTP"
vpc_id = module.vpc.vpc_id
}
resource "eksctl_cluster" "red" {
assume_role {
role_arn = var.role_arn
}
eksctl_bin = "eksctl"
name = "red2"
region = var.region
api_version = "eksctl.io/v1alpha5"
version = "1.16"
vpc_id = module.vpc.vpc_id
kubeconfig_path = "mykubeconfig"
spec = <<EOS
nodeGroups:
- name: ng2
instanceType: m5.large
desiredCapacity: 1
targetGroupARNs:
- ${aws_lb_target_group.tg2.arn}
securityGroups:
attachIDs:
- ${aws_security_group.public_alb_private_backend.id}
iam:
withOIDC: true
serviceAccounts:
- metadata:
name: reader2
namespace: default
labels: {aws-usage: "application"}
attachPolicyARNs:
- "arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess"
vpc:
clusterEndpoints:
privateAccess: true
publicAccess: true
cidr: "${module.vpc.vpc_cidr_block}" # (optional, must match CIDR used by the given VPC)
subnets:
# must provide 'private' and/or 'public' subnets by availibility zone as shown
private:
${module.vpc.azs[0]}:
id: "${module.vpc.private_subnets[0]}"
cidr: "${module.vpc.private_subnets_cidr_blocks[0]}" # (optional, must match CIDR used by the given subnet)
${module.vpc.azs[1]}:
id: "${module.vpc.private_subnets[1]}"
cidr: "${module.vpc.private_subnets_cidr_blocks[1]}" # (optional, must match CIDR used by the given subnet)
${module.vpc.azs[2]}:
id: "${module.vpc.private_subnets[2]}"
cidr: "${module.vpc.private_subnets_cidr_blocks[2]}" # (optional, must match CIDR used by the given subnet)
public:
${module.vpc.azs[0]}:
id: "${module.vpc.public_subnets[0]}"
cidr: "${module.vpc.public_subnets_cidr_blocks[0]}" # (optional, must match CIDR used by the given subnet)
${module.vpc.azs[1]}:
id: "${module.vpc.public_subnets[1]}"
cidr: "${module.vpc.public_subnets_cidr_blocks[1]}" # (optional, must match CIDR used by the given subnet)
${module.vpc.azs[2]}:
id: "${module.vpc.public_subnets[2]}"
cidr: "${module.vpc.public_subnets_cidr_blocks[2]}" # (optional, must match CIDR used by the given subnet)
git:
repo:
url: "git@github.com:mumoshu/gitops-demo.git"
branch: master
fluxPath: "flux/"
user: "gitops"
email: "gitops@myorg.com"
## Uncomment this when `commitOperatorManifests: true`
#privateSSHKeyPath: /path/to/your/ssh/key
operator:
commitOperatorManifests: false
namespace: "flux"
readOnly: true
EOS
depends_on = [
module.vpc]
}
resource "eksctl_nodegroup" "ng1" {
assume_role {
role_arn = var.role_arn
}
name = "ng1"
region = eksctl_cluster.red.region
cluster = eksctl_cluster.red.name
nodes_min = 1
nodes = 1
}
//
//resource "eksctl_courier_alb" "my_alb_courier" {
// listener_arn = aws_alb_listener.podinfo.arn
//
// priority = "11"
//
// step_weight = 10
// step_interval = "5s"
//
// hosts = [
// "exmaple.com"]
//
// destination {
// target_group_arn = aws_lb_target_group.tg1.arn
//
// weight = 100
// }
//
// destination {
// target_group_arn = aws_lb_target_group.tg2.arn
// weight = 0
// }
//
// depends_on = [
// eksctl_cluster.red,
//// helmfile_release_set.mystack1
// ]
//}
resource "helmfile_release_set" "mystack1" {
aws_region = var.region
aws_assume_role {
role_arn = var.role_arn
}
content = file("./helmfile.yaml")
environment = "default"
kubeconfig = eksctl_cluster.red.kubeconfig_path
depends_on = [
eksctl_cluster.red,
]
}
resource "kubectl_ensure" "meta" {
aws_region = var.region
aws_assume_role {
role_arn = var.role_arn
}
kubeconfig = eksctl_cluster.red.kubeconfig_path
namespace = "kube-system"
resource = "configmap"
name = "aws-auth"
labels = {
"key1" = "one"
"key2" = "two"
}
annotations = {
"key3" = "three"
"key4" = "four"
}
}
output "kubeconfig_path" {
value = eksctl_cluster.red.kubeconfig_path
}
output "alb_listener_arn" {
value = aws_alb_listener.podinfo.arn
}
output "vpc_id" {
value = module.vpc.vpc_id
}
output "vpc_public_subnets_ids" {
value = module.vpc.public_subnets
}
output "vpc_private_subnet_ids" {
value = module.vpc.private_subnets
}
output "vpc_public_subnet_cidr_blocks" {
value = module.vpc.public_subnets_cidr_blocks
}
output "vpc_private_subnet_cidr_blocks" {
value = module.vpc.private_subnets_cidr_blocks
}
output "vpc_subnet_azs" {
value = module.vpc.azs
}
output "vpc_cidr_block" {
value = module.vpc.vpc_cidr_block
}
output "vpc_subnet_groups" {
value = {
"public" = [
for i in range(length(module.vpc.azs)):
{
cidr = module.vpc.public_subnets_cidr_blocks[i],
az = module.vpc.azs[i],
id = module.vpc.public_subnets[i],
}
],
"private" = [
for i in range(length(module.vpc.azs)):
{
cidr = module.vpc.private_subnets_cidr_blocks[i],
az = module.vpc.azs[i],
id = module.vpc.private_subnets[i],
}
]
}
}
output "security_group_id" {
value = aws_security_group.public_alb_private_backend.id
}