-
Book Overview & Buying
-
Table Of Contents
Terraform Cookbook - Second Edition
By :
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!
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.
Perform the following steps:
main.tf, create a locals variable called network that calls the built-in yamldecode Terraform function:
locals {
network = yamldecode(file("network.yaml"))
}
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
}
}
}
init, plan, and apply commands. The following picture shows the plan execution:
Figure 2.13: Terraform uses the YAML file
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.
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.
yamldecode function is available at https://www.terraform.io/language/functions/yamldecode.jsonencode function documentation is available at https://www.terraform.io/language/functions/jsonencode.jsondecode function documentation is available at https://www.terraform.io/language/functions/jsondecode.