Skip to content

Variable in Terraform module source

0

Please tell me if it is possible to use the value of a variable in the source of the Terraform module? When I try to use the value of a variable in the module source, I get this error message: "Variables may not be used here. Unsuitable value: the value must be known".

Thank you in advance.

asked 2 years ago2.7K views
1 Answer
1

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

https://github.com/hashicorp/terraform/issues/23948

EXPERT
answered 2 years ago
AWS
EXPERT
reviewed 2 years ago
  • Thank You very much

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.