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.
What You Need
- Terraform 1.15 or later installed on your system. Verify with
terraform version. - Basic familiarity with Terraform modules, variables, and outputs.
- A project directory containing at least one root module and optionally a child module.
- A text editor (e.g., VS Code, Vim) to write
.tffiles.
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
- Use
constsparingly: Only mark variables that are truly needed to resolve module sources. Overuse may reduce flexibility in other commands. - Test deprecation messages: Write clear, actionable messages that tell users exactly what to use instead. Include version removal plans if possible.
- Combine with
terraform plan: Deprecation warnings also appear in plan output. Use them in CI/CD pipelines to catch lingering uses. - Gradual rollout: Introduce deprecation warnings in a minor module version before removing variables in a major release. This gives users time to adapt.
- Document the migration: Update your module’s README or changelog to describe deprecations and new alternatives.
- Anchor links: Use
idattributes on step headings for easy internal navigation (as shown above).
Related Articles
- AMD Expands HDMI 2.1 Capabilities: Display Stream Compression Hits AMDGPU Linux Driver
- Mastering Your System PATH: A Step-by-Step Guide to Adding Directories
- Sovereign Tech Fund Grants €1.28 Million to KDE for Plasma and Infrastructure Improvements
- Linux Weekly Roundup: Standardized Projects Folder, Firefox Ad Blocking, and Major Distro Releases
- Debian 14 'Forky' Enforces Reproducible Builds: Unprecedented Security Mandate Takes Effect
- Kubernetes v1.36 Graduates PSI Metrics to GA: Node, Pod, and Container Pressure Visibility Now Production-Ready
- Fedora 44 Arrives: GNOME 50, Plasma 6.6, and Enhanced Gaming
- HashiCorp Unveils Terraform Cost Analytics and Governance Upgrades in Latest Release