Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform environment specific variables

Tags:

terraform

Anyone know if there's a way to populate variables in Terraform based on what the environment/workspace is? Preferably one that

  • populates the var namespace (ie not an external data source),
  • doesn't require a wrapper
    • like tf(){ terraform --var-file=$(get_tf_env).tfvars
  • takes effect by changing a terraform env/workspace, without any additional manual steps (ie steps that aren't triggered by running terraform env )?
like image 850
Rory Browne Avatar asked Aug 09 '17 10:08

Rory Browne


1 Answers

Populates the var namespace, doesn't require a wrapper, and takes effect by changing the workspace (Terraform 0.12 code):

variable "ami_id" {
  type = map(string)

  default = {
    stg = "ami-foo28929"
    prd = "ami-bar39b12"
  }
}

resource "aws_instance" "this" {
  ami = var.ami_id[terraform.workspace]
  (...)
}
like image 187
AlexT Avatar answered Jan 22 '23 05:01

AlexT