Multiple input.tfvars with remote state file

I am new to terraform and I am dealing with a simple but difficult situation.
I am building a single vsphere virtual machine using remote state file. Once machine is build if i want to build another machine using input.tfvars file terraform creates new machine but at the same time it destroy already created machine which is there in tfstate file. How to avoid this situation.

Welcome @ausafonly!

You can try using workspaces to separate your state files. This way, your state files for different environments, etc. won’t overlap and when you run terraform apply with a new variable file, the previous state file won’t be destroyed.

Thanks Rosemary but what if all my team members want to use same state file because not only me but other team members also working on building VM’s.

Hi @ausafonly,

Thanks for your patience in my response. It would help to learn more about your working pattern.

  • How are you distributing and managing configurations as a team (version control, saved in a shared drive)?

  • How is state stored in your environment (remote, locally on your machine)?

Generally, if state is stored remotely, we can approach this in a few ways:

  1. Add new .tf files, one per VM, in the same module. Run terraform apply in this single module, which will append new or updated VMs to the current remote state file.

  2. Each team member switches workspaces to the VM they want to configure and build. This will switch to the state file associated with the separate VM.

Let me know! I’d love to learn more about how your team is using Terraform to build VMs and what you’re looking to achieve.

Rosemary will try this method and share my experience.