Search code examples
amazon-web-servicesamazon-ecs

How do I integrate API with ECS to scale tasks on demand?


I am creating an API with AWS services. The architecture is very simple, in summary it is:

API Gateway > lambda function > ECS task

My question is about the best way to use ECS because I need to be able to launch many ECS tasks since these tasks are what calculate the result of the APIs.

I tried to apply auto-scaling to the tasks since it is necessary to use a task definition and the resources of this definition are shared with other tasks, there would come a time when I could not execute more tasks and would have a backlog of requests. On the other hand, service tasks could be a viable option but they do not act like standalone tasks and are not deleted shortly after the task finishes so I don't think that's a viable option either.

In general I'm a little lost with this and don't know what would be best.

Is there any way to use ECS as I have in mind or another type of service that could help me more?


Solution

  • You need to use a queue to capture all API requests and hold them until there are sufficient resources available to process the requests. That part of the setup would look like:

    API Gateway -> AWS Lambda -> SQS Queue
    

    Then you would have an ECS Service configured with auto-scaling to run tasks that process the items in the queue. Instead of each task being given a single request to process and then exiting, the tasks would instead poll the SQS Queue for requests to process, and keep running and processing requests (one by one) until the queue is empty.

    You would configure the ECS Service's auto-scaling to use the SQS Queue's ApproximateNumberOfMessagesVisible metric as the Scaling Metric. And you would set the auto-scaling's Minimum capacity value to 0 so that when the queue is empty, there will be no ECS tasks running.