How to Leverage Dynamic Sources and Manage Deprecations in Terraform 1.15
Introduction
Terraform 1.15 introduces two powerful features: the ability to use variables in module sources with the new const attribute, and a formal way to deprecate module variables and outputs. This guide will walk you through implementing these features step by step, so you can make your infrastructure code more dynamic and maintainable.
What You Need
- Terraform 1.15 or later installed on your machine (check with
terraform version) - A basic Terraform project with at least one module (e.g., a root module calling a child module)
- Familiarity with Terraform variables, outputs, and module syntax
- A code editor of your choice
Step-by-Step Guide
Step 1: Declare a Const Variable
The const attribute is a new boolean flag that tells Terraform a variable can be resolved during terraform init. This enables using that variable in module source paths. Add a variable block like this in your root module:
variable "folder" {
type = string
const = true
}
Note: const cannot be combined with sensitive or ephemeral. If you try, Terraform will raise an error.
Step 2: Use the Const Variable in a Module Source
Now reference the variable in a module’s source argument. In your root module, call a module like this:
module "zoo" {
source = "./${var.folder}"
}
During terraform init, Terraform will resolve var.folder (if a value is provided) and use that path. This works with nested modules too – just ensure all intermediate variables have const = true.
If you accidentally reference a non-const variable or a local value in the source, Terraform will emit an error during init.
Step 3: Deprecate a Variable in a Module
When you need to phase out a variable, add the deprecated attribute with a friendly message. In your module’s main file (mod/main.tf):
variable "bad" {
deprecated = "Please use 'good' instead, this variable will be removed in a future release."
}
During terraform validate, any usage of var.bad will produce a warning diagnostic.
Step 4: Deprecate an Output in a Module
Similarly, deprecate an output by adding the deprecated attribute. In the same module file:
output "old" {
value = ...
deprecated = "Please use 'new' instead, this output will be removed."
}
Any reference to this output in calling code will trigger a warning.
Step 5: Chain Deprecated Outputs (Advanced)
Terraform 1.15 allows you to use a deprecated output inside another output that is also deprecated without generating a duplicate warning. For example, in your root module:
module "myModule" {
source = "./mod"
}
output "ancient" {
value = module.myModule.old
deprecated = "Please stop using this entirely."
}
Only the ancient output will produce a warning when referenced. The inner deprecated output (old) does not emit a separate diagnostic because it’s consumed within a deprecated context. This gives you a clean deprecation path.
Step 6: Verify and Interpret Diagnostics
After setting up your deprecations, run terraform validate to see the warnings. Here’s what triggers diagnostics:
- Deprecated variable (root): Warning if a value is passed via CLI,
.tfvars, or environment variable. - Deprecated variable (module call): Warning when you pass a value to the deprecated variable in a module block.
- Deprecated output (direct reference): Warning when you reference the output (e.g.,
module.myModule.oldin locals or outputs). - Deprecated output (inside another deprecated output): Warning only from the outer deprecated output.
This helps module authors and consumers safely phase out old interfaces.
Conclusion and Tips
These features make Terraform modules more flexible and maintainable. Here are some tips for using them effectively:
- Use
constsparingly – Only mark variables that truly need to be known atinittime (e.g., module source paths). Overuse can make your configuration harder to change. - Provide clear deprecation messages – Include what replacement to use and when the removal will happen.
- Test deprecation detection – Run
terraform validateafter updating your module to ensure warnings appear as expected. - Chain deprecations gradually – Use the chaining behavior to avoid overwhelming users with duplicate warnings.
- Update your CI/CD – Consider treating deprecation warnings as actionable items to keep your codebase clean.
By following these steps, you can take full advantage of Terraform’s latest improvements and streamline your infrastructure-as-code workflows.
Related Articles
- Linux Kernel 7.1-rc3 Released: Larger Patches Becoming the 'New Normal'
- Upgrade Your Fedora Silverblue to Fedora 44: A Complete Migration Guide
- 10 Essential Revelations About the Cemu Linux Malware Breach
- DAMON Subsystem Sees Major Upgrades: Tiering, Transparent Huge Pages Among New Features Unveiled at Linux Summit
- Getting Started with gThumb 4.0: A Comprehensive Guide to the GTK4/Libadwaita Overhaul
- Upgrading Fedora Silverblue to Version 44: Your Step-by-Step Q&A Guide
- Everything You Need to Know About Ubuntu 26.04 LTS 'Resolute Raccoon'
- How to Optimize Vulkan Performance with NVIDIA's New Descriptor Heaps and Beta Drivers