2

I am trying to create a terraform script which will create a vpc and other resources. I am passing the parameters for scripts from a .tfvars file. I have successfully created the vpc and resources by executing the script. Now I want to create another vpc with same set of resources but with different set of parameter values. I have created a new .tfvars file with new values and tried to execute it with the old main.tf file. When I execute the 'terraform plan' command its showing that it will delete the vpc and resources created during my first run will create a new vpc with the new values. Is there any method to create resources using same terraform main.tf file and by changing the .tfvars file.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
Hajas
  • 115
  • 1
  • 3
  • 10

3 Answers3

4

You are running into a state-based issue. When you define a resource you give it a name. Those names are used in the state file and that is what is making Terraform to think you are trying to alter an existing resource. You have a couple of ways to address this and it depends on what you are really doing.

Terraform Workspaces

You could use workspaces in terraform for each VPC you are creating, this would keep the state separated, however, workspaces are really intended to separate environments, not multiple resources in the same environment. You can read more here.

Terraform Modules

What it sounds like to me is that you really want to create a terraform module for your VPC configuration. Then create each VPC using your module in the same main.tf. That way you will have unique names resources which will not confuse the state management. You can read more about modules here. A good resource for information about it can be found in this blog post.

Jamie
  • 3,094
  • 1
  • 18
  • 28
  • I am already using modules for creating aws resources, I have resolved the issue using workspace as you have mentioned. I have created different workspace for each vpc and its related resources. Now am able to execute my terraform script without any conflicts between the tfstate files. – Hajas Oct 31 '18 at 06:58
2

The way to do this is by creating a module. You should be able to pretty much cut / paste your current code in to your module. You may only need to remove the provider definition from your module. Then in your new main code (root module) reference the module for each set of resources you want to create.

Ah the reason TF is trying to remove the resources you already created is because they've been captured in its state.

When you create the module add the resources you already created back in. TF will always try and configure as per the code, if the resources are remove it will try and destroy them

Create a module in terraform

Tim Tharratt
  • 1,251
  • 1
  • 10
  • 18
2

This is because you are working on the same tfstate file.

Following you could do : 1. If you are working with local state: copy the whole code in a different directory and with new tfvars file and work there. This will start a new clean tfstate

  1. If you are working with remote state: a. Configure different remote state and then use new tfvars file, or b. Create a different directory, symlink your code to this directory and replace old backend config and tfvars file with the new one. I have sample code of working with multi-env https://github.com/pradeepbhadani/tf-course/tree/master/Lesson5

  2. Create a Terraform module of your VPC code and then call it from a separate directory.

Pradeep Bhadani
  • 4,435
  • 6
  • 29
  • 48