Which variable type is most suitable for storing both a server name (string) and memory size (number) in Terraform?

Master the HashiCorp Terraform Test with these flashcards and multiple choice questions. Each question includes hints and explanations to prepare you effectively. Set yourself up for success!

Using an object as the variable type in Terraform is the most suitable choice for storing both a server name (as a string) and memory size (as a number). An object allows you to define a structured data type that can contain multiple key-value pairs, making it ideal for scenarios where you want to encapsulate different related attributes.

In this context, you can define an object with specific attributes for server name and memory size. For instance, you could define a variable like this:


variable "server_details" {

type = object({

name     = string

memory   = number

})

}

This structure maintains clarity and organization, ensuring that the server's properties are easily accessible and related to one another. Objects provide a way to group different types of data, facilitating better management of complex configurations in Terraform.

Using a set or a list would not be appropriate in this case because both of these types are designed for storing collections. A set contains unique values of the same type, and a list is an ordered collection of elements that can be of the same type, making them unsuitable for holding structured data with various attribute types. A map, while it can hold key-value pairs, doesn’t enforce the data types

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy