Skip to content

Instantly share code, notes, and snippets.

@syntaqx
Last active May 26, 2020 17:51
Show Gist options
  • Save syntaqx/93dc17e8af71a929277ff5a5928ee83f to your computer and use it in GitHub Desktop.
Save syntaqx/93dc17e8af71a929277ff5a5928ee83f to your computer and use it in GitHub Desktop.
Terraform v0.12.0 Using variables dynamically in resources hack until we get for_each. Only works for sets of 2 as implemented.
variable "nodes" {
type = map(object({
size = string
count = number
}))
default = {
manager = {
size = "s-1vcpu-1gb"
count = 1
}
worker = {
size = "s-1vcpu-1gb"
count = 1
}
}
}
locals {
node_keys = keys(var.nodes)
}
resource "digitalocean_droplet" "node" {
count = var.nodes.manager.count + var.nodes.worker.count
name = format("%s-node-%s-%s-%02d", var.project,
(count.index < var.nodes.manager.count ? node_keys[0] : node_keys[1]),
var.region,
count.index + 1 - (count.index < var.nodes.manager.count ? 0 : var.nodes.manager.count),
)
size = (count.index < var.nodes.manager.count ? var.nodes.manager.size : var.nodes.worker.size)
# ...
}
variable "nodes" {
type = map(object({
size = string
count = number
}))
default = {
manager = {
count = 1
size = "s-1vcpu-1gb"
}
worker = {
count = 1
size = "s-1vcpu-1gb"
}
}
}
resource "digitalocean_droplet" "node" {
for_each = var.nodes
count = each.value.count
name = format("%s-node-%s-%s-%02d", var.project, each.key, var.region, count.index + 1)
size = each.value.size
# ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment