ALB ECS scale tasks to zero and scale up via lambda

0

I'm trying to create a setup where my ECS tasks are scaled down automatically when there's no traffic traffic (which works), and are scaled back up when someone connects to them.

For this I've created two target groups, one for my ECS task, and one for my lambda. The lamba and ECS task work great in isolation and they've been tested.

The problem is that I can't figure out how to tell ALB to route to the lambda when ECS has no registered targets. I've tried:

  1. Specifying in the same listener default rule fwding to both ECS (weight 100) and lambda (weight 0) and
  2. Specifying a default rule that goes to the lambda and a higher prio rule that goes to the ECS task.

In both cases only my ECS task target group is hit which which returns a 5xx error. If I check the target health description for my ECS target group I see

{
    "TargetHealthDescriptions": []
}

Which don't know seems like it should fail.

How should I build this?

3 Answers
0

I don’t believe this is a supported working method and the ALB is behaving as normal.

I’d question what your trying to achieve ?

profile picture
EXPERT
answered 8 days ago
  • I'm trying to scale down my cluster when it doesn't receive traffic and immediately scale back up when it does. It feels like a pretty standard setup.

  • You need to have 1 ECS task running at minimum at all times

0

Hi,

The official documentation details the whole process to route requests from ALB to Lambda: https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html

Also have a look to this path to have the details of a full setup: https://dev.to/brooklyn2706/triggering-lambda-function-with-application-load-balancer-2947

Best,

Didier

profile pictureAWS
EXPERT
answered 8 days ago
  • I would like it to go to lambda only when my ECS service has no tasks registered (because it's down). I don't know how to do that.

    The lambda spins up the targets again.

0

I think the problem here is detecting when there is "no traffic" vs "a little traffic" vs "a lot of traffic" - because for the first you'll want to use Lambda; for the second you want to use Lambda but start up ECS; and for the third you want to be using ECS. But how can you tell whether "a little traffic" is about to be "a lot of traffic" or going to go back down to "no traffic"?

If it were me and I was looking for cost-savings and flexibility I'd only use Lambda and avoid the complexity of trying to mix and match traffic between Lambda and ECS.

But if you absolutely must: I'd have a CloudWatch Metrics alarms for increasing/decreasing traffic which trigger a Lambda function that change the listener rules on the fly. So as traffic increases, scale the ECS cluster up then switch from Lambda to ECS; as it decreases switch back and scale down the ECS cluster.

profile pictureAWS
EXPERT
answered 6 days ago
profile picture
EXPERT
reviewed 6 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions