Terraform Variables

This is Day 63 of #90daysofdevops challenge

Terraform Variables

What are variables in terraform?

Variables are a fundamental concept in Terraform that allows you to customize your infrastructure configuration. There are three main types of variables in Terraform:

  • Input Variables - Used to pass input values to a Terraform module from outside. They act as arguments to the module.

  • Local Variables - Used to assign temporary names to expressions within a module. They are scoped to the module where they are declared.

  • Output Variables - Used to export values from a Terraform module. They act as return values from the module.

Task-01: Create a local file using Terraform

  • Let’s create a variables.tf file where we will define our variables.

  • Variables can hold values such as instance names, configurations, or any other information needed for our infrastructure.

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

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

  • In this example, there are 2 variables filename and content. Variable filename holds the path and variable content holds the content that will be written in that file.

  • These variables can be accessed by var object in main.tf

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

  • By referencing var.filename and var.content, we can access the values stored in our variables and utilize them within our Terraform resources.

  • Initializes a new or existing working directory for Terraform

terraform init

  • Produces an execution plan outlining the steps Terraform will take to achieve the desired state defined in the configuration file.
terraform plan

  • When you run Terraform apply, Terraform will generate a file in the local directory named demo-var.txt with the content supplied in the content variable.
terraform apply

  • Check file is created in a specified folder using the ls command.

Task-02: Use terraform to demonstrate usage of List, Set and Object datatypes

Terraform supports various data types, such as maps, lists, sets, and objects, allowing us to handle complex data structures within our configurations. Let’s explore some of these data types and demonstrate their usage.

Map -

A map in Terraform is a data structure used to represent a collection of key-value pairs. Maps are useful for storing configuration data, defining variables, and passing information between different parts of your code.

  • Create a variable of type map and pass two statements that will act as content for two text files.
variable "content_map" {
type = map
default = {
"statement1" = "this is cool"
"statement2" = "this is cooler"
}
}

  • Create two files that will pick the content of the map variable.

  • In this example, we set the content parameter of the local_file resource using the content_map variable. We use dot notation and the syntax var.content_map[“content1”] to refer to the value associated with the key “content1.”

  • Let's run terraform init

terraform plan

terraform apply

  • After successfully commands run check now the files content

List -

Terraform supports lists as a data type to represent an ordered collection of values. Lists are useful for:

  • Creating multiple resources with the same configuration

  • Passing a variable number of values to a module or resource

Now, Create a variable of type list to pass the list of files.

  • Now, replace the file name in main.tf to the list of file variables

  • Run the commands terraform init, terraform plan and terraform apply.

  • Now, check the content inside the files

Object -

Terraform supports objects as a data type to represent a collection of named attributes with different types. Objects are useful for:

  • Grouping related attributes together

  • Making module inputs flexible

  • Allowing optional attributes

The basic syntax for defining an object is:

  variable "config" {
    type = object({
      size  = string
      users = number
    })
  }

This defines a config variable which contains an object with size and users attributes of different types.

You assign values using:

  config = {
    size = "large"
    users = 100
  }

Let's define the object in our variable.tf file

Now, create an output file to view the output of a specific value as per your wish.

Let's run terraform apply to see the output.

Set -

In Terraform, a set is a data structure that allows you to store and manage a collection of unique values. Sets are useful for:

  • Ensuring resource attributes contain unique values

  • Specifying a list of values without duplicates

Create a variable for the type set to pass the string of security groups.

  • Create an output file to check the security group.

  • Run the commands terraform init, terraform plan and terraform apply.


"Thank you for enjoying my DevOps blog! Your positive response fuels my passion to dive deeper into technology and innovation.

Stay tuned for more captivating DevOps articles, where we'll explore this dynamic field together. Follow me on Hashnode and connect on LinkedIn (https://www.linkedin.com/in/som-shanker-pandey/) for the latest updates and discussions.

Did you find this article valuable?

Support Som Pandey's blog by becoming a sponsor. Any amount is appreciated!