diff --git a/app.py b/app.py index faae7e3..f1d54d1 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() @@ -38,10 +39,12 @@ ) ecs_stack.add_dependency(network_stack) +mariadb_cpu_memory = FargateCpuMemory.CPU_1024_MEM_2048 mariadb_props = ServiceProps( + mariadb_cpu_memory, "openchallenges-mariadb", 3306, - 512, + mariadb_cpu_memory.memory, f"ghcr.io/sage-bionetworks/openchallenges-mariadb:{image_version}", { "MARIADB_USER": "maria", @@ -64,10 +67,12 @@ mariadb_props, ) +elasticsearch_cpu_memory = FargateCpuMemory.CPU_1024_MEM_2048 elasticsearch_props = ServiceProps( + elasticsearch_cpu_memory, "openchallenges-elasticsearch", 9200, - 2048, + elasticsearch_cpu_memory.memory, f"ghcr.io/sage-bionetworks/openchallenges-elasticsearch:{image_version}", { "bootstrap.memory_lock": "true", @@ -84,10 +89,12 @@ elasticsearch_props, ) +thumbor_cpu_memory = FargateCpuMemory.CPU_512_MEM_1024 thumbor_props = ServiceProps( + thumbor_cpu_memory, "openchallenges-thumbor", 8889, - 512, + thumbor_cpu_memory.memory, f"ghcr.io/sage-bionetworks/openchallenges-thumbor:{image_version}", { "LOG_LEVEL": "info", @@ -121,10 +128,12 @@ ) thumbor_stack.add_dependency(bucket_stack) +config_server_cpu_memory = FargateCpuMemory.CPU_512_MEM_1024 config_server_props = ServiceProps( + 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", @@ -144,10 +153,12 @@ config_server_props, ) +service_registry_cpu_memory = FargateCpuMemory.CPU_512_MEM_1024 service_registry_props = ServiceProps( + 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", @@ -165,10 +176,12 @@ ) service_registry_stack.add_dependency(config_server_stack) +zipkin_cpu_memory = FargateCpuMemory.CPU_512_MEM_1024 zipkin_props = ServiceProps( + zipkin_cpu_memory, "openchallenges-zipkin", 9411, - 512, + zipkin_cpu_memory.memory, f"ghcr.io/sage-bionetworks/openchallenges-zipkin:{image_version}", {}, ) @@ -181,10 +194,12 @@ zipkin_props, ) +image_service_cpu_memory = FargateCpuMemory.CPU_512_MEM_1024 image_service_props = ServiceProps( + 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", @@ -207,10 +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( + 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", @@ -244,11 +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( + 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", @@ -276,10 +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( + 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", @@ -299,10 +319,12 @@ ) api_gateway_stack.add_dependency(service_registry_stack) +oc_app_cpu_memory = FargateCpuMemory.CPU_512_MEM_1024 oc_app_props = ServiceProps( + 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", @@ -345,10 +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( + 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"}, ) @@ -360,10 +384,12 @@ api_docs_props, ) +apex_cpu_memory = FargateCpuMemory.CPU_256_MEM_512 apex_service_props = ServiceProps( + 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", 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, )