Skip to content

Commit 9b88418

Browse files
Everett SmithEverett Smith
authored andcommitted
Update to support secrets
1 parent 9a8146e commit 9b88418

File tree

4 files changed

+67
-21
lines changed

4 files changed

+67
-21
lines changed

modules/aws_ecs/locals.tf

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,6 @@ locals {
6565
"name" = "POSTGRES_USER",
6666
"value" = var.rds_username
6767
},
68-
{
69-
"name" = "POSTGRES_PASSWORD",
70-
"value" = random_string.rds_password.result
71-
},
72-
{
73-
"name" : "JWT_SECRET",
74-
"value" : random_string.jwt_secret.result
75-
},
76-
{
77-
"name" : "ENCRYPTION_KEY",
78-
"value" : random_string.encryption_key.result
79-
},
8068
{
8169
"name" : "LICENSE_KEY",
8270
"value" : var.retool_license_key
@@ -116,6 +104,24 @@ locals {
116104
]
117105
)
118106

107+
secrets = concat(
108+
var.additional_secrets,
109+
[
110+
{
111+
name = "POSTGRES_PASSWORD",
112+
valueFrom = aws_secretsmanager_secret.rds_password.arn
113+
},
114+
{
115+
name = "JWT_SECRET",
116+
valueFrom = aws_secretsmanager_secret.jwt_secret.arn
117+
},
118+
{
119+
name = "ENCRYPTION_KEY",
120+
valueFrom = aws_secretsmanager_secret.encryption_key.arn
121+
}
122+
]
123+
)
124+
119125
task_log_configuration = (
120126
var.telemetry_enabled ? {
121127
# Send logs to CloudWatch in addition to telemetry service:

modules/aws_ecs/main.tf

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ resource "aws_ecs_service" "telemetry" {
211211
resource "aws_ecs_task_definition" "retool_jobs_runner" {
212212
family = "retool-jobs-runner"
213213
task_role_arn = aws_iam_role.task_role.arn
214-
execution_role_arn = var.launch_type == "FARGATE" ? aws_iam_role.execution_role[0].arn : null
214+
execution_role_arn = aws_iam_role.execution_role[0].arn
215215
requires_compatibilities = var.launch_type == "FARGATE" ? ["FARGATE"] : ["EC2"]
216216
network_mode = "awsvpc"
217217
cpu = var.launch_type == "FARGATE" ? var.fargate_task_resource_map["jobs_runner"]["cpu"] : null
@@ -249,6 +249,8 @@ resource "aws_ecs_task_definition" "retool_jobs_runner" {
249249
}
250250
]
251251
)
252+
253+
secrets = local.secrets
252254
}
253255
]
254256
))
@@ -257,7 +259,7 @@ resource "aws_ecs_task_definition" "retool_jobs_runner" {
257259
resource "aws_ecs_task_definition" "retool" {
258260
family = "retool"
259261
task_role_arn = aws_iam_role.task_role.arn
260-
execution_role_arn = var.launch_type == "FARGATE" ? aws_iam_role.execution_role[0].arn : null
262+
execution_role_arn = aws_iam_role.execution_role[0].arn
261263
requires_compatibilities = var.launch_type == "FARGATE" ? ["FARGATE"] : ["EC2"]
262264
network_mode = "awsvpc"
263265
cpu = var.launch_type == "FARGATE" ? var.fargate_task_resource_map["main"]["cpu"] : null
@@ -300,6 +302,8 @@ resource "aws_ecs_task_definition" "retool" {
300302
}
301303
]
302304
)
305+
306+
secrets = local.secrets
303307
}
304308
]
305309
))
@@ -309,7 +313,7 @@ resource "aws_ecs_task_definition" "retool_workflows_backend" {
309313
count = var.workflows_enabled ? 1 : 0
310314
family = "retool-workflows-backend"
311315
task_role_arn = aws_iam_role.task_role.arn
312-
execution_role_arn = var.launch_type == "FARGATE" ? aws_iam_role.execution_role[0].arn : null
316+
execution_role_arn = aws_iam_role.execution_role[0].arn
313317
requires_compatibilities = var.launch_type == "FARGATE" ? ["FARGATE"] : ["EC2"]
314318
network_mode = "awsvpc"
315319
cpu = var.launch_type == "FARGATE" ? var.fargate_task_resource_map["workflows_backend"]["cpu"] : null
@@ -352,6 +356,8 @@ resource "aws_ecs_task_definition" "retool_workflows_backend" {
352356
}
353357
]
354358
)
359+
360+
secrets = local.secrets
355361
}
356362
]
357363
))
@@ -361,7 +367,7 @@ resource "aws_ecs_task_definition" "retool_workflows_worker" {
361367
count = var.workflows_enabled ? 1 : 0
362368
family = "retool-workflows-worker"
363369
task_role_arn = aws_iam_role.task_role.arn
364-
execution_role_arn = var.launch_type == "FARGATE" ? aws_iam_role.execution_role[0].arn : null
370+
execution_role_arn = aws_iam_role.execution_role[0].arn
365371
requires_compatibilities = var.launch_type == "FARGATE" ? ["FARGATE"] : ["EC2"]
366372
network_mode = "awsvpc"
367373
cpu = var.launch_type == "FARGATE" ? var.fargate_task_resource_map["code_executor"]["cpu"] : null
@@ -408,6 +414,8 @@ resource "aws_ecs_task_definition" "retool_workflows_worker" {
408414
}
409415
]
410416
)
417+
418+
secrets = local.secrets
411419
}
412420
]
413421
))
@@ -417,7 +425,7 @@ resource "aws_ecs_task_definition" "retool_code_executor" {
417425
count = var.code_executor_enabled ? 1 : 0
418426
family = "retool-code-executor"
419427
task_role_arn = aws_iam_role.task_role.arn
420-
execution_role_arn = var.launch_type == "FARGATE" ? aws_iam_role.execution_role[0].arn : null
428+
execution_role_arn = aws_iam_role.execution_role[0].arn
421429
requires_compatibilities = var.launch_type == "FARGATE" ? ["FARGATE"] : ["EC2"]
422430
network_mode = "awsvpc"
423431
cpu = var.launch_type == "FARGATE" ? var.fargate_task_resource_map["telemetry"]["cpu"] : null
@@ -472,6 +480,8 @@ resource "aws_ecs_task_definition" "retool_code_executor" {
472480
}
473481
] : []
474482
)
483+
484+
secrets = local.secrets
475485
}
476486
]
477487
))
@@ -481,7 +491,7 @@ resource "aws_ecs_task_definition" "retool_telemetry" {
481491
count = var.telemetry_enabled ? 1 : 0
482492
family = "retool-telemetry"
483493
task_role_arn = aws_iam_role.task_role.arn
484-
execution_role_arn = var.launch_type == "FARGATE" ? aws_iam_role.execution_role[0].arn : null
494+
execution_role_arn = aws_iam_role.execution_role[0].arn
485495
requires_compatibilities = var.launch_type == "FARGATE" ? ["FARGATE"] : ["EC2"]
486496
network_mode = "awsvpc"
487497
cpu = var.launch_type == "FARGATE" ? var.ecs_task_resource_map["telemetry"]["cpu"] : null

modules/aws_ecs/roles.tf

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ resource "aws_iam_role" "service_role" {
6969
}
7070
}
7171

72-
# Execution Role for Fargate
7372
data "aws_iam_policy_document" "execution_role_assume_policy" {
7473
statement {
7574
actions = ["sts:AssumeRole"]
@@ -82,17 +81,42 @@ data "aws_iam_policy_document" "execution_role_assume_policy" {
8281
}
8382

8483
resource "aws_iam_role" "execution_role" {
85-
count = var.launch_type == "FARGATE" ? 1 : 0
8684
name = "${var.deployment_name}-execution-role"
8785
assume_role_policy = data.aws_iam_policy_document.execution_role_assume_policy.json
8886
}
8987

9088
resource "aws_iam_role_policy_attachment" "execution_role" {
91-
count = var.launch_type == "FARGATE" ? 1 : 0
9289
role = aws_iam_role.execution_role[0].name
9390
policy_arn = "arn:${var.iam_partition}:iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy"
9491
}
9592

93+
data "aws_iam_policy_document" "execution_role_read_secrets" {
94+
statement {
95+
effect = "Allow"
96+
97+
actions = [
98+
"secretsmanager:GetSecretValue",
99+
]
100+
101+
resources = [
102+
aws_secretsmanager_secret.rds_password.arn,
103+
aws_secretsmanager_secret.encryption_key.arn,
104+
aws_secretsmanager_secret.jwt_secret.arn
105+
]
106+
}
107+
}
108+
109+
resource aws_iam_policy "execution_role_read_secrets" {
110+
name = "ExecutionRoleReadSecrets"
111+
description = "Allows ECS or EC2 instance execution to read secrets block values from AWS Secret Manager"
112+
policy = data.aws_iam_policy_document.execution_role_read_secrets.json
113+
}
114+
115+
resource "aws_iam_role_policy_attachment" "execution_role_read_secrets" {
116+
role = aws_iam_role.execution_role[0].name
117+
policy_arn = aws_iam_policy.execution_role_read_secrets.arn
118+
}
119+
96120
# IAM Role for EC2 instances
97121
resource "aws_iam_instance_profile" "ec2" {
98122
count = var.launch_type == "EC2" ? 1 : 0

modules/aws_ecs/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,12 @@ variable "additional_env_vars" {
514514
description = "Additional environment variables (e.g. BASE_DOMAIN)"
515515
}
516516

517+
variable "additional_secrets" {
518+
type = list(map(string))
519+
default = []
520+
description = "Optional additional environment variables set from pre-existing AWS Secrets Manager Secrets."
521+
}
522+
517523
variable "additional_temporal_env_vars" {
518524
type = list(map(string))
519525
default = []

0 commit comments

Comments
 (0)