0
0
Terraformcloud~5 mins

Object type definition in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to group related settings together in Terraform. Object type lets you define a fixed structure with named fields. This helps keep your configuration clear and organized.
When you want to pass multiple related values as one input to a module.
When you want to define a variable with specific named properties.
When you want to output a structured set of values from your Terraform code.
When you want to validate that input values have the right shape and types.
When you want to avoid errors by enforcing a fixed format for complex data.
Config File - main.tf
main.tf
variable "server_config" {
  type = object({
    name    = string
    cpu     = number
    memory  = number
    enabled = bool
  })
  default = {
    name    = "my-server"
    cpu     = 2
    memory  = 4096
    enabled = true
  }
}

output "server_name" {
  value = var.server_config.name
}

This file defines a variable named server_config with an object type. The object has four fields: name (text), cpu (number), memory (number), and enabled (true/false).

The default block sets example values for these fields.

The output server_name shows how to access a field from the object.

Commands
This command sets up Terraform in the current folder. It downloads any needed plugins and prepares to run your configuration.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/random... - Installing hashicorp/random v3.4.3... - Installed hashicorp/random v3.4.3 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This checks your Terraform files for syntax errors and type mistakes before applying changes.
Terminal
terraform validate
Expected OutputExpected
Success! The configuration is valid.
This command applies your Terraform configuration to create or update infrastructure. The flag skips the confirmation prompt.
Terminal
terraform apply -auto-approve
Expected OutputExpected
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # null_resource.example will be created + resource "null_resource" "example" { + id = (known after apply) } Plan: 1 to add, 0 to change, 0 to destroy. null_resource.example: Creating... null_resource.example: Creation complete after 0s [id=1234567890] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skip manual approval prompt
This shows the value of the output named server_name, which reads the name field from the object variable.
Terminal
terraform output server_name
Expected OutputExpected
my-server
Key Concept

If you remember nothing else from this pattern, remember: object type lets you group related values with fixed names and types for clearer, safer Terraform code.

Common Mistakes
Defining an object variable but missing a required field in the input.
Terraform will error because the object expects all fields to be present and correctly typed.
Always provide all fields defined in the object type with the correct data types.
Using a map or list instead of an object when fixed field names are needed.
Maps and lists do not enforce field names, so you lose structure and safety.
Use object type when you want fixed named fields with specific types.
Summary
Define an object type variable with named fields and their types.
Initialize Terraform and validate your configuration to catch errors early.
Apply the configuration to create or update infrastructure.
Use outputs to access specific fields from the object variable.