Reputation: 896
I am confounded by what terraform variables are supposed to do. Right now they seem awfully useless to resolve my specific requirement, perhaps I'm really barking up the wrong tree. Let's say I have a config like this:
variable "majorVersion" {
type = "string"
}
module "media-assets" {
majorVersion = "${var.majorVersion}"
region = "us-east-1"
bucket = "uploads-${var.majorVersion}"
source = "./modules/media-assets"
user = "appics-production"
}
Let's assume simply the assets module defines S3 buckets for now. All goes well if I run terraform like so:
terraform apply -var="majorVersion=v1"
However, if I run it again with
terraform apply -var="majorVersion=v2"
I want terraform to leave the resources of v1 alone! Instead terraform wants to tear down v1!! How can I accomplish a parametrised set of configurations without having to write the whole shebang all over again for each version? It seems tedious and beside the point!
Upvotes: 0
Views: 304
Reputation: 528
Workspace is your friend. You use workspaces to switch between 'setups' without having to touch your scripts. These 'setups' can be versions, environments, whatnot.
To achieve your specific need, you could have this in your variable.tf
variable "majorVersion" {
type = "map"
default = {
v1 = "v1"
v2 = "v2"
}
}
module "media-assets" {
majorVersion = "${var.majorVersion[terraform.workspace]}"
}
Before you run your apply command, select the right workspace
terraform workspace select v1
Although, you can probably see that you will need to add to the majorVersion map as you go. For that, I would suggest leaving versioning out of terraform scripts.
Upvotes: 1