-
Notifications
You must be signed in to change notification settings - Fork 0
/
brokerFargateTfModule.tf
200 lines (179 loc) · 6.32 KB
/
brokerFargateTfModule.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
locals {
broker_Client_URL = "https://${var.nameprefix}-${var.task_name}.${var.r53_zone}:${tostring(var.broker_port)}"
logGroupPath = "/ecs/${var.nameprefix}-${var.task_name}"
certificate_arn = var.certificate_arn != "" ? var.certificate_arn : data.aws_acm_certificate.domain_cert.arn
fargateHostIp = "127.0.0.1"
healthCheckSleepTime = "120"
}
resource "aws_lb" "broker_aws_alb" {
name = lower("${var.nameprefix}-${var.task_name}")
internal = true
load_balancer_type = "application"
subnets = var.FARGATE_Subnets
security_groups = [aws_security_group.ALB_SecurityGroup.id]
}
resource "aws_lb_target_group" "lb_target_group" {
name = lower("${var.nameprefix}-${var.task_name}-tg")
port = var.broker_port
protocol = "HTTP"
target_type = "ip"
vpc_id = data.aws_subnet.ecs_service_Subnet.vpc_id
health_check {
enabled = true
path = "/"
port = var.broker_port
interval = 30
unhealthy_threshold = 5
healthy_threshold = 3
timeout = 10
matcher = "200,401"
}
}
resource "aws_lb_listener" "front_end" {
load_balancer_arn = aws_lb.broker_aws_alb.arn
port = var.broker_port
protocol = "HTTPS"
ssl_policy = "ELBSecurityPolicy-TLS-1-2-2017-01"
certificate_arn = local.certificate_arn
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.lb_target_group.arn
}
}
resource "aws_route53_record" "snyk_broker_dns_record" {
zone_id = data.aws_route53_zone.r53_zone.id
name = "${var.nameprefix}-${var.task_name}"
type = "A"
#ttl = 60
alias {
name = aws_lb.broker_aws_alb.dns_name
zone_id = aws_lb.broker_aws_alb.zone_id
evaluate_target_health = true
}
}
resource "aws_cloudwatch_log_group" "snyk_broker_Log_Group" {
name = local.logGroupPath
}
resource "aws_iam_role" "ecs-task-exe-iam-role" {
name = "${var.nameprefix}-${lower(var.task_name)}-container-execution-role"
description = "${var.nameprefix}-${lower(var.task_name)} container execution role"
assume_role_policy = jsonencode(
{
"Version" : "2008-10-17",
"Statement" : [
{
"Sid" : "",
"Effect" : "Allow",
"Principal" : {
"Service" : "ecs-tasks.amazonaws.com"
},
"Action" : "sts:AssumeRole"
}
]
}
)
inline_policy {
name = "${var.nameprefix}-${lower(var.task_name)}-container-execution-policy"
policy = jsonencode(
{
"Version" : "2012-10-17",
"Statement" : [
{
"Sid" : "secretManagerPermissions",
"Effect" : "Allow",
"Action" : [
"secretsmanager:GetSecretValue",
"kms:Decrypt"
],
"Resource" : [
var.exe_task_ssm_secret,
var.registry_token_fargate_ssm_secret
]
},
{
"Sid" : "CloudWatchPermissions",
"Effect" : "Allow",
"Action" : [
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource" : [
"arn:aws:logs:*:*:log-group:${local.logGroupPath}*:log-stream:*",
"arn:aws:logs:*:*:log-group:${local.logGroupPath}*"
]
}
]
}
)
}
}
resource "aws_security_group" "ALB_SecurityGroup" {
name = "ALB_${var.nameprefix}_${var.task_name}_securityGroup"
description = "ALB_${var.nameprefix}_${var.task_name} ecs task securityGroup"
vpc_id = data.aws_subnet.ecs_service_Subnet.vpc_id
tags = {
"Name" = "ALB_${var.nameprefix}_${var.task_name}_securityGroup"
}
dynamic "ingress" {
for_each = var.ALB_SG_Ingress_Rules
content {
description = ingress.value.description
from_port = ingress.value.port
to_port = ingress.value.port
protocol = ingress.value.protocol
security_groups = ingress.value.security_groups
cidr_blocks = ingress.value.cidr_blocks
ipv6_cidr_blocks = ingress.value.ipv6_cidr_blocks
}
}
}
resource "aws_security_group" "FARGATE_SecurityGroup" {
name = "FARGATE_${var.nameprefix}_${var.task_name}_securityGroup"
description = "FARGATE_${var.nameprefix}_${var.task_name} ecs task securityGroup"
vpc_id = data.aws_subnet.ecs_service_Subnet.vpc_id
tags = {
Name = "FARGATE_${var.nameprefix}_${var.task_name}_securityGroup"
}
dynamic "ingress" {
for_each = var.fargate_SG_Ingress_Rules
content {
description = ingress.value.description
from_port = ingress.value.port
to_port = ingress.value.port
protocol = ingress.value.protocol
security_groups = ingress.value.security_groups
cidr_blocks = ingress.value.cidr_blocks
ipv6_cidr_blocks = ingress.value.ipv6_cidr_blocks
}
}
dynamic "egress" {
for_each = var.Fargate_SG_egress_rules
content {
description = egress.value.description
from_port = egress.value.port
to_port = egress.value.port
protocol = egress.value.protocol
security_groups = egress.value.security_groups
cidr_blocks = egress.value.cidr_blocks
ipv6_cidr_blocks = egress.value.cidr_blocks
}
}
}
resource "aws_security_group_rule" "ALB_2_Fargate_SG_rule_Ingress" {
type = "ingress"
description = "ALB to Fargate access on port ${var.broker_port} inbound Rule "
from_port = var.broker_port
to_port = var.broker_port
protocol = "tcp"
security_group_id = aws_security_group.FARGATE_SecurityGroup.id
source_security_group_id = aws_security_group.ALB_SecurityGroup.id
}
resource "aws_security_group_rule" "ALB_2_Fargate_SG_rule_egress" {
type = "egress"
description = "ALB to Fargate access on port ${var.broker_port} outbound Rule "
from_port = var.broker_port
to_port = var.broker_port
protocol = "tcp"
security_group_id = aws_security_group.ALB_SecurityGroup.id
source_security_group_id = aws_security_group.FARGATE_SecurityGroup.id
}