-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
293 lines (250 loc) · 7.87 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
terraform {
required_providers {
commonfate = {
source = "common-fate/commonfate"
version = ">= 2.25.3, < 3.0.0"
}
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
data "aws_caller_identity" "current" {}
data "aws_region" "current" {}
locals {
name_prefix = join("-", compact([var.namespace, var.stage, var.id]))
aws_account_id = data.aws_caller_identity.current.account_id
aws_region = data.aws_region.current.name
}
module "iam_roles" {
source = "./modules/iam_roles"
name_prefix = local.name_prefix
assume_role_external_id = var.assume_role_external_id
aws_account_id = local.aws_account_id
aws_region = local.aws_region
common_fate_aws_account_id = var.common_fate_aws_account_id
ecs_cluster_name = var.ecs_cluster_name
}
#trivy:ignore:AVD-AWS-0104
resource "aws_security_group" "ecs_proxy_sg" {
name = "${local.name_prefix}-proxy"
description = "Common Fate Proxy networking"
vpc_id = var.vpc_id
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_cloudwatch_log_group" "proxy_log_group" {
name = "${local.name_prefix}-proxy"
retention_in_days = var.log_retention_in_days
}
resource "aws_iam_role" "proxy_ecs_execution_role" {
name = "${local.name_prefix}-proxy-er"
description = "The execution role used by ECS to run the Proxy task."
assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Action = "sts:AssumeRole",
Effect = "Allow",
Principal = {
Service = "ecs-tasks.amazonaws.com"
}
Condition = {
ArnLike = {
"aws:SourceArn" = "arn:aws:ecs:${local.aws_region}:${local.aws_account_id}:*"
}
StringEquals = {
"aws:SourceAccount" : "${local.aws_account_id}"
}
}
}
]
})
}
resource "aws_iam_role_policy_attachment" "proxy_ecs_execution_role_policy_attach" {
role = aws_iam_role.proxy_ecs_execution_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
}
# TASK ROLE
resource "aws_iam_role" "proxy_ecs_task_role" {
name = "${local.name_prefix}-proxy-ecs-tr"
description = "The task role assumed by the Proxy task."
assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Action = "sts:AssumeRole",
Effect = "Allow",
Principal = {
Service = "ecs-tasks.amazonaws.com"
}
Condition = {
ArnLike = {
"aws:SourceArn" = "arn:aws:ecs:${local.aws_region}:${local.aws_account_id}:*"
}
StringEquals = {
"aws:SourceAccount" : "${local.aws_account_id}"
}
}
}
]
})
}
resource "aws_iam_policy" "tagged_assume_role_policy" {
name = "allow-assume-proxy-ecs-tr-for-tagged-roles"
description = "Policy to allow assuming roles only if they have a common-fate-aws-proxy-access-role tag"
policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Effect = "Allow"
Action = "sts:AssumeRole"
Resource = "*"
Condition = {
StringEquals = {
"aws:ResourceTag/common-fate-aws-proxy-access-role" = "true"
}
}
}
]
})
}
resource "aws_iam_role_policy_attachment" "assume_role_policy_attachment" {
role = aws_iam_role.proxy_ecs_task_role.name
policy_arn = aws_iam_policy.tagged_assume_role_policy.arn
}
resource "aws_iam_role_policy_attachment" "ecs_task_role_ssm" {
role = aws_iam_role.proxy_ecs_task_role.name
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
// TODO I think its only the execution role that needs this
resource "aws_iam_role_policy_attachment" "ecs_task_execution_role_ssm" {
role = aws_iam_role.proxy_ecs_execution_role.name
policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}
data "aws_iam_policy_document" "ssm_permissions" {
statement {
actions = ["ssmmessages:CreateControlChannel",
"ssmmessages:CreateDataChannel",
"ssmmessages:OpenControlChannel",
"ssmmessages:OpenDataChannel"]
resources = ["*"]
}
}
resource "aws_iam_policy" "ssm_permissions_role" {
name = "${local.name_prefix}-ssm-permissions-role"
policy = data.aws_iam_policy_document.ssm_permissions.json
}
resource "aws_iam_role_policy_attachment" "ecs_task_role_ssm_perms" {
role = aws_iam_role.proxy_ecs_task_role.name
policy_arn = aws_iam_policy.ssm_permissions_role.arn
}
// TODO I think its only the execution role that needs this
resource "aws_iam_role_policy_attachment" "ecs_task_execution_role_ssm_perms" {
role = aws_iam_role.proxy_ecs_execution_role.name
policy_arn = aws_iam_policy.ssm_permissions_role.arn
}
resource "aws_ecs_task_definition" "proxy_task" {
family = "${local.name_prefix}-proxy"
network_mode = "awsvpc"
requires_compatibilities = ["FARGATE"]
cpu = var.ecs_task_cpu
memory = var.ecs_task_memory
execution_role_arn = aws_iam_role.proxy_ecs_execution_role.arn
task_role_arn = aws_iam_role.proxy_ecs_task_role.arn
container_definitions = jsonencode([{
name = "aws-proxy-container",
image = "${var.proxy_image_repository}:${var.release_tag}",
portMappings = [{
containerPort = 9999,
}],
environment = [
{
name = "LOG_LEVEL"
value = var.enable_verbose_logging ? "DEBUG" : "INFO"
},
{
name = "CF_RELEASE_TAG"
value = var.release_tag
},
{
name = "CF_ACCESS_URL"
value = var.app_url
},
{
name = "CF_API_URL"
value = var.app_url
},
{
name = "CF_CLIENT_ID"
value = var.proxy_service_client_id
},
{
name = "CF_CLIENT_SECRET"
value = var.proxy_service_client_secret
},
{
name = "CF_OIDC_ISSUER"
value = var.auth_issuer
},
{
name = "CF_INTEGRATION_ID"
value = var.id
},
{
name = "CF_ECS_CLUSTER_READ_ROLE_ARN"
value = module.iam_roles.read_role_arn
},
],
logConfiguration = {
logDriver = "awslogs",
options = {
"awslogs-group" = aws_cloudwatch_log_group.proxy_log_group.name,
"awslogs-region" = local.aws_region,
"awslogs-stream-prefix" = "aws-proxy"
}
},
# Link to the security group
linuxParameters = {
securityGroupIds = [aws_security_group.ecs_proxy_sg.id]
}
},
])
}
resource "aws_ecs_service" "proxy_service" {
name = "${local.name_prefix}-proxy"
cluster = var.ecs_cluster_id
task_definition = aws_ecs_task_definition.proxy_task.arn
launch_type = "FARGATE"
desired_count = var.desired_task_count
// enables ecs exec
enable_execute_command = true
deployment_circuit_breaker {
enable = true
rollback = true
}
network_configuration {
subnets = var.subnet_ids
security_groups = [aws_security_group.ecs_proxy_sg.id]
assign_public_ip = true
}
}
resource "commonfate_ecs_proxy" "proxy" {
id = var.id
aws_account_id = local.aws_account_id
aws_region = local.aws_region
ecs_cluster_name = var.ecs_cluster_name
ecs_task_definition_family = "${local.name_prefix}-proxy"
ecs_cluster_reader_role_arn = module.iam_roles.read_role_arn
ecs_cluster_security_group_id = aws_security_group.ecs_proxy_sg.id
ecs_cluster_task_role_name = aws_iam_role.proxy_ecs_task_role.name
ecs_cluster_task_container_name = "aws-proxy-container"
}