Skip to content

Commit 90be065

Browse files
authored
Run heavy processing tasks in the same server cluster (#4174)
1 parent b8a46f6 commit 90be065

File tree

10 files changed

+71
-154
lines changed

10 files changed

+71
-154
lines changed

backend/pycon/tasks.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,14 @@ def launch_heavy_processing_worker():
2727
if settings.ENVIRONMENT == "local":
2828
return
2929

30-
cluster_name = f"pythonit-{settings.ENVIRONMENT}-heavy-processing-worker"
30+
cluster_name = f"pythonit-{settings.ENVIRONMENT}"
3131
ecs_client = boto3.client("ecs", region_name=settings.AWS_REGION_NAME)
3232

33-
response = ecs_client.list_tasks(cluster=cluster_name, desiredStatus="RUNNING")
33+
response = ecs_client.list_tasks(
34+
cluster=cluster_name,
35+
desiredStatus="RUNNING",
36+
family=f"pythonit-{settings.ENVIRONMENT}-heavy-processing-worker",
37+
)
3438

3539
if len(response["taskArns"]) > 0:
3640
return

backend/pycon/tests/test_tasks.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,17 @@ def test_launch_heavy_processing_worker_starts_task(settings, mocker):
3939
launch_heavy_processing_worker()
4040

4141
mock_client.return_value.describe_tasks.assert_called_with(
42-
cluster="pythonit-production-heavy-processing-worker", tasks=["arn-abc"]
42+
cluster="pythonit-production", tasks=["arn-abc"]
4343
)
4444

4545
mock_client.return_value.list_tasks.assert_called_with(
46-
cluster="pythonit-production-heavy-processing-worker", desiredStatus="RUNNING"
46+
cluster="pythonit-production",
47+
desiredStatus="RUNNING",
48+
family="pythonit-production-heavy-processing-worker",
4749
)
4850

4951
mock_client.return_value.run_task.assert_called_with(
50-
cluster="pythonit-production-heavy-processing-worker",
52+
cluster="pythonit-production",
5153
taskDefinition="pythonit-production-heavy-processing-worker",
5254
count=1,
5355
networkConfiguration={

infrastructure/applications/applications.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module "pycon_backend" {
2626
security_group_id = module.cluster.security_group_id
2727
server_ip = module.cluster.server_ip
2828
logs_group_name = module.cluster.logs_group_name
29+
iam_role_arn = module.cluster.iam_role_arn
2930

3031
providers = {
3132
aws = aws

infrastructure/applications/cluster/iam.tf

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ data "aws_iam_policy_document" "server_assume_role" {
2020

2121
principals {
2222
type = "Service"
23-
identifiers = ["ec2.amazonaws.com", "ecs-tasks.amazonaws.com"]
23+
identifiers = ["ec2.amazonaws.com", "ecs-tasks.amazonaws.com", "ecs.amazonaws.com"]
2424
}
2525

2626
actions = ["sts:AssumeRole"]
@@ -35,7 +35,6 @@ data "aws_iam_policy_document" "server_role_policy" {
3535
"ses:*",
3636
"ecs:*",
3737
"ecr:*",
38-
"ec2:DescribeInstances",
3938
]
4039
resources = [
4140
"*"
@@ -78,4 +77,34 @@ data "aws_iam_policy_document" "server_role_policy" {
7877
resources = ["*"]
7978
effect = "Allow"
8079
}
80+
81+
statement {
82+
actions = [
83+
"ec2:DescribeAvailabilityZones",
84+
"ec2:DescribeInstances",
85+
"ec2:CreateVolume",
86+
"ec2:AttachVolume",
87+
"ec2:DetachVolume",
88+
"ec2:CreateTags",
89+
"ec2:DeleteVolume",
90+
"ec2:DescribeVolumes",
91+
]
92+
resources = ["*"]
93+
effect = "Allow"
94+
}
95+
96+
statement {
97+
actions = [
98+
"ssmmessages:CreateControlChannel",
99+
"ssmmessages:CreateDataChannel",
100+
"ssmmessages:OpenControlChannel",
101+
"ssmmessages:OpenDataChannel"
102+
]
103+
resources = ["*"]
104+
effect = "Allow"
105+
}
106+
}
107+
108+
output "iam_role_arn" {
109+
value = aws_iam_role.server.arn
81110
}

infrastructure/applications/cluster/security.tf

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@ resource "aws_security_group_rule" "server_rds" {
2222
security_group_id = aws_security_group.server.id
2323
}
2424

25+
resource "aws_security_group_rule" "in_redis" {
26+
type = "egress"
27+
from_port = 6379
28+
to_port = 6379
29+
protocol = "tcp"
30+
source_security_group_id = aws_security_group.server.id
31+
security_group_id = aws_security_group.server.id
32+
}
33+
34+
resource "aws_security_group_rule" "out_redis" {
35+
# needed by fargate to connect to the server with redis
36+
type = "ingress"
37+
from_port = 6379
38+
to_port = 6379
39+
protocol = "tcp"
40+
source_security_group_id = aws_security_group.server.id
41+
security_group_id = aws_security_group.server.id
42+
}
43+
2544
resource "aws_security_group_rule" "web_http" {
2645
type = "ingress"
2746
from_port = 80

infrastructure/applications/pycon_backend/variables.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ variable "cluster_id" {}
99
variable "security_group_id" {}
1010
variable "server_ip" {}
1111
variable "logs_group_name" {}
12+
variable "iam_role_arn" {}

infrastructure/applications/pycon_backend/worker.tf

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,13 @@ locals {
169169
value = jsonencode({
170170
subnets = [data.aws_subnet.public_1a.id],
171171
security_groups = [
172-
data.aws_security_group.rds.id,
173-
data.aws_security_group.lambda.id,
174-
aws_security_group.instance.id
172+
var.security_group_id
175173
],
176174
})
177175
},
178176
{
179177
name = "ECS_SERVICE_ROLE",
180-
value = aws_iam_role.ecs_service.arn
178+
value = var.iam_role_arn
181179
},
182180
{
183181
name = "AWS_SES_CONFIGURATION_SET"

infrastructure/applications/pycon_backend/worker_heavy_processing.tf

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,21 @@
1-
resource "aws_ecs_cluster" "heavy_processing_worker" {
2-
name = "pythonit-${terraform.workspace}-heavy-processing-worker"
3-
4-
setting {
5-
name = "containerInsights"
6-
value = "enabled"
7-
}
8-
}
9-
10-
resource "aws_cloudwatch_log_group" "heavy_processing_worker_logs" {
11-
name = "/ecs/pythonit-${terraform.workspace}-heavy-processing-worker"
12-
retention_in_days = 7
13-
}
14-
151
resource "aws_ecs_task_definition" "heavy_processing_worker" {
162
family = "pythonit-${terraform.workspace}-heavy-processing-worker"
173
requires_compatibilities = ["FARGATE"]
184
cpu = 4096
195
memory = 16384
206
network_mode = "awsvpc"
21-
execution_role_arn = aws_iam_role.worker.arn
22-
task_role_arn = aws_iam_role.worker.arn
7+
execution_role_arn = var.iam_role_arn
8+
task_role_arn = var.iam_role_arn
239

2410
ephemeral_storage {
2511
size_in_gib = 21
2612
}
13+
2714
runtime_platform {
2815
operating_system_family = "LINUX"
2916
cpu_architecture = "ARM64"
3017
}
18+
3119
container_definitions = jsonencode([
3220
{
3321
name = "worker"
@@ -62,9 +50,9 @@ resource "aws_ecs_task_definition" "heavy_processing_worker" {
6250
logConfiguration = {
6351
logDriver = "awslogs"
6452
options = {
65-
"awslogs-group" = aws_cloudwatch_log_group.heavy_processing_worker_logs.name
53+
"awslogs-group" = var.logs_group_name
6654
"awslogs-region" = "eu-central-1"
67-
"awslogs-stream-prefix" = "ecs"
55+
"awslogs-stream-prefix" = "heavy-processing-worker"
6856
}
6957
}
7058

infrastructure/applications/pycon_backend/worker_role.tf

Lines changed: 0 additions & 111 deletions
This file was deleted.

infrastructure/applications/pycon_backend/worker_security.tf

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)