I know that DigitalOcean still does not have auto scaling for Droplets, but I’m using Terraform to manage my infrastructure and want to set up an auto-scaling group for Droplets based on CPU usage.
Ideally, when CPU usage exceeds a certain threshold, new Droplets should be created automatically, and when the usage decreases, they should be removed. What’s the best way to approach this with Terraform and DigitalOcean, and are there any limitations or additional services I need to integrate to achieve this?
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Hey!
Great question! Indeed, as you mentioned, while DigitalOcean doesn’t have native auto-scaling for Droplets, you can still achieve something similar by combining Terraform with monitoring tools like DigitalOcean’s Monitoring and external automation for scaling.
Here’s an example of how you can use a variable in Terraform to control the number of Droplets:
variable "droplet_count" {
default = 1
}
resource "digitalocean_droplet" "web" {
count = var.droplet_count
name = "web-${count.index + 1}"
image = "ubuntu-20-04-x64"
region = "nyc3"
size = "s-1vcpu-1gb"
}
In this case, the droplet_count
variable will control how many Droplets are created. You can modify this value dynamically based on the CPU usage and then trigger a Terraform apply.
resource "digitalocean_loadbalancer" "lb" {
name = "web-lb"
region = "nyc3"
forwarding_rule {
entry_port = 80
entry_protocol = "http"
target_port = 80
target_protocol = "http"
}
droplet_ids = digitalocean_droplet.web[*].id
}
This will provide a way to “auto-scale” your Droplets based on demand, even though it’s not built into DigitalOcean’s platform natively.
On another note, if you’re just getting started with Terraform and DigitalOcean, this ebook might be helpful: Introduction to Terraform ebook.
Let me know if you have any other questions, and good luck with your project!
- Bobby
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.