How to Leverage Dynamic Sources and Manage Deprecations in Terraform 1.15

By

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.

How to Leverage Dynamic Sources and Manage Deprecations in Terraform 1.15

What You Need

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:

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:

By following these steps, you can take full advantage of Terraform’s latest improvements and streamline your infrastructure-as-code workflows.

Tags:

Related Articles

Recommended

Discover More

Musk vs. Altman: Key Moments from Closing ArgumentsThe Hidden Cost of AI Efficiency: Are We Losing Team Cohesion?Fedora Hummingbird Launches as Rolling OS with Zero-CVE Container Approach10 Critical Insights on Automation in Modern Cybersecurity: Speed, AI, and Human DefendersUbuntu’s Runtime App Permissions: A Smarter Way to Control Access