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!

The most suitable variable type for storing both a server name, which is a string, and memory size, which is a number, is the object type. An object in Terraform is a complex structure that allows you to define a collection of attributes, each of which can have different types. In this case, you can create an object that contains fields for both the server name and memory size, like so:


variable "server_config" {

type = object({

name = string

memory = number

})

}


Using an object allows you to group related data together in a cohesive way, making it easier to manage and reference. This is particularly beneficial when the attributes have different types, as in this instance.

In contrast, other types such as lists and maps have limitations that make them less suitable for this use case. A list can only store items of the same type in an ordered sequence, which would not accommodate the requirement for both a string and a number. A map stores key-value pairs where all values must be of the same type, but it is not as structured as an object for cases where you want to enforce specific types for different fields. Finally, a set is also not ideal since it
Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy