# Experimental Lambda-based Scaler 🦑

**URL:** <https://forum.buildkite.community/t/experimental-lambda-based-scaler/425>\
**Category:** Elastic CI Stack for AWS\
**Created:** [April 10, 2019, 4:15am UTC](https://forum.buildkite.community/t/experimental-lambda-based-scaler/425 "2019-04-10T04:15:40Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![max](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/max/32/1523_2.png) [@max](https://forum.buildkite.community/u/max)\
**Post date:** [September 25, 2024, 4:45pm UTC](https://forum.buildkite.community/t/experimental-lambda-based-scaler/425/14 "2024-09-25T16:45:24Z")

</div>

In case others run into anything similar in the future:

For some reason, setting `disconnect-after-idle-timeout` did not fully work for me (at least, as I interpreted its functionality from this forum post). It would stop the `buildkite-agent` service on the underlying server/instance, but it would not ‘terminate the instance and decrement the autoscaling group desired count atomically’ as stated above.

I’m not sure why this is the case, but regardless, a workaround is as follows:  
In your buildkite agent’s cloud-init (or similar) script after you start the buildkite agent, run a background script that does something like:

```auto
#!/bin/bash
sleep 300
while service buildkite-agent status | grep -q 'Running'; do
  sleep 60
done
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
AWS_REGION=$(curl -s http://169.254.169.254/latest/meta-data/placement/region)
aws ec2 terminate-instances --instance-ids "$INSTANCE_ID" --region "$AWS_REGION"

```

Depending on your ASG config, you may need additional commands before the terminate to decrement the ASG desired-counts accordingly. Feels pretty janky but it works.

---

_[View the full topic](https://forum.buildkite.community/t/experimental-lambda-based-scaler/425)._
