0
0
Terraformcloud~5 mins

Optional attributes in objects in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to create a group of settings where some are needed and others are not. Optional attributes let you skip some settings without breaking your configuration.
When you want to define a resource or module input that can have extra settings but doesn't always need them.
When you want to make your Terraform code flexible for different environments with different needs.
When you want to avoid errors caused by missing values that are not always required.
When you want to provide default values but allow users to override them if needed.
When you want to simplify your configuration by not forcing all attributes to be set every time.
Config File - main.tf
main.tf
variable "server_config" {
  type = object({
    cpu    = number
    memory = number
    disk   = optional(number)
  })
  default = {
    cpu    = 2
    memory = 4096
  }
}

resource "null_resource" "example" {
  triggers = {
    cpu    = var.server_config.cpu
    memory = var.server_config.memory
    disk   = try(var.server_config.disk, 100)
  }
}

This file defines a variable named server_config as an object with three attributes: cpu and memory are required, while disk is optional.

The default value sets cpu and memory, but leaves out disk.

The resource uses these values, and for disk, it uses try() to provide a fallback value of 100 if disk is not set.

Commands
This command sets up Terraform in the current folder by downloading necessary plugins and preparing the environment.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/null... - Installing hashicorp/null v3.2.1... - Installed hashicorp/null v3.2.1 (signed by HashiCorp) Terraform has been successfully initialized!
This command checks if the Terraform files are correct and ready to be applied.
Terminal
terraform validate
Expected OutputExpected
Success! The configuration is valid.
This command creates or updates the infrastructure based on the configuration without asking for confirmation.
Terminal
terraform apply -auto-approve
Expected OutputExpected
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 to apply changes immediately
This command shows the outputs of the applied infrastructure, if any are defined.
Terminal
terraform output
Expected OutputExpected
No output (command runs silently)
Key Concept

If you remember nothing else from this pattern, remember: optional attributes let you leave out some settings safely while keeping others required.

Common Mistakes
Not marking an attribute as optional but then not providing it in the input.
Terraform will give an error because it expects that attribute to always be present.
Use the 'optional' keyword in the object type to allow skipping that attribute.
Forgetting to handle missing optional attributes when using them in resources.
Using an optional attribute directly without a fallback can cause errors if it is missing.
Use functions like 'try()' or 'lookup()' to provide default values when optional attributes are missing.
Summary
Define object variables with 'optional' attributes to allow flexible inputs.
Use 'try()' or similar functions to safely use optional attributes in resources.
Run 'terraform init' to prepare, 'terraform validate' to check, and 'terraform apply' to create resources.