The open blogging platform. Say no to algorithms and paywalls.

Exploring Map, List, Set, and Object Datatypes in Terraform

Today, we will explore the power of variables in Terraform and how they can enhance the flexibility and reusability of our infrastructure code.

Today, we will explore the power of variables in Terraform and how they can enhance the flexibility and reusability of our infrastructure code. Variables in Terraform allow us to define and store dynamic values that can be used across our configuration files.

What are variables in Terraform?

Variables in Terraform are used to store and manage dynamic values in the configuration files. They make the infrastructure code flexible and reusable. Variables can hold various types of data such as strings, numbers, lists, and maps. They allow customization and parameterization of the configuration, enabling the creation of multiple instances with different values. Variables are defined using the variable block and accessed using the var object.

Here is our Today’s Task-01:

- Create a local file using Terraform.
  • To begin, 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/terrform-tutorials/terraform-variables/demo-var.txt"
}

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

  • In this example, we have defined two variables: filename and content.
  • The filename variable holds the default path of a file, and the content variable holds the default content that will be written to that file.
  • To access these variables in our main.tf file, we can use the var object. For example:
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.

  • This initializes terraform in the folder. It installs all the plugins and providers required to run the Terraform file.
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 var-demo.txt with the content supplied in the content variable.
terraform apply

  • Now, we can see a file is created with the content that we had given in the variable file.

Here is our Today’s Task-02:

- Use terraform to demonstrate usage of Map, List, Set and Object datatypes.
  • Data Types in Terraform: — 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 -

In Terraform, a map is a data structure that allows you to store and manage a collection of key-value pairs. Maps are often used to define input variables, output values, and resource configurations.

  • Create a variable of type map and pass two statements that will act as content for two text files.
 variable "file_contents" {
 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.”
  • Run the commands terraform init, terraform plan and terraform apply.
  • After successfully commands run check now the demo.txt file content

List -

In Terraform, a list is a data structure that allows you to store and manage a collection of values. Lists are often used to define input variables, output values, and resource configurations.

  • Create a variable of type list to pass the list of files.
  • Replace the file name in the main.tf code to the list of file variables. This will assign the filename and location through the variable.
  • Run the commands terraform init, terraform plan and terraform apply.
  • After successfully commands run check now the file1 and file2 content.

Object -

Unlike a map and list, an object is a structural type that can hold several types of values. It is a set of named attributes, each with its own type. In the example, an object variable named aws_ec2_object is defined with the default value of a set of EC2 instance properties. This variable can be used to refer to the specific properties of the EC2 instance defined in the object in your Terraform configuration.

  • Now, create an output file to view the output of a specific value as per our wish.
  • You can see the output of aws-ec2-instance value.

Set -

In Terraform, a set is a data structure that allows you to store and manage a collection of unique values. Sets are often used to define input variables and to manage resource configurations that require unique sets of values.

  • Create a variable for the type set to pass the string of security groups.
  • Create a output file to check the security group.
  • Plan and apply the terraform for the HCL code.

🔸Thankyou for reading 👍.




Continue Learning