The "defaults" function was removed in Terraform 1.3.0 as part of the optional attributes feature stabilization. This error occurs when using the deprecated function with newer Terraform versions. Replace it with native default value syntax in variable type definitions.
Terraform 1.3.0 de-experimentalized the optional attributes feature and removed the `defaults()` helper function. This function was used to apply default values to optional object attributes, but it's no longer available. The error indicates your Terraform code is using a function that no longer exists in the current version.
Find all variable definitions that use optional() type constraints:
grep -r "optional(" .This will show you where optional attributes are declared. The defaults() function was typically used to provide default values for these optional fields.
Instead of using defaults() in a locals block, specify defaults directly in the variable type definition:
Before (Terraform < 1.3):
variable "settings" {
type = object({
name = string
enabled = optional(bool)
})
}
locals {
settings = defaults(var.settings, {
enabled = true
})
}After (Terraform >= 1.3):
variable "settings" {
type = object({
name = string
enabled = optional(bool, true) # Default value here
})
default = null
}Now the default value is specified inline with the optional attribute.
Delete any locals blocks that use the defaults() function entirely, since the defaults are now in the variable definition:
Remove this:
locals {
# Delete this entire block - no longer needed!
irsa_config = defaults(var.irsa_config, {
create_kubernetes_namespace = true
namespace_name = "karpenter"
})
}The defaults are now applied automatically when the variable is declared with inline defaults.
If using third-party modules that still have defaults() calls, you have two options:
Option 1: Update the module to a newer version
module "eks_blueprints" {
source = "aws-ia/eks-blueprints/aws"
version = ">= 5.0" # Use latest version without defaults()
}Option 2: Fork and fix the module yourself
- Download the module source
- Update all defaults() calls to inline defaults
- Point your module source to your fork
- Submit upstream PR to help others
Verify your changes are syntactically correct:
terraform validateThis will catch any remaining issues with the function calls or syntax. If you get other errors, review the variable definitions for consistency.
Once validation passes, test your configuration:
# Reinitialize if module versions changed
terraform init -upgrade
# Plan the changes
terraform plan
# Apply if plan looks correct
terraform applyThe configuration should now work with Terraform 1.3.0+.
Terraform version compatibility:
- Terraform 0.15.x - 1.2.x: defaults() function available
- Terraform 1.3.0+: defaults() function removed (breaking change)
Multiple default values in nested objects:
For complex nested optional objects, you can provide defaults for multiple levels:
variable "config" {
type = object({
database = optional(object({
name = optional(string, "mydb")
port = optional(number, 5432)
enabled = optional(bool, true)
}))
})
}Using merge() as alternative:
If you have complex merging logic, use merge() function instead:
locals {
defaults = {
tag_env = "dev"
tag_team = "platform"
}
tags = merge(local.defaults, var.additional_tags)
}Migration checklist for modules:
- [ ] Update module source version in terraform configuration
- [ ] Run terraform get -update to download new versions
- [ ] Review any breaking changes in module changelog
- [ ] Test in development environment first
- [ ] Update documentation for your team
Error: Error rendering template: template not found
How to fix "template not found" error in Terraform
Error: Error generating private key
How to fix 'Error generating private key' in Terraform
Error creating Kubernetes Service: field is immutable
How to fix "field is immutable" errors in Terraform
Error: Error creating local file: open: permission denied
How to fix "Error creating local file: permission denied" in Terraform
Error: line endings have changed from CRLF to LF
Line endings have changed from CRLF to LF in Terraform