A pattern I have implemented is to have my API code on both ECS/Fargate and Lambda at the same time, and send traffic to the appropriate one using an Elastic Load Balancer. I flag specific endpoints as "cpu intensive" and have them run on lambda.
Implemented by
- Duplicating all routes in the API with the "/sls/" prefix (this is a couple of lines in FastAPI)
- Setting up a rule in ELB to route to Lambda if the route starts with /sls, or to ECS otherwise.
- Set up the CPU intensive routes to automatically respond with a 307 to the same route but prefixed with /sls.
Boom, with that the system can handle bursts of CPU intensive traffic (e.g. data exports) while remaining responsive to the simple 99% of requests all on one vCPU.
And the same dockerfile, with just a tiny change, can be used both in ECS and Lambda.