0
0
Terraformcloud~5 mins

Null values handling in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to leave a setting empty or optional in your infrastructure code. Terraform lets you use null values to skip or ignore those settings safely without errors.
When you want to create a resource but leave some optional fields empty.
When you want to conditionally include a value only if it exists.
When you want to avoid errors from missing or empty inputs in your configuration.
When you want to write reusable modules that accept optional parameters.
When you want to override a default value with no value at all.
Config File - main.tf
main.tf
variable "instance_name" {
  type    = string
  default = null
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  tags = {
    Name = var.instance_name != null ? var.instance_name : "default-instance"
  }
}

This Terraform file defines a variable instance_name that can be null. The AWS instance resource uses this variable for its tag Name. If instance_name is null, it uses the default name default-instance. This shows how to handle null values safely.

Commands
This command initializes the Terraform working directory. It downloads necessary provider plugins and prepares the environment.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized!
This command shows what Terraform will do before applying changes. It helps verify that null values are handled correctly and the resource will be created with the right tags.
Terminal
terraform plan
Expected OutputExpected
An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_instance.example will be created + resource "aws_instance" "example" { + ami = "ami-0c55b159cbfafe1f0" + arn = (known after apply) + id = (known after apply) + instance_type = "t2.micro" + tags = { + "Name" = "default-instance" } } Plan: 1 to add, 0 to change, 0 to destroy. ───────────────────────────────────────────────────────────────────────────── Note: You didn't specify an "-out" parameter to save this plan, so Terraform can't guarantee that exactly these actions will be performed if "terraform apply" is subsequently run.
This command applies the planned changes and creates the AWS instance. The -auto-approve flag skips manual confirmation for faster execution.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_instance.example: Creating... aws_instance.example: Still creating... [10s elapsed] aws_instance.example: Creation complete after 15s [id=i-0abcd1234efgh5678] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approve the apply step without prompting
This command shows any outputs defined in the Terraform configuration. Here it confirms the resource creation and values.
Terminal
terraform output
Expected OutputExpected
Key Concept

If you remember nothing else from this pattern, remember: use null to safely skip optional values and avoid errors in Terraform configurations.

Common Mistakes
Setting an empty string "" instead of null for optional variables.
Terraform treats empty strings as actual values, which may cause unexpected behavior or resource changes.
Use null to represent no value and handle it explicitly in your resource definitions.
Not checking for null before using a variable in resource attributes.
This can cause Terraform to fail or apply incorrect values if the variable is null.
Use conditional expressions like var.name != null ? var.name : "default" to handle null safely.
Summary
Define variables with default = null to make them optional.
Use conditional expressions to check for null before using variables.
Run terraform init, plan, and apply to create resources with optional values handled safely.