Terraform — Retrieving Values From YAML files

Rafael Medeiros
3 min readFeb 24, 2023

Terraform is an open-source infrastructure as code tool that enables you to manage your infrastructure as code.

In this post, I will show you how to retrieve values from YAML files in Terraform. YAML is a human-readable data serialization format that is commonly used for configuration files. It is often used to define variables, configurations, and other data that are required for an application or service to run.

Retrieving values from YAML files in Terraform is useful when you need to extract specific data from a configuration file and use it in your infrastructure code. This can save you time and effort by reducing the need to manually enter data into your Terraform code.

To retrieve values from a YAML file in Terraform, you can use the yamldecode function. This function allows you to parse a YAML file and extract the values you need. Here's an example of how to use the yamldecode function:


locals {
yaml_rg= yamldecode(file("${path.module}/config.yaml"))
}
resource "azurerm_resource_group" "this" {
for_each = local.yaml_rg
name = each.key
location = each.value.location
}

In this example, we define a local variable called yaml_rg that retrieves the values from a YAML file called config.yaml in the same directory as the Terraform file. We then use the local.yaml_rg variable to set the values of the name and location parameters of the azure_resource_group resource.

This is how the config.yaml file looks like:

---
resource_groups:
"rg-01":
location: "eastus"
tags:
description: "this is a test resource group"
"rg-02":
location: "eastus"
tags:
description: "This is a second resource group"

If we use Terraform console command, we can inspect the local.yaml_rg variable:

This is exaclty what we are looking for. These values will be passed to the azurerm_resource_group that will create 02 resources due to the foreach loop we defined in the resource block.

If you need further explanation about foreach parameter, I have written this article to help you.

If you want to create more resource groups, you can easily add another block in the yaml file:

---
resource_groups:
"rg-01":
location: "eastus"
tags:
description: "this is a test resource group"
"rg-02":
location: "eastus"
tags:
description: "This is a second resource group"
"rg-03":
location: "westus"
tags:
description: "This is a third resource group"

As simple as that.

Using the yamldecode function in Terraform makes it easy to retrieve values from YAML files and use them in your infrastructure code. By doing so, you can reduce the risk of human error.

Conclusion

Retrieving values from YAML files in Terraform is a simple and effective way to manage your infrastructure as code. By using the yamldecode function, you can easily extract the data you need from your configuration files and use it in your infrastructure code.

Want more insights on DevOps, security, and automation?

Don’t miss out — Follow me!

Connect with me on Linkedin!

Sign up to discover human stories that deepen your understanding of the world.

Rafael Medeiros
Rafael Medeiros

Written by Rafael Medeiros

DevOps Engineer | CNCF Kubestronaut | 3x Azure | Cloud | Security | Devops | Another IT Professional willing to help the community

Responses (2)

Write a response

and makes strange errors
routeros_ip_dhcp_server_lease.leases["eset-eth"]: Creating...
Error: json.Unmarshal - invalid character '<' looking for beginning of value
with routeros_ip_dhcp_server_lease.leases["eset-eth"],
on main.tf line 5, in resource…

1

Very useful blog. Thanks.
I created yaml file like this
---
inbound_rules:
- name : "Ib-bs-nsgrule_443"
priority : 110
access : "Allow"
protocol : "Tcp"
source_address_prefix : "*"
destination_address_prefix : "*"
source_port_range : "*"
destination_port_range…