Snowball
Snowball

Reputation: 1512

Vertical Scaling with Terraform

I am using terraform to set up a VPS Cluster.

This works already pretty well. I have defined a variable called instance_size which is set to 1gb by default. I am setting up several VPS with the exact same configuration by using the count parameter within my resource definition:

variable "swarm_instance_size" {
    default = "1gb"
}

resource "digitalocean_droplet" "server" {
    image = "123456"
    size = "${var.instance_size}"
    count = "3"
    name = "${format("server-%02d", (count.index + 1))}"
    region = "sfc1"
}

(Excluded SSH Keys and Provisioners for Brevity)

My Question is now how to best vertically scale only a single one of those ressources.

I am able to scale all of them without downtime by increasing the instance_size to for example 2gb and executing terraform apply --parallelism 1 so that only a single resource gets updated at a time.

I can as well target only a single one of those ressources by using terraform apply -target digitalocean_droplet.server[0] -var instance_size=2gb. The problem is that this leads to unexpected configuration drift as I haven't declared that change anywhere else than when executing the command.

Is the only and best way to declare each one of those servers as a single resource ommiting the count parameter? And than if I want to add a new server instead of increasing the count parameter I just add another resource definition to my terraform config?

Upvotes: 1

Views: 585

Answers (1)

ThomasVdBerge
ThomasVdBerge

Reputation: 8140

By using a map

For example (this goes in your variables.tf file):

variable "machines" {
  description = "description here"
  type = "map"
  default = {
    "server-01" = 1gb
    "server-02" = 1gb
    "server-03" = 2gb
  }
}

Then in your actual code:

size = ${lookup(var.machines, format("server-%02d", (count.index + 1)))}

You can then apply the plan.

Upvotes: 1

Related Questions