Last active
May 26, 2020 17:51
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
# ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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