# Dynamic number of agents per instance

**URL:** <https://forum.buildkite.community/t/dynamic-number-of-agents-per-instance/4271>\
**Category:** Elastic CI Stack for AWS\
**Created:** [March 3, 2025, 5:07am UTC](https://forum.buildkite.community/t/dynamic-number-of-agents-per-instance/4271 "2025-03-03T05:07:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![abraham](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@abraham](https://forum.buildkite.community/u/abraham)\
**Post date:** [March 3, 2025, 5:07am UTC](https://forum.buildkite.community/t/dynamic-number-of-agents-per-instance/4271/1 "2025-03-03T05:07:26Z")

</div>

Hi,

In our current stack configuration we have a fixed number for the AgentsPerInstance parameter. I was wondering if there’s a way to scale that depending on the instance type. For example, a 12xlarge could handle 24 agents but a 24xlarge could support 48.  
We’re currently struggling with spot instance availability and being more flexible with our allowed instance types could help.

Thanks!

---

<div class="post-metadata">

**Author:** ![jykingston](https://avatars.discourse-cdn.com/v4/letter/j/7feea3/32.png) [@jykingston](https://forum.buildkite.community/u/jykingston)\
**Post date:** [March 3, 2025, 6:03am UTC](https://forum.buildkite.community/t/dynamic-number-of-agents-per-instance/4271/2 "2025-03-03T06:03:24Z")

</div>

Hey Abraham 👋

Welcome back to the Buildkite Forums!

There isn’t a option that would allow this in the CloudFormation stack template, but you could easily do this with a bootstrap script that edits the agent config!

The template has a `BootstrapScriptUrl` parameter which will run before the agent starts, that would let you perform any logic needed to alter the spawn count.

For example, you could get the instance type using the ec2 metadata `ec2-metadata --instance-type` and depending on the value returned you could then alter the `spawn` value that’s set in `/etc/buildkite-agent/buildkite-agent.cfg`.

If you’d prefer to not use the config file there is also a env which controls this; `BUILDKITE_AGENT_SPAWN` you can [read more about it here](https://buildkite.com/docs/agent/v3/configuration#spawn).

Hope that helps!

---

<div class="post-metadata">

**Author:** ![abraham](https://avatars.discourse-cdn.com/v4/letter/a/6a8cbe/32.png) [@abraham](https://forum.buildkite.community/u/abraham)\
**Post date:** [March 5, 2025, 4:35am UTC](https://forum.buildkite.community/t/dynamic-number-of-agents-per-instance/4271/3 "2025-03-05T04:35:43Z")

</div>

Hi,

Thanks for your suggestions. I was able to control the number of agents by modifying the .cfg as you suggested (although my first attempt with simply calling `export BUILDKITE_AGENT_SPAWN=N` didn’t appear to have any effect).

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.buildkite.community/system/32/1321_2.png) [@system](https://forum.buildkite.community/u/system)\
**Post date:** [April 4, 2025, 2:36pm UTC](https://forum.buildkite.community/t/dynamic-number-of-agents-per-instance/4271/4 "2025-04-04T14:36:14Z")

</div>

Closed on April 4, 2025, 2:36pm UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
