- Newest
- Most votes
- Most comments
Hi
unfortunately, Terraform currently doesn't support using variable values directly in the source argument of a module block. This means you cannot reference variables within the source definition.
Terraform needs to determine the module source location during the terraform init phase. This stage happens before variables are defined or resolved. Since variables aren't known at this point, they cannot be used in the source path.
There are a few workarounds to achieve a similar outcome:
Conditional Directories: Structure your modules into subdirectories based on the desired source. Use a variable to control which subdirectory the calling module references. External Script:
Create an external script (e.g., bash or Python) that interacts with the source repository (like Git) based on a variable value. Within your Terraform code, call the script to dynamically set the module source path before Terraform uses it. Pre-configuration:
If your use case involves a limited number of source options, consider pre-configuring the source path based on the environment (e.g., dev, staging, prod) using different Terraform configurations or environment variables.
For more follow the link
Relevant content
- asked 3 years ago
- asked 2 years ago
- AWS OFFICIALUpdated 2 years ago

Thank You very much