Hi and welcome to Your Devops Tip blog series. In this series I am going to share some interesting scenarios, challenge and solutions on topics related to realtime use cases on Cloud and Devops.
Scenario
Imagine you're working with Terraform and managing multiple .tfvars files to define your infrastructure variables.
Challenge
How do you ensure that the correct variable values are applied, especially when dealing with multiple .tfvars files with potentially conflicting settings?
Solution
Understanding how Terraform prioritizes these files can streamline your configuration management and avoid unexpected results.Here’s a quick glance on how Terraform handles variable files:
Automatic .tfvars Files
abc.auto.tfvars and terraform.tfvars are picked up automatically by Terraform.
abc.auto.tfvars has precedence over terraform.tfvars.
Example:
abc.auto.tfvars has below values
instance_type = "t3.medium"terraform.tfvars has below values
instance_type = "t2.micro"
Result: Terraform will use instance_type = "t3.medium" during terraform plan/apply.
Custom .tfvars Files
Custom files (e.g., xyz.tfvars) require you to explicitly specify them using the -var-file option. These files override all other .tfvars settings when provided.
Example
- xyz.tfvars has below value instance_type = "p3dn.24xlarge"
When specified during terraform plan or terraform apply, instance_type = "p3dn.24xlarge" will be used. If xyz.tfvars does not include all variables, Terraform will use values from terraform.tfvars or abc.auto.tfvars for any missing variables.
Now you know this, you can control which variables are applied and manage multiple .tfvars files effectively in Terraform.
The solution provided above is focussing on using native terraform techniques. There are wrappers like Teragrunt is also available for handling such scenarios.
Hope this helps streamline your Terraform configurations!
For more techbytes on Cloud and Devops please stay connected to NandiTechBytes on Youtube. Also I am sharing the playlist here for you , please check it out.
Playlist Cloud & Devops Projects and Tasks
Cheers
Keep Learning!
@NandiTechBytes