Reputation: 2025
AWS Fargate as a concept is an amazing service. It allows you to downscale your service to 0 instances when it's not actively used, or at least this is how it is advertised. In practise I'm having a hard time making this happen. As far as I can tell, you do need a load balancer in front of Fargate, and ALB automatically performs health check requests every 30 seconds, preventing the container from ever going to sleep. The maximum time for the health check is 300 seconds. Not much really taken into account that the service will be needed only for like 15 minutes a day. What is the right way of exposing the Fargate-based application to the world in such a way that it actually goes down when not used?
Upvotes: 0
Views: 1185
Reputation: 471
If you choose to use AWS Fargate with ALB, as you mention, you will never get it to turn off. To be fair, it typically takes 15-30s for a container task to spin up so the fact you have it behind an active Load Balancer would give a fairly negative experience to callers for very infrequent tasks since they would timeout.
You have several options here though depending on how known interval traffic you have and whether you can prepare in advance.
Assumption: Cost Savings is driver and traffic is randomly/low volume called via sync HTTP/S calls.
Note: A dedicated ALB + Fargate will always have a provisioned capacity and can be overkill for rarely called code.
1) Port your app to Lambda and use API Gateway
This means you only get charged for calls coming through. The startup of a lambda 'container' is very fast and if you get only a few calls a day it will cost pennies. This does require you to port your app though which can be difficult.
2) API GW + Lambda proxy
This is tricker - and assumes that the caller will retry after 30s timeout. What you do here is have the call initially come into a Lambda function that will START UP your Fargate service. It then makes call and returns result. You use the scaling rules to shut it down as traffic went away (typically minutes of low activity). Here you don't have a LB though..you are doing that all in Lambda code since assumption is you only have 1 task running.
Upvotes: 1