-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
419 lines (368 loc) · 13.6 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
# A Terraform file that creates the AWS resources needed to deploy Hubs Community Edition.
terraform {
required_version = "1.7.0"
required_providers {
kubectl = {
source = "alekc/kubectl"
version = ">= 2.0.2"
}
helm = {
source = "hashicorp/helm"
version = "2.12.1"
}
kubernetes = {
source = "hashicorp/kubernetes"
version = "2.25.2"
}
}
}
provider "aws" {
region = var.REGION
}
provider "helm" {
kubernetes {
host = module.eks.cluster_endpoint
cluster_ca_certificate = base64decode(module.eks.cluster_certificate_authority_data)
exec {
api_version = "client.authentication.k8s.io/v1beta1"
args = ["eks", "get-token", "--cluster-name", module.eks.cluster_name]
command = "aws"
}
}
}
# VPC
resource "aws_vpc" "hubs_ce_vpc" {
cidr_block = "10.0.0.0/16"
instance_tenancy = "default"
tags = {
Name = "${var.SERVICE_NAME_TAG}-vpc"
ServiceName = var.SERVICE_NAME_TAG
EnvName = var.ENV_NAME_TAG
"kubernetes.io/cluster/${var.SERVICE_NAME_TAG}" = "shared"
}
}
# 2 Public Subnet
resource "aws_subnet" "hubs_ce_public_subnet1" {
vpc_id = aws_vpc.hubs_ce_vpc.id
cidr_block = "10.0.1.0/24"
availability_zone = "${var.REGION}a"
map_public_ip_on_launch = true # 各サブネットに自動でIPが振られるようにする
tags = {
Name = "${var.SERVICE_NAME_TAG}-public-subnet1"
ServiceName = var.SERVICE_NAME_TAG
EnvName = var.ENV_NAME_TAG
"kubernetes.io/cluster/${var.SERVICE_NAME_TAG}" = "shared"
"kubernetes.io/role/elb" = 1
}
}
resource "aws_subnet" "hubs_ce_public_subnet2" {
vpc_id = aws_vpc.hubs_ce_vpc.id
cidr_block = "10.0.2.0/24"
availability_zone = "${var.REGION}c"
map_public_ip_on_launch = true
tags = {
Name = "${var.SERVICE_NAME_TAG}-public-subnet2"
ServiceName = var.SERVICE_NAME_TAG
EnvName = var.ENV_NAME_TAG
"kubernetes.io/cluster/${var.SERVICE_NAME_TAG}" = "shared"
"kubernetes.io/role/elb" = 1
}
}
# 2 Private Subnet
resource "aws_subnet" "hubs_ce_private_subnet1" {
vpc_id = aws_vpc.hubs_ce_vpc.id
cidr_block = "10.0.3.0/24"
availability_zone = "${var.REGION}a"
tags = {
Name = "${var.SERVICE_NAME_TAG}-private-subnet1"
ServiceName = var.SERVICE_NAME_TAG
EnvName = var.ENV_NAME_TAG
}
}
resource "aws_subnet" "hubs_ce_private_subnet2" {
vpc_id = aws_vpc.hubs_ce_vpc.id
cidr_block = "10.0.4.0/24"
availability_zone = "${var.REGION}c"
tags = {
Name = "${var.SERVICE_NAME_TAG}-private-subnet2"
ServiceName = var.SERVICE_NAME_TAG
EnvName = var.ENV_NAME_TAG
}
}
# Internet Gateway
resource "aws_internet_gateway" "hubs_ce_igw" {
vpc_id = aws_vpc.hubs_ce_vpc.id
tags = {
Name = "${var.SERVICE_NAME_TAG}-igw"
ServiceName = var.SERVICE_NAME_TAG
EnvName = var.ENV_NAME_TAG
}
}
# Route Table
resource "aws_route_table" "hubs_ce_public_route_table" {
vpc_id = aws_vpc.hubs_ce_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.hubs_ce_igw.id
}
tags = {
Name = "${var.SERVICE_NAME_TAG}-public-route-table"
ServiceName = var.SERVICE_NAME_TAG
EnvName = var.ENV_NAME_TAG
}
}
# Route Table Association
resource "aws_route_table_association" "hubs_ce_public_route_table_association1" {
subnet_id = aws_subnet.hubs_ce_public_subnet1.id
route_table_id = aws_route_table.hubs_ce_public_route_table.id
}
resource "aws_route_table_association" "hubs_ce_public_route_table_association2" {
subnet_id = aws_subnet.hubs_ce_public_subnet2.id
route_table_id = aws_route_table.hubs_ce_public_route_table.id
}
# EKS Cluster
module "eks" {
source = "terraform-aws-modules/eks/aws"
version = "19.15.3"
cluster_name = var.SERVICE_NAME_TAG
cluster_version = "1.27"
vpc_id = aws_vpc.hubs_ce_vpc.id
subnet_ids = [aws_subnet.hubs_ce_public_subnet1.id, aws_subnet.hubs_ce_public_subnet2.id]
cluster_endpoint_public_access = true
eks_managed_node_groups = {
one = {
name = "node-group-1"
instance_types = [var.NODE_GROUPS_INSTANCE_TYPE]
min_size = var.NODE_GROUPS_MIN_SIZE
max_size = var.NODE_GROUPS_MAX_SIZE
desired_size = var.NODE_GROUPS_DESIRED_CAPACITY
iam_role_additional_policies = {
AmazonEFSCSIDriverPolicy = "arn:aws:iam::aws:policy/service-role/AmazonEFSCSIDriverPolicy"
}
}
}
cluster_addons = {
coredns = {
enabled = true
}
}
tags = {
ServiceName = var.SERVICE_NAME_TAG
EnvName = var.ENV_NAME_TAG
}
}
# Update kubeconfig
resource "null_resource" "update_kubeconfig" {
triggers = {
cluster_name = module.eks.cluster_name
}
provisioner "local-exec" {
command = "aws eks update-kubeconfig --name ${module.eks.cluster_name} --region ${var.REGION}"
}
depends_on = [module.eks]
}
# HACK: EKS module does not detect changes in desired_capacity, so use null_resource to change desired_capacity
# Detail: https://github.com/bryantbiggs/eks-desired-size-hack
resource "null_resource" "update_desired_size" {
triggers = {
desired_size = var.NODE_GROUPS_DESIRED_CAPACITY
}
provisioner "local-exec" {
interpreter = ["/bin/bash", "-c"]
# Note: this requires the awscli to be installed locally where Terraform is executed
command = <<-EOT
aws eks update-nodegroup-config \
--cluster-name ${module.eks.cluster_name} \
--nodegroup-name ${element(split(":", module.eks.eks_managed_node_groups["one"].node_group_id), 1)} \
--scaling-config desiredSize=${var.NODE_GROUPS_DESIRED_CAPACITY} \
--region ${var.REGION}
EOT
}
depends_on = [module.eks, null_resource.update_kubeconfig]
}
# Security Group Ingress Rules For EKS nodegroup
resource "aws_vpc_security_group_ingress_rule" "hubs_ce_eks_node_security_group_tcp_4443" {
security_group_id = module.eks.node_security_group_id
ip_protocol = "tcp"
from_port = 4443
to_port = 4443
cidr_ipv4 = "0.0.0.0/0"
}
resource "aws_vpc_security_group_ingress_rule" "hubs_ce_eks_node_security_group_tcp_5349" {
security_group_id = module.eks.node_security_group_id
ip_protocol = "tcp"
from_port = 5349
to_port = 5349
cidr_ipv4 = "0.0.0.0/0"
}
resource "aws_vpc_security_group_ingress_rule" "hubs_ce_eks_node_security_group_udp_35000_60000" {
security_group_id = module.eks.node_security_group_id
ip_protocol = "udp"
from_port = 35000
to_port = 60000
cidr_ipv4 = "0.0.0.0/0"
}
resource "aws_vpc_security_group_ingress_rule" "hubs_ce_eks_node_security_group_tcp_443" {
security_group_id = module.eks.node_security_group_id
ip_protocol = "tcp"
from_port = 443
to_port = 443
cidr_ipv4 = "0.0.0.0/0"
}
resource "aws_vpc_security_group_ingress_rule" "hubs_ce_eks_node_security_group_tcp_80" {
security_group_id = module.eks.node_security_group_id
ip_protocol = "tcp"
from_port = 80
to_port = 80
cidr_ipv4 = "0.0.0.0/0"
}
# Subnet Group For RDS
resource "aws_db_subnet_group" "hubs_ce_db_subnet_group" {
name = "${var.SERVICE_NAME_TAG}-db-subnet-group"
subnet_ids = [aws_subnet.hubs_ce_private_subnet1.id, aws_subnet.hubs_ce_private_subnet2.id]
tags = {
Name = "${var.SERVICE_NAME_TAG}-db-subnet-group"
ServiceName = var.SERVICE_NAME_TAG
EnvName = var.ENV_NAME_TAG
}
}
# Security Group For RDS
resource "aws_security_group" "hubs_ce_db_security_group" {
name = "${var.SERVICE_NAME_TAG}-db-security-group"
description = "Security group for RDS"
vpc_id = aws_vpc.hubs_ce_vpc.id
tags = {
Name = "${var.SERVICE_NAME_TAG}-db-security-group"
ServiceName = var.SERVICE_NAME_TAG
EnvName = var.ENV_NAME_TAG
}
}
# Allow access to DB from EKS node
resource "aws_vpc_security_group_ingress_rule" "hubs_ce_db_security_group_tcp_5432" {
security_group_id = aws_security_group.hubs_ce_db_security_group.id
ip_protocol = "tcp"
from_port = 5432
to_port = 5432
referenced_security_group_id = module.eks.node_security_group_id
}
# Allow access to EKS node from DB
resource "aws_vpc_security_group_ingress_rule" "hubs_ce_eks_node_security_group_tcp_5432" {
security_group_id = module.eks.node_security_group_id
ip_protocol = "tcp"
from_port = 5432
to_port = 5432
referenced_security_group_id = aws_security_group.hubs_ce_db_security_group.id
}
# RDS Cluster
resource "aws_rds_cluster" "hubs_ce_db" {
cluster_identifier = "${var.SERVICE_NAME_TAG}-database-1"
engine = "aurora-postgresql"
engine_version = var.DB_ENGINE_VERSION
database_name = "retdb"
master_username = "postgres"
master_password = var.DB_MASTER_PASSWORD
backup_retention_period = var.DB_BACKUP_RETENTION_PERIOD
db_subnet_group_name = aws_db_subnet_group.hubs_ce_db_subnet_group.name
vpc_security_group_ids = [aws_security_group.hubs_ce_db_security_group.id]
final_snapshot_identifier = false # Set to true if the last snapshot is taken on deletion
skip_final_snapshot = true # Set to false if the last snapshot is taken on deletion
# final_snapshot_identifier = "${var.SERVICE_NAME_TAG}-database-final-snapshot-${timestamp()}" # Uncomment out if you want to take a final snapshot on deletion
tags = {
Name = "${var.SERVICE_NAME_TAG}-database"
ServiceName = var.SERVICE_NAME_TAG
EnvName = var.ENV_NAME_TAG
}
}
# RDS Instance
resource "aws_rds_cluster_instance" "hubs_ce_db_instance" {
identifier = "${var.SERVICE_NAME_TAG}-database-instance-1"
cluster_identifier = aws_rds_cluster.hubs_ce_db.id
instance_class = var.DB_INSTANCE_CLASS
engine = "aurora-postgresql"
engine_version = var.DB_ENGINE_VERSION
db_subnet_group_name = aws_db_subnet_group.hubs_ce_db_subnet_group.name
tags = {
Name = "${var.SERVICE_NAME_TAG}-database-instance"
ServiceName = var.SERVICE_NAME_TAG
EnvName = var.ENV_NAME_TAG
}
}
# EFS
resource "aws_efs_file_system" "hubs_ce_efs" {
creation_token = "${var.SERVICE_NAME_TAG}-efs"
tags = {
Name = "${var.SERVICE_NAME_TAG}-efs"
ServiceName = var.SERVICE_NAME_TAG
EnvName = var.ENV_NAME_TAG
}
}
# Security Group for EFS
resource "aws_security_group" "hubs_ce_efs_security_group" {
name = "${var.SERVICE_NAME_TAG}-efs-security-group"
description = "Security group for EFS"
vpc_id = aws_vpc.hubs_ce_vpc.id
tags = {
Name = "${var.SERVICE_NAME_TAG}-efs-security-group"
ServiceName = var.SERVICE_NAME_TAG
EnvName = var.ENV_NAME_TAG
}
}
# Allow all traffic access from EKS nodes to EFS
resource "aws_vpc_security_group_ingress_rule" "hubs_ce_efs_security_group_all" {
security_group_id = aws_security_group.hubs_ce_efs_security_group.id
ip_protocol = "-1"
referenced_security_group_id = module.eks.node_security_group_id
}
# Create mount targets for EFS
resource "aws_efs_mount_target" "hubs_ce_efs_mount_target1" {
file_system_id = aws_efs_file_system.hubs_ce_efs.id
subnet_id = aws_subnet.hubs_ce_public_subnet1.id
security_groups = [aws_security_group.hubs_ce_efs_security_group.id]
}
resource "aws_efs_mount_target" "hubs_ce_efs_mount_target2" {
file_system_id = aws_efs_file_system.hubs_ce_efs.id
subnet_id = aws_subnet.hubs_ce_public_subnet2.id
security_groups = [aws_security_group.hubs_ce_efs_security_group.id]
}
# EFS CSI Driver for EKS
# https://registry.terraform.io/modules/terraform-aws-modules/eks/aws/latest
# https://github.com/terraform-aws-modules/terraform-aws-iam/blob/master/examples/iam-role-for-service-accounts-eks/main.tf
# https://registry.terraform.io/modules/terraform-aws-modules/iam/aws/latest/submodules/iam-role-for-service-accounts-eks
module "efs_csi_irsa_role" {
source = "terraform-aws-modules/iam/aws//modules/iam-role-for-service-accounts-eks"
role_name = "efs-csi-irsa-role"
attach_efs_csi_policy = true
oidc_providers = {
ex = {
provider_arn = module.eks.oidc_provider_arn
namespace_service_accounts = ["kube-system:efs-csi-controller-sa"]
}
}
tags = {
Name = "${var.SERVICE_NAME_TAG}-efs-csi-irsa-role"
ServiceName = var.SERVICE_NAME_TAG
EnvName = var.ENV_NAME_TAG
}
}
resource "helm_release" "aws_efs_csi_driver" {
chart = "aws-efs-csi-driver"
name = "aws-efs-csi-driver"
namespace = "kube-system"
repository = "https://kubernetes-sigs.github.io/aws-efs-csi-driver/"
set {
name = "image.repository"
value = "602401143452.dkr.ecr.eu-west-3.amazonaws.com/eks/aws-efs-csi-driver"
}
set {
name = "controller.serviceAccount.create"
value = true
}
set {
name = "controller.serviceAccount.annotations.eks\\.amazonaws\\.com/role-arn"
value = module.efs_csi_irsa_role.iam_role_arn
}
set {
name = "controller.serviceAccount.name"
value = "efs-csi-controller-sa"
}
}