FG
☁️ Cloud & DevOps

terraform get: can't use variable in module source parameter?

Freshabout 19 hours ago
Mar 14, 20260 views
Confidence Score95%
95%

Problem

I'm trying to avoid hard-coding module sources; the simplest approach would be: [code block] The result I get while attempting to run `terraform get -update` is [code block]

Error Output

Error loading Terraform: Error downloading modules: error downloading module 'file:///home/thisisme/terraform-env/${var.foo_module_source}': source path error: stat /home/thisisme/terraform-env/${var.foo_mo

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Use Terraform locals for dynamic module source paths

Medium Risk

Terraform does not support variable interpolation in module source paths directly. When you attempt to use a variable in the source parameter, Terraform tries to resolve it as a literal path, leading to errors when it cannot find the specified path.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Define a local variable for the module source

    Create a local variable in your Terraform configuration that constructs the module source path using the variable you want to use. This allows you to dynamically set the source without hard-coding it.

    hcl
    locals {
      foo_module_source = "${var.foo_module_source}"
    }
  2. 2

    Reference the local variable in the module block

    Update your module block to use the local variable instead of the original variable directly. This ensures that Terraform can correctly resolve the path when it processes the module.

    hcl
    module "example" {
      source = local.foo_module_source
    }
  3. 3

    Run Terraform commands to apply changes

    After making the changes, run the Terraform commands to ensure that the configuration is applied correctly. Start by initializing the configuration and then apply it.

    bash
    terraform init
    terraform apply
  4. 4

    Verify module loading

    Check the output of the Terraform commands to confirm that the module loads without errors. Ensure that the module is correctly sourced from the specified path.

    bash
    terraform get -update

Validation

Confirm that the module loads successfully without any errors related to the source path. You can also check the Terraform state to ensure the module is correctly initialized.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

terraformiacawsenhancementthinkingconfig