terraform tfvars vs variables.tf

9,558 views
Skip to first unread message

BC

unread,
Jun 22, 2016, 12:43:56 PM6/22/16
to Terraform
Can anyone explain the difference and use cases for a tfvars file over a variables.tf file? I am looking into the best way to do a multiple environment terraform config and am having a difficult time understanding what should go in a tfvars file and what should go in a variables.tf file. Currently I have all my variables in a 'variables.tf' file and I am planning to create separate variable files and (remote) state files for each environment. Examples and pointers would be greatly appreciated.

Andrew Langhorn

unread,
Jun 22, 2016, 2:41:46 PM6/22/16
to terrafo...@googlegroups.com
Hi Brett,

We do something very similar to what it seems you're describing: we have a 'template' directory in our repository containing all of our modules and their instantiations. This directory also contains a variables.tf where we instantiate variables, and sometimes provide default values to them.

In the same repository, we have a folder per environment, and have a .tfvars file in which we pass values to the variables from variables.tf (or override the values if they're defaulted already).

Andrew

On 22 June 2016 at 17:43, BC <brett...@gmail.com> wrote:
Can anyone explain the difference and use cases for a tfvars file over a variables.tf file? I am looking into the best way to do a multiple environment terraform config and am having a difficult time understanding what should go in a tfvars file and what should go in a variables.tf file. Currently I have all my variables in a 'variables.tf' file and I am planning to create separate variable files and (remote) state files for each environment. Examples and pointers would be greatly appreciated.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/96aa096a-cc05-41b7-b5ca-6f46d16099dd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Andrew Langhorn
Senior Site Reliability Engineer
Emailandrew....@thoughtworks.com

ThoughtWorks

David Adams

unread,
Jun 22, 2016, 11:11:28 PM6/22/16
to terrafo...@googlegroups.com
Yeah, we use it like Andrew, so we'll have one set of TF code, and multiple tfvars files, one for each "environment" (in our case, typically an aws region + environment). The best way to think about variables.tf is not as a place to define values for variables, but a place to define the _inputs_ for this set of tf files (really, a module). Some of those inputs can have defaults, maybe all of them, but you use the tfvars files to override.

I've found the tfvars files work best if you also use some sort of wrapper script to simplify the command line situation. So early on, we used a Makefile like this for our multi-environment TF projects:

    ENV := iad-prod

    all: setup plan apply

    setup:
        terraform get

    plan:
  terraform plan -var-file=${ENV}.tfvars -state=${ENV}.tfstate -backup=-

    apply:
  terraform apply -var-file=${ENV}.tfvars -state=${ENV}.tfstate -backup=-

    destroy:
  terraform destroy -var-file=${ENV}.tfvars -state=${ENV}.tfstate -backup=-

Then we have files like iad-prod.tfvars, pdx-nonprod.tfvars, etc. and we can run:

    $ make apply ENV=pdx-nonprod

And that will apply the TF using the values in pdx-nonprod.tfvars, and then write the state out to pdx-nonprod.tfstate.

We're currently transitioning to a more complicated wrapper that manages remote state for our projects as well, but this Makefile works really well for 95% of our projects.

On Wed, Jun 22, 2016 at 11:43 AM, BC <brett...@gmail.com> wrote:
Can anyone explain the difference and use cases for a tfvars file over a variables.tf file? I am looking into the best way to do a multiple environment terraform config and am having a difficult time understanding what should go in a tfvars file and what should go in a variables.tf file. Currently I have all my variables in a 'variables.tf' file and I am planning to create separate variable files and (remote) state files for each environment. Examples and pointers would be greatly appreciated.

--

Andre Marcelo-Tanner

unread,
Jun 28, 2016, 11:54:37 AM6/28/16
to Terraform
I do the same but use a shell script to wrap terraform which also uses a statefile and sets the env variable

BC

unread,
Jun 28, 2016, 4:43:15 PM6/28/16
to Terraform
All,
Very helpful, thanks for the pointers.
Reply all
Reply to author
Forward
0 new messages