Using YAML with Terraform: A Practical Guide (with examples)

Updated: February 3, 2024 By: Guest Contributor Post a comment

Introduction

YAML, a human-friendly data serialization standard, is widely used for configuration files in software applications. Terraform, on the other hand, is an immensely popular infrastructure as code tool that allows users to define and provision infrastructure using a high-level configuration language known as HashiCorp Configuration Language (HCL). Integrating YAML with Terraform can offer the flexibility of YAML with the power of Terraform, easing the management and provisioning of infrastructure.

This guide will cover how to use YAML with Terraform effectively, showcasing its potential through practical examples. We’ll start with the basics and gradually move on to more advanced scenarios.

Getting Started with YAML and Terraform

To kick things off, let’s establish a baseline understanding of YAML and how we can utilize it within Terraform configurations.

Basic YAML Configuration Example

# config.yaml
name: "my-vm"
size: "large"

This simple YAML configuration file specifies a virtual machine (VM) name and size. Let’s see how to use this YAML file within Terraform.

Reading YAML in Terraform

locals {
  config = yamldecode(file("${path.module}/config.yaml"))
}

resource "virtual_machine" "example" {
  name = local.config.name
  size = local.config.size
}

This Terraform snippet demonstrates how to read the YAML file and use its values to provision a VM. The yamldecode function interprets the YAML file and converts it into a map that Terraform can understand.

Managing Lists and Maps in YAML for Terraform

YAML’s real power shines when handling complex data structures like lists and maps. Here’s how to manage such structures in Terraform:

List Example

# vm_list.yaml
vms:
  - name: "vm1"
    size: "small"
  - name: "vm2"
    size: "medium"

And the corresponding Terraform configuration:

locals {
  vm_list = yamldecode(file("${path.module}/vm_list.yaml"))
}

resource "virtual_machine" "vm" {
  count = length(local.vm_list.vms)

  name = local.vm_list.vms[count.index].name
  size = local.vm_list.vms[count.index].size
}

Here, we use Terraform’s count attribute to create multiple VMs based on the list in the YAML file. This demonstrates how to loop over lists in YAML to create resources in Terraform.

Advanced YAML Usage

Moving to more advanced topics, let’s explore how to handle conditional logic and incorporate external data into our Terraform configurations using YAML.

Using YAML for Conditional Logic

# conditional.yaml
env: "production"
tags:
  production: "true"
  development: "false"

To apply this logic within Terraform:

locals {
  env_config = yamldecode(file("${path.module}/conditional.yaml"))
}

resource "some_resource" "example" {
  tags = {
    Environment = local.env_config.env
    Production = local.env_config.tags.production
    Development = local.env_config.tags.development
  }
}

This setup allows you to maintain environmental configurations in YAML, making it easier to manage and understand.

Conclusion

Integrating YAML with Terraform enhances both the ease of use and the power of infrastructure as code. Through practical examples, we’ve demonstrated how to handle simple to complex configurations using the two. As you expand your use of Terraform and YAML, the possibilities for managing and provisioning your infrastructure become virtually limitless.