DEV Community

Dulanjana Lakmal
Dulanjana Lakmal

Posted on

Terraform list of object validation

Introduction for Terraform

Terraform is an open-source infrastructure as code (IaC) tool developed by HashiCorp. It allows users to define and provision infrastructure resources such as virtual machines, storage accounts, and networking components in a declarative way using a high-level configuration language.

With Terraform, infrastructure can be described in code, allowing for version control, collaboration, and automation. Terraform also supports a wide range of cloud providers, including Amazon Web Services, Microsoft Azure, Google Cloud Platform, and others, as well as on-premises solutions.

Terraform uses a state file to keep track of the current state of the infrastructure, which can be used to plan, apply, and manage changes to the infrastructure over time. This makes it easier to manage and maintain complex infrastructure environments, and allows for easier scaling and modification of infrastructure as needed.

What is list of object?

In Terraform, a list of objects is a data structure that allows you to define a list of maps, where each map represents an object with a set of key-value pairs. This data structure is often used to represent a collection of similar resources or configurations that need to be created in a single block of code.

Here’s an example of a list of objects in Terraform, which represents a collection of virtual machines to be created:

virtual_machines = [  { 
  name = "vm-1"   
  size = "Standard_DS2_v2"  
  image = "UbuntuServer:16.04.0-LTS"  },
{ 
  name = "vm-2"
  size = "Standard_DS1_v2"
  image = "UbuntuServer:18.04-LTS"  },
{    
  name = "vm-3"
  size = "Standard_DS3_v2"
  image = "WindowsServer:2016-Datacenter"
  }]
Enter fullscreen mode Exit fullscreen mode

In this example, virtual_machines is a list of maps, where each map represents a virtual machine configuration with name, size, and image attributes. This list can then be used in Terraform to create multiple virtual machines with a single module or resource block.

Now is validation part of this

variable "virtual_machines" {
  description = "virtual_machines"
  type        = list(object({
    name  = string
    size  = string
    image = string
  }))
  default     = []
  validation {
    condition     = can(regex("^(Standard_DS3_v2|Standard_DS1_v2)$", var.virtual_machines[0].size))
    error_message = "Invalid input, options: \"Standard_DS3_v2\",\"Standard_DS1_v2\"."
  }
}
Enter fullscreen mode Exit fullscreen mode

This is an example of a Terraform variable definition for a list of objects called virtual_machines. Let's break it down:

  • description: This is an optional field that allows you to add a description of the variable for documentation purposes.

  • type: This defines the type of the variable, which in this case is a list of objects. The objects have three attributes, name, size, and image, each of which is a string.

  • default: This sets the default value for the variable, which in this case is an empty list.

  • validation: This allows you to add a validation condition to the variable. In this example, the condition argument uses the regex function to check that the size attribute of the first object in the list matches either "Standard_DS3_v2" or "Standard_DS1_v2". If the condition is not met, the error_message argument is used to generate an error message.

  • var.virtual_machines[0].size is an expression that refers to the size attribute of the first object in the virtual_machines list.
    In Terraform, the var prefix is used to reference a variable value. In this case, var.virtual_machines refers to the value of the virtual_machines variable, which is a list of objects.
    [0] is used to access the first object in the list. Since Terraform uses zero-based indexing, [0] refers to the first element of the list.
    Finally, .size is used to access the size attribute of the first object in the list.
    Overall, var.virtual_machines[0].size is used in the validation condition to check the size attribute of the first object in the list against the regular expression ^(Standard_DS3_v2|Standard_DS1_v2)$. If the size does not match either of these values, the validation condition will fail and an error message will be generated.

This variable definition is useful when you want to define a list of virtual machines with specific sizes and ensure that only the correct sizes are used. The validation argument helps to catch errors early by generating an error message if an invalid size is specified.

Top comments (0)