Mastering Terraform 1.15: A Step-by-Step Guide to Dynamic Sources and Deprecation Warnings

Introduction

Terraform 1.15 introduces two powerful features that streamline module management: dynamic module sources using the new const attribute and built-in deprecation warnings for variables and outputs. This guide walks you through each feature step by step, ensuring you can leverage them to write cleaner, more maintainable infrastructure code. By the end, you'll be able to define variables usable during terraform init, create flexible module sources, and gracefully deprecate module inputs or outputs without breaking existing configurations.

Mastering Terraform 1.15: A Step-by-Step Guide to Dynamic Sources and Deprecation Warnings

What You Need

Step-by-Step Instructions

Step 1: Define a const Variable

In Terraform 1.15, the const attribute marks a variable as available during terraform init. Create a variable block with const = true. Note that const cannot be used alongside sensitive or ephemeral.

variable "folder" {
  type  = string
  const = true
}

Save this in your root module’s variables.tf file.

Step 2: Use the const Variable in a Module Source

Now reference the const variable inside a module’s source argument. This allows the module source to be dynamic based on input provided at initialization.

module "zoo" {
  source = "./${var.folder}"
}

When you run terraform init, Terraform evaluates var.folder using the value you provide (via -var, environment variable, or default). The source path must exist.

Step 3: Extend Dynamic Sources to Nested Modules

The const attribute works recursively. To use a const variable inside a child module’s source, explicitly declare it with const = true in that module.

# child-module/variables.tf
variable "subfolder" {
  type    = string
  const   = true
}

# root/main.tf
module "child" {
  source = "./${var.folder}"
  subfolder = "animals"
}

Terraform will report an error during init if you attempt to use a non-const variable or any local value in a module source.

Step 4: Mark a Variable as Deprecated

To deprecate a variable, add the deprecated attribute with a descriptive message. This warns users who set a value for that variable.

# main.tf
variable "bad" {
  deprecated = "Please use 'good' instead. This variable will be removed in a future version."
}

Similarly, deprecate an output:

output "old" {
  value      = ...
  deprecated = "Please use 'new' instead. This output will be removed."
}

Save these in your module’s .tf files.

Step 5: Trigger Deprecation Warnings

When you use a deprecated variable or reference a deprecated output, Terraform emits a warning during validation (terraform validate) or plan. Create a file that consumes these deprecated items.

# main.tf
variable "root" {
  deprecated = "This should no longer be used."
}

module "myModule" {
  source = "./mod"
  bad    = "not good"    # passes value to deprecated variable
}

locals {
  moduleUsage = module.myModule.old  # references deprecated output
}

Run terraform validate. You will see warnings like:
Warning: Variable "bad" is deprecated (use 'good' instead)
Warning: Output "old" is deprecated (use 'new' instead)
Additionally, if var.root receives a value via CLI or environment, Terraform warns that this variable is deprecated.

Step 6: Gracefully Chain Deprecated Outputs

Module authors can allow deprecated outputs to be used inside other deprecated outputs without double-warnings. In the child module, mark the output old as deprecated. Then in the root module, define another deprecated output that references old.

# mod/main.tf
output "old" {
  value      = "some data"
  deprecated = "Use 'new' instead"
}

# main.tf
module "myModule" {
  source = "./mod"
}

output "ancient" {
  value      = module.myModule.old
  deprecated = "Please stop using this"
}

When you run terraform validate, only one warning appears: for the ancient output. The innermost deprecated item is not flagged again, allowing clean migration paths.

Tips

Tags:

Recommended

Discover More

Warning: Your 'Close' Button May Not Actually Stop Windows Programs – Here's WhyMedicare Part B Costs in 2026: What Retirees Need to KnowOptimizing Pull Request Performance at GitHub: A Q&A on the Files Changed TabInside V8: How JSON.stringify Got a 2x Speed BoostWindows 11 Gets Smarter, Faster, and Less Distracting: What You Need to Know