Using YAML files in Terraform configuration
In the previous recipes, we learned that to set dynamic values inside Terraform configuration, we can use variables.
In some use cases, we must use an external source for configuration, such as JSON or YAML files, and we can imagine that these files are provided manually by external teams or generated automatically by external systems, and we can’t rewrite these files in Terraform variables.
The goal of this recipe is to show how to use a YAML file inside a Terraform configuration.
Let’s get started!
Getting ready
To complete this recipe, we have a YAML file named network.yaml
with the following content:
vnet: "myvnet"
address_space: "10.0.0.0/16"
subnets:
- name: subnet1
iprange: "10.0.1.0/24"
- name: subnet2
iprange: "10.0.2.0/24"
This file contains the configuration of the Azure network with virtual network and subnets configuration, and it’s placed in the same folder as the Terraform configuration.
In our Terraform configuration, we will use this YAML file to provision the Azure virtual network and subnets.
The source code of this recipe is available at https://github.com/PacktPublishing/Terraform-Cookbook-Second-Edition/tree/main/CHAP02/yaml.
How to do it…
Perform the following steps:
- First, in
main.tf
, create alocals
variable callednetwork
that calls the built-inyamldecode
Terraform function:locals { network = yamldecode(file("network.yaml")) }
- Then call this
local.network
variable and its subproperties, which are defined in the YAML file inside the Terraform resource:resource "azurerm_virtual_network" "vnet" { name = local.network.vnet location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name address_space = [local.network.address_space] dynamic "subnet" { for_each = local.network.subnets content { name = subnet.value.name address_prefix = subnet.value.iprange } } }
- Finally, run the Terraform workflow with the
init
,plan
, andapply
commands. The following picture shows theplan
execution:
Figure 2.13: Terraform uses the YAML file
- We can see that the Terraform configuration uses the content configuration of the YAML file.
How it works…
In Step 1, we use the built-in Terraform function yamldecode
, which takes on the parameters of the network.yaml
file. This function decodes YAML content to Terraform key-value maps.
The result of this map is stored in a local variable named network
.
Then in Step 2, we call this local variable by using local.network
and use all the sub-keys as object notation, defined in the YAML configuration.
That is all for the Terraform configuration. Finally, we run the terraform
init
, plan
, and apply
Terraform commands.
The plan
execution shows that Terraform uses YAML for configuration.
There’s more…
In this recipe, we saw how to decode a YAML file, but we can also encode a YAML file, from Terraform to YAML, by using the built-in yamlencode
Terraform function (https://www.terraform.io/language/functions/yamlencode).
We learned an example of decoding a YAML file. With Terraform we can do the same operations with a JSON file using the built-in jsondecode
and jsonencode
functions.
However, it is better to use Terraform variables for Terraform variable validation by using the terraform validate
command. Indeed, the YAML file will not be integrated into the validation of Terraform if it is badly formatted or if some information is missing – in these instances it will throw an error.
See also
- The documentation of the built-in
yamldecode
function is available at https://www.terraform.io/language/functions/yamldecode. - The built-in
jsonencode
function documentation is available at https://www.terraform.io/language/functions/jsonencode. - The built-in
jsondecode
function documentation is available at https://www.terraform.io/language/functions/jsondecode.