Question

How can I automatically scale my Droplets based on CPU usage using Terraform and DigitalOcean?

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?


Submit an answer


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!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
October 24, 2024

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.

  1. First, set up DigitalOcean Monitoring or an external monitoring tool like Prometheus to keep track of CPU usage on your Droplets. DigitalOcean Monitoring can trigger alerts when CPU thresholds are exceeded, which is key for deciding when to scale up or down:

https://docs.digitalocean.com/products/monitoring/

  1. Once you have the monitoring in place, since Terraform isn’t event-driven, you’ll need to use a cron job, CI/CD pipeline (like GitHub Actions or Jenkins), or another automation tool to trigger Terraform runs. The idea is to monitor the CPU usage and, when the usage crosses a defined threshold, trigger Terraform to either scale up by creating more Droplets or scale down by destroying unnecessary ones.

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.

  1. To distribute traffic across the Droplets, use DigitalOcean’s Load Balancers. Here’s how you can set up a load balancer using Terraform:
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

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.