What are complex types (objects) in Terraform?

Overview

In Terraform, we have complex data types which allow us to group several simple data types in a single variable. This concept is identical to the class principle we see in most commonly used object-oriented programming languages. This functionality allows us to assign different data types to specific subfields within the variable. The following example uses a triangle object to demonstrate this data type.

Declaring an object in Terraform

To declare an object in Terraform, we’ll create three files:

  • vars.tf
  • outputs.tf
  • testing.tfvars

Variable file

First, we create a vars.tf file to define the structure of our object and declare its subfields:

variable triangle{
    type = object({
             s_one= number, 
             s_two= number,
             s_three= number, 
             description= string

 })
}

This file aims to create our desired object to use later. As shown in the example above, an object variable with the name triangle is created, followed by its structure (3 subfields for lengths and a description):

  • s_one:number
  • s_two:number
  • s_three:number
  • description:string

Output file

Next, we’ll create an outputs.tf file and declare an output variable to display the object we defined in the vars.tf file:

output triangle_output{
    value = var.triangle
    }

This file aims to create an output value to store and print the object we declared in the vars.tf file. As shown, an output value is created to match the triangle variable previously declared:

  • triangle_output:triangle

Testing file

Finally, we’ll create a testing.tfvars file to assign values to the object declared in the vars.tf file:

triangle={
s_one=1,
s_two=2.5,
s_three=2.5,
description="this is a triangle"
}

The purpose of this file is to assign values to all the subfields within the object declared in the vars.tf file, using a single script.

Without a testing.tfvars file, the user will be prompted for input for each of the variables. This might get tedious as the vars.tf file increases in size. As shown in the file, the following values are assigned to the variables within the object previously declared:

  • Type: triangle

  • s_one:1

  • s_two: 2.5

  • s_three:2.5

  • description: “this is a triangle”

Example

The following code executes the Terraform infrastructure for the three files we have created (vars.tf, outputs.tf, testing.tfvars) and displays the values of the variables in the terminal:

output triangle_output{
    value = var.triangle
    }
Complex types in Terraform

Explanation

To initialize the Terraform infrastructure for the above working example:

  1. Navigate to the directory where files are present.

  2. To build and run the terraform files, we’ll enter the following command in the terminal: terraform init && terraform apply -auto-approve -var-file="testing.tfvars"

This will initialize the project and assign the values present in the testing.tfvars file.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved