Skip to main content

Command Palette

Search for a command to run...

Day 63: Demystifying Terraform Variables: A Comprehensive Guide

Published
2 min readView as Markdown

Day 63 - Terraform Variables 🚀🔥


Introduction

Welcome to another edition of Terraform Tutorials! In today's post, we'll dive deep into Terraform variables and explore their significance in infrastructure as code (IaC) workflows. Whether you're a Terraform novice or an experienced user, understanding how to leverage variables is crucial for efficient and maintainable configurations.

Task-01: Creating a Local File

Let's start by creating a simple local file using Terraform variables. In your variables.tf file:

variable "filename" {
  default = "/home/ubuntu/terraform-tutorials/terraform-variables/demo-var.txt"
}

variable "content" {
  default = "This is coming from a variable which was updated"
}

In your main.tf file:i

provider "local" {}

resource "local_file" "devops" {
  filename = var.filename
  content  = var.content
}

Run the following commands:

terraform init
terraform apply

Take a look at the generated local file, demo-var.txt, and observe how the content is derived from the specified variable.

Task-02: Exploring Data Types

Terraform supports various data types, including lists, sets, and objects. In your variables.tf file:

variable "list_example" {
  type    = list
  default = ["item1", "item2", "item3"]
}

variable "set_example" {
  type    = set(string)
  default = ["value1", "value2", "value3"]
}

variable "object_example" {
  type = object({
    key1 = string
    key2 = number
    key3 = bool
  })
  default = {
    key1 = "value1"
    key2 = 42
    key3 = true
  }
}

Run:

terraform refresh

  1. Review Output: Examine the outputs showcasing the usage of list, set, and object data types within Terraform configurations.

    Explore how Terraform handles lists, sets, and objects. Check the outputs to see the values generated based on these data types.

Conclusion

In this tutorial, we covered the essentials of Terraform variables, creating local files, and working with different data types. Remember, mastering variables is key to crafting flexible and dynamic infrastructure configurations.

I hope you found this guide informative! If you have any questions or insights to share, feel free to leave a comment below.

Happy Terraforming!

Lokesh Telange ✨✨

More from this blog

Introduction Of DevOps

108 posts