diff --git a/moto/ecs/models.py b/moto/ecs/models.py index dfd6d921d509..6c63f3eb76ba 100644 --- a/moto/ecs/models.py +++ b/moto/ecs/models.py @@ -1076,16 +1076,21 @@ def run_task( @staticmethod def _calculate_task_resource_requirements(task_definition): - resource_requirements = {"CPU": 0, "MEMORY": 0, "PORTS": [], "PORTS_UDP": []} + resource_requirements = { + "CPU": int(task_definition.cpu or "0"), + "MEMORY": int(task_definition.memory or "0"), + "PORTS": [], + "PORTS_UDP": [], + } for container_definition in task_definition.container_definitions: # cloudformation uses capitalized properties, while boto uses all lower case # CPU is optional resource_requirements["CPU"] += container_definition.get( - "cpu", container_definition.get("Cpu", 0) + "cpu", container_definition.get("Cpu", 0) or 0 ) - # either memory or memory reservation must be provided + # either memory or memory reservation must be provided if not on the task level if ( "Memory" in container_definition or "MemoryReservation" in container_definition @@ -1094,9 +1099,13 @@ def _calculate_task_resource_requirements(task_definition): "Memory", container_definition.get("MemoryReservation") ) else: - resource_requirements["MEMORY"] += container_definition.get( - "memory", container_definition.get("memoryReservation") - ) + try: + resource_requirements["MEMORY"] += container_definition.get( + "memory", container_definition["memoryReservation"] + ) + except KeyError: + if not task_definition.memory: + raise port_mapping_key = ( "PortMappings" diff --git a/moto/ecs/responses.py b/moto/ecs/responses.py index 40c1f86e3e2f..1114cc322f8c 100644 --- a/moto/ecs/responses.py +++ b/moto/ecs/responses.py @@ -87,6 +87,8 @@ def delete_cluster(self): def register_task_definition(self): family = self._get_param("family") container_definitions = self._get_param("containerDefinitions") + cpu = self._get_param("cpu") + memory = self._get_param("memory") volumes = self._get_param("volumes") tags = self._get_param("tags") network_mode = self._get_param("networkMode")