Sling Academy
Home/DevOps/Using YAML with Terraform: A Practical Guide (with examples)

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

Last updated: February 03, 2024

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.

Next Article: How to write conditional logic in Terraform

Previous Article: How to use outputs to expose Terraform provisioned resources

Series: Terraform Tutorials

DevOps

You May Also Like

  • How to reset Ubuntu to factory settings (4 approaches)
  • Making GET requests with cURL: A practical guide (with examples)
  • Git: What is .DS_Store and should you ignore it?
  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide