DEV Community

Raju Nandi
Raju Nandi

Posted on

Your DevOps Tip#2 - On Managing Terraform Variables

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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay