From 901aad6268d0fb4b44382494464c304472e22617 Mon Sep 17 00:00:00 2001 From: tschaffter Date: Fri, 13 Dec 2024 04:50:27 +0000 Subject: [PATCH 1/2] Parametrize task cpu and memory --- app.py | 24 ++++++++-- openchallenges/fargate_cpu_memory.py | 71 ++++++++++++++++++++++++++++ openchallenges/service_props.py | 3 ++ openchallenges/service_stack.py | 4 +- 4 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 openchallenges/fargate_cpu_memory.py diff --git a/app.py b/app.py index faae7e3..015d4c4 100644 --- a/app.py +++ b/app.py @@ -2,14 +2,15 @@ from aws_cdk.aws_scheduler_alpha import ScheduleExpression from openchallenges.bucket_stack import BucketStack -from openchallenges.network_stack import NetworkStack +from openchallenges.data_integration_props import DataIntegrationProps +from openchallenges.data_integration_stack import DataIntegrationStack from openchallenges.ecs_stack import EcsStack -from openchallenges.service_stack import ServiceStack -from openchallenges.service_stack import LoadBalancedServiceStack +from openchallenges.fargate_cpu_memory import FargateCpuMemory from openchallenges.load_balancer_stack import LoadBalancerStack +from openchallenges.network_stack import NetworkStack from openchallenges.service_props import ServiceProps, ContainerVolume -from openchallenges.data_integration_stack import DataIntegrationStack -from openchallenges.data_integration_props import DataIntegrationProps +from openchallenges.service_stack import LoadBalancedServiceStack +from openchallenges.service_stack import ServiceStack import openchallenges.utils as utils app = cdk.App() @@ -39,6 +40,7 @@ ecs_stack.add_dependency(network_stack) mariadb_props = ServiceProps( + FargateCpuMemory.CPU_1024_MEM_2048, "openchallenges-mariadb", 3306, 512, @@ -65,6 +67,7 @@ ) elasticsearch_props = ServiceProps( + FargateCpuMemory.CPU_1024_MEM_2048, "openchallenges-elasticsearch", 9200, 2048, @@ -85,6 +88,7 @@ ) thumbor_props = ServiceProps( + FargateCpuMemory.CPU_1024_MEM_2048, "openchallenges-thumbor", 8889, 512, @@ -122,6 +126,7 @@ thumbor_stack.add_dependency(bucket_stack) config_server_props = ServiceProps( + FargateCpuMemory.CPU_1024_MEM_2048, "openchallenges-config-server", 8090, 1024, @@ -145,6 +150,7 @@ ) service_registry_props = ServiceProps( + FargateCpuMemory.CPU_1024_MEM_2048, "openchallenges-service-registry", 8081, 1024, @@ -166,6 +172,7 @@ service_registry_stack.add_dependency(config_server_stack) zipkin_props = ServiceProps( + FargateCpuMemory.CPU_1024_MEM_2048, "openchallenges-zipkin", 9411, 512, @@ -182,6 +189,7 @@ ) image_service_props = ServiceProps( + FargateCpuMemory.CPU_1024_MEM_2048, "openchallenges-image-service", 8086, 1024, @@ -208,6 +216,7 @@ image_service_stack.add_dependency(zipkin_stack) challenge_service_props = ServiceProps( + FargateCpuMemory.CPU_1024_MEM_2048, "openchallenges-challenge-service", 8085, 1024, @@ -246,6 +255,7 @@ organization_service_props = ServiceProps( + FargateCpuMemory.CPU_1024_MEM_2048, "openchallenges-organization-service", 8084, 1024, @@ -277,6 +287,7 @@ organization_service_stack.add_dependency(zipkin_stack) api_gateway_props = ServiceProps( + FargateCpuMemory.CPU_1024_MEM_2048, "openchallenges-api-gateway", 8082, 1024, @@ -300,6 +311,7 @@ api_gateway_stack.add_dependency(service_registry_stack) oc_app_props = ServiceProps( + FargateCpuMemory.CPU_1024_MEM_2048, "openchallenges-app", 4200, 1024, @@ -346,6 +358,7 @@ ) api_docs_props = ServiceProps( + FargateCpuMemory.CPU_256_MEM_512, "openchallenges-api-docs", 8010, 256, @@ -361,6 +374,7 @@ ) apex_service_props = ServiceProps( + FargateCpuMemory.CPU_256_MEM_512, "openchallenges-apex", 8000, 200, diff --git a/openchallenges/fargate_cpu_memory.py b/openchallenges/fargate_cpu_memory.py new file mode 100644 index 0000000..f737cbb --- /dev/null +++ b/openchallenges/fargate_cpu_memory.py @@ -0,0 +1,71 @@ +from enum import Enum + + +class FargateCpuMemory(Enum): + CPU_256_MEM_512 = (256, 512) + CPU_256_MEM_1024 = (256, 1024) + CPU_256_MEM_2048 = (256, 2048) + CPU_512_MEM_1024 = (512, 1024) + CPU_512_MEM_2048 = (512, 2048) + CPU_512_MEM_3072 = (512, 3072) + CPU_512_MEM_4096 = (512, 4096) + CPU_1024_MEM_2048 = (1024, 2048) + CPU_1024_MEM_3072 = (1024, 3072) + CPU_1024_MEM_4096 = (1024, 4096) + CPU_1024_MEM_5120 = (1024, 5120) + CPU_1024_MEM_6144 = (1024, 6144) + CPU_1024_MEM_7168 = (1024, 7168) + CPU_1024_MEM_8192 = (1024, 8192) + CPU_2048_MEM_4096 = (2048, 4096) + CPU_2048_MEM_5120 = (2048, 5120) + CPU_2048_MEM_6144 = (2048, 6144) + CPU_2048_MEM_7168 = (2048, 7168) + CPU_2048_MEM_8192 = (2048, 8192) + CPU_2048_MEM_9216 = (2048, 9216) + CPU_2048_MEM_10240 = (2048, 10240) + CPU_2048_MEM_11264 = (2048, 11264) + CPU_2048_MEM_12288 = (2048, 12288) + CPU_2048_MEM_13312 = (2048, 13312) + CPU_2048_MEM_14336 = (2048, 14336) + CPU_2048_MEM_15360 = (2048, 15360) + CPU_2048_MEM_16384 = (2048, 16384) + CPU_4096_MEM_8192 = (4096, 8192) + CPU_4096_MEM_9216 = (4096, 9216) + CPU_4096_MEM_10240 = (4096, 10240) + CPU_4096_MEM_11264 = (4096, 11264) + CPU_4096_MEM_12288 = (4096, 12288) + CPU_4096_MEM_13312 = (4096, 13312) + CPU_4096_MEM_14336 = (4096, 14336) + CPU_4096_MEM_15360 = (4096, 15360) + CPU_4096_MEM_16384 = (4096, 16384) + CPU_4096_MEM_30720 = (4096, 30720) + CPU_8192_MEM_16384 = (8192, 16384) + CPU_8192_MEM_20480 = (8192, 20480) + CPU_8192_MEM_24576 = (8192, 24576) + CPU_8192_MEM_28672 = (8192, 28672) + CPU_8192_MEM_61440 = (8192, 61440) + CPU_16384_MEM_32768 = (16384, 32768) + CPU_16384_MEM_40960 = (16384, 40960) + CPU_16384_MEM_49152 = (16384, 49152) + CPU_16384_MEM_57344 = (16384, 57344) + CPU_16384_MEM_122880 = (16384, 122880) + + @property + def cpu(self) -> int: + """Returns the CPU value of the pair.""" + return self.value[0] + + @property + def memory(self) -> int: + """Returns the memory value of the pair.""" + return self.value[1] + + @staticmethod + def valid_combinations() -> list: + """Returns all valid CPU and memory combinations.""" + return [(item.cpu, item.memory) for item in FargateCpuMemory] + + @staticmethod + def is_valid(cpu: int, memory: int) -> bool: + """Checks if a CPU and memory pair is valid.""" + return (cpu, memory) in FargateCpuMemory.valid_combinations() diff --git a/openchallenges/service_props.py b/openchallenges/service_props.py index 25b16c9..fddfa8c 100644 --- a/openchallenges/service_props.py +++ b/openchallenges/service_props.py @@ -1,5 +1,6 @@ from dataclasses import dataclass from typing import List +from openchallenges.fargate_cpu_memory import FargateCpuMemory CONTAINER_LOCATION_PATH_ID = "path://" @@ -37,6 +38,7 @@ class ServiceProps: def __init__( self, + task_cpu_memory: FargateCpuMemory, container_name: str, container_port: int, container_memory: int, @@ -44,6 +46,7 @@ def __init__( container_env_vars: dict, container_volumes: List[ContainerVolume] = None, ) -> None: + self.task_cpu_memory = task_cpu_memory self.container_name = container_name self.container_port = container_port self.container_memory = container_memory diff --git a/openchallenges/service_stack.py b/openchallenges/service_stack.py index e1be848..559a284 100644 --- a/openchallenges/service_stack.py +++ b/openchallenges/service_stack.py @@ -86,8 +86,8 @@ def __init__( self.task_definition = ecs.FargateTaskDefinition( self, "TaskDef", - cpu=1024, - memory_limit_mib=4096, + cpu=props.task_cpu_memory.cpu, + memory_limit_mib=props.task_cpu_memory.memory, task_role=task_role, execution_role=execution_role, ) From c035828d02622f4ad9a5f9bf6886d6a6e4060980 Mon Sep 17 00:00:00 2001 From: tschaffter Date: Fri, 13 Dec 2024 05:00:30 +0000 Subject: [PATCH 2/2] Specify the container memory --- app.py | 66 ++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 27 deletions(-) diff --git a/app.py b/app.py index 015d4c4..f1d54d1 100644 --- a/app.py +++ b/app.py @@ -39,11 +39,12 @@ ) ecs_stack.add_dependency(network_stack) +mariadb_cpu_memory = FargateCpuMemory.CPU_1024_MEM_2048 mariadb_props = ServiceProps( - FargateCpuMemory.CPU_1024_MEM_2048, + mariadb_cpu_memory, "openchallenges-mariadb", 3306, - 512, + mariadb_cpu_memory.memory, f"ghcr.io/sage-bionetworks/openchallenges-mariadb:{image_version}", { "MARIADB_USER": "maria", @@ -66,11 +67,12 @@ mariadb_props, ) +elasticsearch_cpu_memory = FargateCpuMemory.CPU_1024_MEM_2048 elasticsearch_props = ServiceProps( - FargateCpuMemory.CPU_1024_MEM_2048, + elasticsearch_cpu_memory, "openchallenges-elasticsearch", 9200, - 2048, + elasticsearch_cpu_memory.memory, f"ghcr.io/sage-bionetworks/openchallenges-elasticsearch:{image_version}", { "bootstrap.memory_lock": "true", @@ -87,11 +89,12 @@ elasticsearch_props, ) +thumbor_cpu_memory = FargateCpuMemory.CPU_512_MEM_1024 thumbor_props = ServiceProps( - FargateCpuMemory.CPU_1024_MEM_2048, + thumbor_cpu_memory, "openchallenges-thumbor", 8889, - 512, + thumbor_cpu_memory.memory, f"ghcr.io/sage-bionetworks/openchallenges-thumbor:{image_version}", { "LOG_LEVEL": "info", @@ -125,11 +128,12 @@ ) thumbor_stack.add_dependency(bucket_stack) +config_server_cpu_memory = FargateCpuMemory.CPU_512_MEM_1024 config_server_props = ServiceProps( - FargateCpuMemory.CPU_1024_MEM_2048, + config_server_cpu_memory, "openchallenges-config-server", 8090, - 1024, + config_server_cpu_memory.memory, f"ghcr.io/sage-bionetworks/openchallenges-config-server:{image_version}", { "GIT_DEFAULT_LABEL": "test-2", @@ -149,11 +153,12 @@ config_server_props, ) +service_registry_cpu_memory = FargateCpuMemory.CPU_512_MEM_1024 service_registry_props = ServiceProps( - FargateCpuMemory.CPU_1024_MEM_2048, + service_registry_cpu_memory, "openchallenges-service-registry", 8081, - 1024, + service_registry_cpu_memory.memory, f"ghcr.io/sage-bionetworks/openchallenges-service-registry:{image_version}", { "SERVER_PORT": "8081", @@ -171,11 +176,12 @@ ) service_registry_stack.add_dependency(config_server_stack) +zipkin_cpu_memory = FargateCpuMemory.CPU_512_MEM_1024 zipkin_props = ServiceProps( - FargateCpuMemory.CPU_1024_MEM_2048, + zipkin_cpu_memory, "openchallenges-zipkin", 9411, - 512, + zipkin_cpu_memory.memory, f"ghcr.io/sage-bionetworks/openchallenges-zipkin:{image_version}", {}, ) @@ -188,11 +194,12 @@ zipkin_props, ) +image_service_cpu_memory = FargateCpuMemory.CPU_512_MEM_1024 image_service_props = ServiceProps( - FargateCpuMemory.CPU_1024_MEM_2048, + image_service_cpu_memory, "openchallenges-image-service", 8086, - 1024, + image_service_cpu_memory.memory, f"ghcr.io/sage-bionetworks/openchallenges-image-service:{image_version}", { "SERVER_PORT": "8086", @@ -215,11 +222,12 @@ image_service_stack.add_dependency(thumbor_stack) image_service_stack.add_dependency(zipkin_stack) +challenge_service_cpu_memory = FargateCpuMemory.CPU_1024_MEM_2048 challenge_service_props = ServiceProps( - FargateCpuMemory.CPU_1024_MEM_2048, + challenge_service_cpu_memory, "openchallenges-challenge-service", 8085, - 1024, + challenge_service_cpu_memory.memory, f"ghcr.io/sage-bionetworks/openchallenges-challenge-service:{image_version}", { "SERVER_PORT": "8085", @@ -253,12 +261,12 @@ challenge_service_stack.add_dependency(elasticsearch_stack) challenge_service_stack.add_dependency(zipkin_stack) - +organization_service_cpu_memory = FargateCpuMemory.CPU_1024_MEM_2048 organization_service_props = ServiceProps( - FargateCpuMemory.CPU_1024_MEM_2048, + organization_service_cpu_memory, "openchallenges-organization-service", 8084, - 1024, + organization_service_cpu_memory.memory, f"ghcr.io/sage-bionetworks/openchallenges-organization-service:{image_version}", { "SERVER_PORT": "8084", @@ -286,11 +294,12 @@ organization_service_stack.add_dependency(elasticsearch_stack) organization_service_stack.add_dependency(zipkin_stack) +api_gateway_cpu_memory = FargateCpuMemory.CPU_512_MEM_1024 api_gateway_props = ServiceProps( - FargateCpuMemory.CPU_1024_MEM_2048, + api_gateway_cpu_memory, "openchallenges-api-gateway", 8082, - 1024, + api_gateway_cpu_memory.memory, f"ghcr.io/sage-bionetworks/openchallenges-api-gateway:{image_version}", { "SERVER_PORT": "8082", @@ -310,11 +319,12 @@ ) api_gateway_stack.add_dependency(service_registry_stack) +oc_app_cpu_memory = FargateCpuMemory.CPU_512_MEM_1024 oc_app_props = ServiceProps( - FargateCpuMemory.CPU_1024_MEM_2048, + oc_app_cpu_memory, "openchallenges-app", 4200, - 1024, + oc_app_cpu_memory.memory, f"ghcr.io/sage-bionetworks/openchallenges-app:{image_version}", { "API_DOCS_URL": f"https://{fully_qualified_domain_name}/api-docs", @@ -357,11 +367,12 @@ app, f"{stack_name_prefix}-data-integration", data_integration_props ) +api_docs_cpu_memory = FargateCpuMemory.CPU_256_MEM_512 api_docs_props = ServiceProps( - FargateCpuMemory.CPU_256_MEM_512, + api_docs_cpu_memory, "openchallenges-api-docs", 8010, - 256, + api_docs_cpu_memory.memory, f"ghcr.io/sage-bionetworks/openchallenges-api-docs:{image_version}", {"PORT": "8010"}, ) @@ -373,11 +384,12 @@ api_docs_props, ) +apex_cpu_memory = FargateCpuMemory.CPU_256_MEM_512 apex_service_props = ServiceProps( - FargateCpuMemory.CPU_256_MEM_512, + apex_cpu_memory, "openchallenges-apex", 8000, - 200, + apex_cpu_memory.memory, f"ghcr.io/sage-bionetworks/openchallenges-apex:{image_version}", { "API_DOCS_HOST": "openchallenges-api-docs",