Skip to content

Commit 52cacc7

Browse files
add a security group for ecs tasks (#2717)
1 parent 6ec1aa2 commit 52cacc7

File tree

6 files changed

+68
-13
lines changed

6 files changed

+68
-13
lines changed

infrastructure/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ Migrate and load data into the new database.
164164
- Networking:
165165
- VPC: owasp-nest-staging-vpc
166166
- Subnets: subnets will be auto-selected due to VPC selection.
167-
- Security group name: select all with `owasp-nest-staging-` prefix.
168-
(*Note*: temporary step, will be further improved)
167+
- Security group name: select the ECS security group (e.g. `owasp-nest-staging-ecs-sg`).
169168
- Click "Create"
170169
- The task is now running... Click on the task ID to view Logs, Status, etc.
171170
- Follow the same steps for `owasp-nest-staging-load-data` and `owasp-nest-staging-index-data`.

infrastructure/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ module "ecs" {
6161
aws_region = var.aws_region
6262
common_tags = local.common_tags
6363
container_parameters_arns = module.parameters.ssm_parameter_arns
64+
ecs_sg_id = module.security.ecs_sg_id
6465
environment = var.environment
6566
fixtures_read_only_policy_arn = module.storage.fixtures_read_only_policy_arn
6667
fixtures_s3_bucket = var.fixtures_s3_bucket
67-
lambda_sg_id = module.security.lambda_sg_id
6868
private_subnet_ids = module.networking.private_subnet_ids
6969
project_name = var.project_name
7070
}

infrastructure/modules/ecs/main.tf

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ module "sync_data_task" {
133133
private_subnet_ids = var.private_subnet_ids
134134
project_name = var.project_name
135135
schedule_expression = "cron(17 05 * * ? *)"
136-
security_group_ids = [var.lambda_sg_id]
136+
security_group_ids = [var.ecs_sg_id]
137137
task_name = "sync-data"
138138
}
139139

@@ -162,7 +162,7 @@ module "owasp_update_project_health_metrics_task" {
162162
private_subnet_ids = var.private_subnet_ids
163163
project_name = var.project_name
164164
schedule_expression = "cron(17 17 * * ? *)"
165-
security_group_ids = [var.lambda_sg_id]
165+
security_group_ids = [var.ecs_sg_id]
166166
task_name = "owasp-update-project-health-metrics"
167167
}
168168

@@ -183,7 +183,7 @@ module "owasp_update_project_health_scores_task" {
183183
private_subnet_ids = var.private_subnet_ids
184184
project_name = var.project_name
185185
schedule_expression = "cron(22 17 * * ? *)"
186-
security_group_ids = [var.lambda_sg_id]
186+
security_group_ids = [var.ecs_sg_id]
187187
task_name = "owasp-update-project-health-scores"
188188
}
189189

@@ -202,7 +202,7 @@ module "migrate_task" {
202202
memory = var.migrate_task_memory
203203
private_subnet_ids = var.private_subnet_ids
204204
project_name = var.project_name
205-
security_group_ids = [var.lambda_sg_id]
205+
security_group_ids = [var.ecs_sg_id]
206206
task_name = "migrate"
207207
}
208208

@@ -231,7 +231,7 @@ module "load_data_task" {
231231
memory = var.load_data_task_memory
232232
private_subnet_ids = var.private_subnet_ids
233233
project_name = var.project_name
234-
security_group_ids = [var.lambda_sg_id]
234+
security_group_ids = [var.ecs_sg_id]
235235
task_name = "load-data"
236236
task_role_arn = aws_iam_role.ecs_task_role.arn
237237
}
@@ -251,6 +251,6 @@ module "index_data_task" {
251251
memory = var.index_data_task_memory
252252
private_subnet_ids = var.private_subnet_ids
253253
project_name = var.project_name
254-
security_group_ids = [var.lambda_sg_id]
254+
security_group_ids = [var.ecs_sg_id]
255255
task_name = "index-data"
256256
}

infrastructure/modules/ecs/variables.tf

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ variable "container_parameters_arns" {
1515
default = {}
1616
}
1717

18+
variable "ecs_sg_id" {
19+
description = "The ID of the security group for the ECS tasks"
20+
type = string
21+
}
22+
1823
variable "environment" {
1924
description = "The environment (e.g., staging, production)"
2025
type = string
@@ -42,10 +47,7 @@ variable "index_data_task_memory" {
4247
default = "2048"
4348
}
4449

45-
variable "lambda_sg_id" {
46-
description = "The ID of the security group for the Lambda function"
47-
type = string
48-
}
50+
4951

5052
variable "load_data_task_cpu" {
5153
description = "The CPU for the load-data task"

infrastructure/modules/security/main.tf

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,23 @@ terraform {
1313
}
1414
}
1515

16+
resource "aws_security_group" "ecs" {
17+
description = "Security group for ECS tasks"
18+
name = "${var.project_name}-${var.environment}-ecs-sg"
19+
tags = merge(var.common_tags, {
20+
Name = "${var.project_name}-${var.environment}-ecs-sg"
21+
})
22+
vpc_id = var.vpc_id
23+
24+
egress {
25+
cidr_blocks = var.default_egress_cidr_blocks
26+
description = "Allow all outbound traffic"
27+
from_port = 0
28+
protocol = "-1"
29+
to_port = 0
30+
}
31+
}
32+
1633
resource "aws_security_group" "lambda" {
1734
description = "Security group for Lambda functions (Zappa)"
1835
name = "${var.project_name}-${var.environment}-lambda-sg"
@@ -90,6 +107,17 @@ resource "aws_security_group" "redis" {
90107
}
91108
}
92109

110+
resource "aws_security_group_rule" "rds_from_ecs" {
111+
count = var.create_rds_proxy ? 0 : 1
112+
description = "PostgreSQL from ECS"
113+
from_port = var.db_port
114+
protocol = "tcp"
115+
security_group_id = aws_security_group.rds.id
116+
source_security_group_id = aws_security_group.ecs.id
117+
to_port = var.db_port
118+
type = "ingress"
119+
}
120+
93121
resource "aws_security_group_rule" "rds_from_lambda" {
94122
count = var.create_rds_proxy ? 0 : 1
95123
description = "PostgreSQL from Lambda"
@@ -112,6 +140,17 @@ resource "aws_security_group_rule" "rds_from_proxy" {
112140
source_security_group_id = aws_security_group.rds_proxy[0].id
113141
}
114142

143+
resource "aws_security_group_rule" "rds_proxy_from_ecs" {
144+
count = var.create_rds_proxy ? 1 : 0
145+
type = "ingress"
146+
description = "PostgreSQL from ECS"
147+
from_port = var.db_port
148+
to_port = var.db_port
149+
protocol = "tcp"
150+
security_group_id = aws_security_group.rds_proxy[0].id
151+
source_security_group_id = aws_security_group.ecs.id
152+
}
153+
115154
resource "aws_security_group_rule" "rds_proxy_from_lambda" {
116155
count = var.create_rds_proxy ? 1 : 0
117156
type = "ingress"
@@ -122,3 +161,13 @@ resource "aws_security_group_rule" "rds_proxy_from_lambda" {
122161
security_group_id = aws_security_group.rds_proxy[0].id
123162
source_security_group_id = aws_security_group.lambda.id
124163
}
164+
165+
resource "aws_security_group_rule" "redis_from_ecs" {
166+
description = "Redis from ECS"
167+
from_port = var.redis_port
168+
protocol = "tcp"
169+
security_group_id = aws_security_group.redis.id
170+
source_security_group_id = aws_security_group.ecs.id
171+
to_port = var.redis_port
172+
type = "ingress"
173+
}

infrastructure/modules/security/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
output "ecs_sg_id" {
2+
description = "The ID of the ECS security group"
3+
value = aws_security_group.ecs.id
4+
}
5+
16
output "lambda_sg_id" {
27
description = "The ID of the Lambda security group"
38
value = aws_security_group.lambda.id

0 commit comments

Comments
 (0)