-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
411 lines (345 loc) · 11.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
data "aws_vpc" "vpc" {
id = var.vpc_id
}
data "aws_ssm_parameter" "rds" {
for_each = toset(local.ssm_parameters.rds)
name = "/rds/${each.value}"
with_decryption = true
}
################################################################################
# Postgres Security Group
################################################################################
module "postgres_security_group" {
source = "terraform-aws-modules/security-group/aws"
version = "~> 5.1.2"
name = local.rds.sg_name
description = local.rds.sg_description
vpc_id = var.vpc_id
ingress_with_cidr_blocks = [
{
from_port = 0
to_port = local.rds.port
protocol = "tcp"
cidr_blocks = data.aws_vpc.vpc.cidr_block
},
]
egress_with_cidr_blocks = [{
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = "0.0.0.0/0"
}, ]
tags = local.default_tags
}
################################################################################
# RDS Kong
################################################################################
module "kong_rds" {
source = "terraform-aws-modules/rds/aws"
version = "~> 6.7.0"
identifier = local.rds.db_identifier
engine = local.rds.engine
engine_version = local.rds.engine_version
family = local.rds.engine_family
major_engine_version = local.rds.major_engine_version
instance_class = var.rds_instance_class
storage_encrypted = local.rds.storage_encrypted
storage_type = local.rds.storage_type
allocated_storage = var.db_allocated_storage
max_allocated_storage = var.db_max_allocated_storage
multi_az = var.multi_az
manage_master_user_password = var.manage_master_user_password
db_name = local.rds.postgres_db_name
username = local.rds.postgres_username
port = local.rds.port
password = local.rds.postgres_password
backup_retention_period = var.backup_retention_period
backup_window = var.backup_window
deletion_protection = var.deletion_protection
maintenance_window = var.maintenance_window
vpc_security_group_ids = [module.postgres_security_group.security_group_id]
create_db_subnet_group = var.create_db_subnet_group
subnet_ids = var.private_subnet_ids
performance_insights_enabled = var.performance_insights_enabled
performance_insights_retention_period = var.performance_insights_retention_period
tags = merge(local.default_tags, var.rds_db_tags)
}
################################################################################
# Internal ALB Security Group
################################################################################
module "internal_alb_security_group" {
source = "terraform-aws-modules/security-group/aws"
version = "~> 5.1.2"
name = local.kong.alb_sg_name
vpc_id = var.vpc_id
ingress_with_cidr_blocks = [{
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = data.aws_vpc.vpc.cidr_block
}]
egress_with_cidr_blocks = [{
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = "0.0.0.0/0"
}, ]
tags = local.default_tags
}
################################################################################
# Public ALB Security Group
################################################################################
module "public_alb_security_group" {
source = "terraform-aws-modules/security-group/aws"
version = "~> 5.1.2"
name = local.kong.alb_sg_name
vpc_id = var.vpc_id
ingress_with_cidr_blocks = [
for port in [80, 443] :
{
protocol = "tcp"
from_port = port
to_port = port
cidr_blocks = "0.0.0.0/0"
}
]
egress_with_cidr_blocks = [{
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = "0.0.0.0/0"
}, ]
tags = local.default_tags
}
################################################################################
# ECS Task Security Group
################################################################################
module "ecs_task_security_group" {
source = "terraform-aws-modules/security-group/aws"
version = "~> 5.1.2"
name = local.kong.ecs_task_sg_name
vpc_id = var.vpc_id
ingress_with_cidr_blocks = [
{
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = data.aws_vpc.vpc.cidr_block
},
]
egress_with_cidr_blocks = [{
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = "0.0.0.0/0"
}, ]
tags = local.default_tags
}
################################################################################
# ECS Execution IAM Role
################################################################################
data "aws_iam_policy_document" "assume_role_policy" {
statement {
actions = ["sts:AssumeRole"]
effect = "Allow"
principals {
type = local.ecs.iam.principal_type
identifiers = local.ecs.iam.principal_identifiers
}
}
}
resource "aws_iam_role" "ecs_exec" {
name_prefix = local.ecs.iam.name_prefix
assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
tags = local.default_tags
}
resource "aws_iam_role_policy_attachment" "ecs_exec" {
count = length(local.ecs.iam.ecs_exec_policy_arn)
role = aws_iam_role.ecs_exec.name
policy_arn = element(local.ecs.iam.ecs_exec_policy_arn, count.index)
}
################################################################################
# ECS Kong
################################################################################
data "aws_ecs_cluster" "default" {
cluster_name = var.cluster_name
}
module "ecs_kong" {
source = "infraspecdev/ecs-deployment/aws"
version = "~> 4.0.4"
vpc_id = var.vpc_id
cluster_name = data.aws_ecs_cluster.default.cluster_name
service = {
name = local.kong.service_name
desired_count = var.desired_count_for_kong_service
force_new_deployment = var.force_new_deployment
load_balancer = [
{
target_group_arn = module.internal_alb_kong.target_groups_arns[local.kong.internal_target_group]
container_name = local.kong.name
container_port = local.kong.admin_port
},
{
target_group = local.kong.public_target_group
container_name = local.kong.name
container_port = local.kong.proxy_port
}
]
network_configuration = {
subnets = var.private_subnet_ids
security_groups = [module.ecs_task_security_group.security_group_id]
assign_public_ip = false
}
}
task_definition = {
family = local.kong.task_definition_family
network_mode = local.kong.network_mode
cpu = var.cpu_for_kong_task
memory = var.memory_for_kong_task
task_role_arn = aws_iam_role.ecs_exec.arn
execution_role_arn = aws_iam_role.ecs_exec.arn
container_definitions = [
{
name = local.kong.name
image = var.container_image
essential = true
command = local.kong.commands
portMappings = local.kong.portMappings
environment = [
for key, value in local.kong.environment : {
name = key
value = value
}
]
logConfiguration = var.log_configuration_for_kong
}
]
volume = []
}
create_capacity_provider = false
load_balancer = {
name = "${local.kong.name}-public"
internal = false
subnets_ids = var.public_subnet_ids
security_groups_ids = [module.public_alb_security_group.security_group_id]
enable_deletion_protection = false
target_groups = {
(local.kong.public_target_group) = {
name = "${local.kong.name}-public"
port = 8000
protocol = "HTTP"
target_type = "ip"
health_check = {
enabled = true
path = "/status"
port = local.kong.admin_port
matcher = 200
interval = 120
timeout = 5
healthy_threshold = 2
unhealthy_threshold = 3
}
}
}
listeners = {
kong_https = {
port = 443
protocol = "HTTPS"
certificate = local.kong.public_acm_certificate
ssl_policy = var.ssl_policy
default_action = [
{
type = "forward"
target_group = local.kong.public_target_group
conditions = [
{
field = "host-header"
values = var.kong_public_domain_name
}
]
},
]
}
}
}
create_acm = true
acm_certificates = {
(local.kong.public_acm_certificate) = {
domain_name = var.kong_public_domain_name
validation_option = {
domain_name = var.kong_public_domain_name
validation_domain = var.kong_public_domain_name
}
record_zone_id = module.kong_public_dns_record.zone_id
}
}
depends_on = [module.kong_rds]
}
################################################################################
# Internal ALB Kong
################################################################################
module "internal_alb_kong" {
source = "infraspecdev/ecs-deployment/aws//modules/alb"
version = "~> 2.0.0"
name = "${local.kong.name}-internal"
internal = true
subnets_ids = var.private_subnet_ids
security_groups_ids = [module.internal_alb_security_group.security_group_id]
enable_deletion_protection = false
target_groups = {
(local.kong.internal_target_group) = {
name = "${local.kong.name}-internal"
port = 8001
protocol = "HTTP"
target_type = "ip"
vpc_id = var.vpc_id
health_check = {
enabled = true
path = "/status"
port = local.kong.admin_port
matcher = 200
interval = 120
timeout = 5
healthy_threshold = 2
unhealthy_threshold = 3
}
}
}
listeners = {
kong_http = {
port = 80
protocol = "HTTP"
default_action = [
{
type = "forward"
target_group = local.kong.internal_target_group
conditions = [
{
field = "host-header"
values = var.kong_admin_domain_name
}
]
},
]
}
}
tags = local.default_tags
}
################################################################################
# Route53 Record For Public ALB
################################################################################
module "kong_public_dns_record" {
source = "./modules/route-53-record"
domain = var.kong_public_domain_name
alb_dns_name = module.ecs_kong.alb_dns_name
alb_zone_id = module.ecs_kong.alb_zone_id
}
################################################################################
# Route53 Record For Internal ALB
################################################################################
module "kong_internal_dns_record" {
source = "./modules/route-53-record"
domain = var.kong_admin_domain_name
alb_dns_name = module.internal_alb_kong.dns_name
alb_zone_id = module.ecs_kong.alb_zone_id
}