I try to find a useful example that demonstrates functionality when I create these posts and videos. Unfortunately, with Override files in Terraform, that was not so easy. As a matter of fact, you may never need to use override files at all, except for some specific situations we’ll get to shortly. With that said, Override files are another tool in the Terraform toolbelt and something you should be aware of if planning on a Terraform certification.
Override Overview
An Override file, as the name implies, overrides values in a Terraform configuration. It contains configuration settings that replace or override values in existing Terraform configuration files.
When Terraform runs, it first loads all .tf and .tf.json files in the current directory. Each of those files includes configuration settings for the deployment. We divide these files out to make the configuration more readable, but terraform treats them all as one file.
After those files are processed, the override file is merged into the configuration. The merge process replaces any values in the configuration with the values from the override file.
Using Override Files
Override files have the name override.tf, or override.tf.json. Also, an Override file can have a custom name flowed by an underscore and override.tf for underscore.tf.json
- override.tf
- override.tf.json
- _override.tf
- _override.tf.json
Multiple override files can be used and are processed and merged in lexicographical order. Override files can be used with Resource, Data, Variables, Output, Local, and Terraform blocks.
Override files cannot be applied to a module because the override file has to be in the root directory to take effect. The override file can’t be passed into a module. The better option is to expose anything that needs to change in the module as an input variable.
Why Bother?
You may be asking, why bother with override files when we can simply change the original Terraform files? That’s a good observation and in practice, changing the source makes for cleaner, easier-to-read code. Overriding the configurations can make the code difficult to read.
However, Override files can be useful for machine or developer-specific settings. For example, if we commit code to a central repository but need to tweak the local configuration. Override files are also helpful for automation. If using Azure DevOps or GitHub Actions, an Override file can be used to modify the deployment without changing the configuration files.
Demo
Let’s review a short example to see how Override Files work. The code below uses a previous example in this series on Data Sources located at the link below. The actual code is not important, and we don’t even apply the configuration. It is simply an example of Override files. https://github.com/tsrob50/TerraformExamples/tree/main/Data%20Example
Below is an example of the main.tf file. It shows version 2.90.0. The version at the time of writing is 2.91.0.
Copy the Terraform block from the main.tf file and paste it into a new file called override.tf. Remove the source line. That line is not required because only the value of the version will change.
The new override.tf file will look like the image below.
Next, as a second example, let’s change the name of the resource group with the override file. In this example, the resource group name was hard-coded. It should have been a variable, and although not how we would do it in the real world, it makes for an okay example.
Copy the resource group block into the override.tf file. Remove the location, only the name is changed for this example. Update the Resource Group name, this example uses tftestrg02. Once finished, the updated override.tf file looks like the image below.
Save the file and run terraform init and terraform plan. Once finished, notice the AzureRM version shows the updated value of 2.91.0 and the resource group name now shows the updated name.
That is how to use an Override file to modify settings in a Terraform configuration.