hope you can help out with my question.
i want to create new resources using the tfvars method. However instead of creating new resources, terraform wants to replace resources… i’m unsure what i’m doing incorrect…
this is my code:
i was expecting that when i run 2 plans with the tfvars files i would end up with 2 resources, however i end up with 1 resource that terraform want’s to replace…
i’m using Tf version 1.3.1
to give some context: i’m creating multiple resources in my terraform code, also based on tfvars. i’m also destroying these resources. to make sure i only destroy resource that are part of an environment i want to create various security groups
As far as Terraform is concerned, first you told it to create a resource, and then you told it to change that existing resource from having one name to another. In the case of this resource type, I guess that change needs a destroy and recreate.
Having multiple pre-prepared tfvars files is not the same as having entirely separate environments that Terraform manages independently.
There are various ways to achieve that, but the simplest might be to make a complete copy of your code in a different directory, per environment.
using tfvars can be used to differentiate between environments and its resources that terraform creates, in the examples hashicorp provides, ec2 instances are also created with different instance types, using tfvars files.
the variable “env” creates a new name based on the input of the tfvars file, but it seems terraform sees the resources as allready existing…
simply copying the code to a different directory is exactly what i wanted to avoid, i wanted to be sure that my code base would be 100% the same for every environment.
If you want to discuss some documentation you’ve found, you’ll need to provide a link.
The term workspace is unfortunately rather overloaded within Terraform. There is the concept of workspace built in to the Terraform CLI, then there is a separate workspace concept existing in Terraform Cloud/Enterprise, and then there are other workspace-like arrangements that some people build in their custom automation around Terraform.
Perhaps my initial concept of the use case for tfvars files wasn’t correct.
I was hopping it would create (new) resources and destroy said resources based on the tfvars file provided with the plan/destroy and leave resource that are created / destroyed via other tfvars files alone.
This seems inline with the link provided.
All that link says, is that input variables can be specified as files. Nothing on that page says anything about that enabling multiple environments that are managed independently.
If you’d like me to try to make a more targeted suggestion, you’d have to share how you are managing storage of your Terraform state, and triggering of Terraform runs.
You are seeing what you want to see instead of what is actually written then.
An example use case would be scaling a deployment up and down by providing separate tfvars files to define small/medium/large scaling.
–
According to
the S3 backend supports storing different workspace’s states at different keys in S3.
Therefore you could proceed by using the built in Terraform CLI workspace support to separate your environments.
For this to work, you’d have to ensure you always matched the correct workspace with the correct -var-file - you may well want to write your own wrapper script to help with this.