Search code examples
pythonamazon-web-servicesboto3amazon-sqsaws-fargate

Trigger task on ECS Fargate based on SQS Queue


For my application, I'm trying to use ECS Fargate to run a computing task. I want ECS Fargate to be able to service a queue of requests and autoscale as needed. In other words, the backend of my app should ideally write to a queue in SQS with the data needed to run the task and ECS Fargate should poll the SQS queue and run the tasks as needed and return the output. How would I construct an architecture like this using Python Boto 3? Can someone provide a working code example?


Solution

  • You would configure the ECS auto-scaling to scale based on the SQS ApproximateNumberOfMessagesVisible metric. It could scale down to 0 ECS Fargate tasks when there are 0 messages visible, and scale up to multiple tasks polling the queue based on the scaling configuration you configure.

    Inside each task your Python code would have a loop where it polls the SQS queue using the Boto3 receive_message method. Ideally using long polling. Inside the loop your code would take each message that was received and do whatever processing you need to do on that message, then call delete_message when it has successfully processed the message.