We are getting very strange issue while using count in conjunction with Module. Below code doesn’t instantiate the module even if both the variable values are matching, we tried to assign static value i.e. count = 1, but in that case as well we don’t see the module getting instantiated. Please suggest what could be the possible fix.
Option 1:
module “mymodule” {
source = “./module/sample”
count = var.xyz == var.abc ? 1 : 0
}
Option 2:
module “mymodule” {
source = “./module/sample”
count = 1
}
Both of the above options doesn’t work for me for modules
Can you explain what you mean by the following:
Is this that it is not appearing as a resource to add when running a plan or apply, or that it is not provisioning the resources in the module, or something else?
Can you share your output from a plan/apply and can you share a more complete example than above?
Thanks
It does not get executed in plan/apply phases, resulting in no resource creation.
Not sure if there is any bug or any setting missing in TF package configuration
Without some further example of your Terraform code it is difficult to determine for sure. If you are able to most a minimum reproducible example and also the output of your plan command it may help.
However, two things immediately spring to mind:
- You are running your
terraform plan
/apply
from the incorrect folder/directory which contains no terraform files and therefore is treating it as an empty module with nothing to deploy.
or
- At some point you have already applied/deployed this successfully and the module/resources are in your state. Therefore, as Terraform determines there are no changes (or at least changes relevant to the module/resources in question) it will not show the module in a
plan
or apply
(Outside of during the state refresh progress lines). Perhaps run a terraform state list
to see if the module/resources are already in your state.
Official Terraform 1.8.5 Repo via JARaaS Hybrid RAG - Documentation - code current 6/17/2024
Note: Sources at the end of the response
It appears that you are facing an issue with the conditional instantiation of a module using the count
parameter in Terraform. This issue might stem from the way Terraform evaluates the condition and handles the count
argument. Here are some potential reasons and fixes for the problem you are experiencing:
Potential Issues:
- Variable Definition: Ensure that
var.xyz
and var.abc
are correctly defined and have the expected values.
- Parentheses Usage: Sometimes, Terraform might require explicit grouping using parentheses for condition expressions.
- Terraform Version: Compatibility issues might arise from different versions of Terraform. Ensure you are using a version that supports this syntax.
Solution Suggestions:
- Check Variable Definitions: Ensure that
var.xyz
and var.abc
are defined and their values are what you expect.
variable "xyz" {
description = "A description for xyz"
type = string
}
variable "abc" {
description = "A description for abc"
type = string
}
- Using Parentheses: Try adding parentheses around the condition in the
count
argument.
module "mymodule" {
source = "./module/sample"
count = (var.xyz == var.abc) ? 1 : 0
}
- Explicit Boolean Logic: Explicitly compare the values and ensure they are of a compatible type.
module "mymodule" {
source = "./module/sample"
count = (var.xyz == var.abc) ? 1 : 0
}
- Terraform Version: Ensure you are using a Terraform version that supports this syntax. Update Terraform to the latest version if necessary.
terraform -v
- Trial with Static Value – Confirm Module Instantiation: If
count = 1
does not work, there might be another issue preventing module instantiation.
Ensure that the module path and contents are correct.
module "mymodule" {
source = "./module/sample"
count = 1
}
- Debug and Log Output: Output the values of
var.xyz
and var.abc
for verification.
output "xyz_value" {
value = var.xyz
}
output "abc_value" {
value = var.abc
}
Verification:
- Run
terraform plan
to see the evaluated values and to understand what Terraform will apply.
- Check the debug logs if necessary using
TF_LOG=DEBUG terraform apply
.
Example Code for Fix Verification:
variable "xyz" {
description = "A description for xyz"
type = string
default = "value1"
}
variable "abc" {
description = "A description for abc"
type = string
default = "value1"
}
module "mymodule" {
source = "./module/sample"
count = (var.xyz == var.abc) ? 1 : 0
}
output "xyz_value" {
value = var.xyz
}
output "abc_value" {
value = var.abc
}
Sources:
Internal Document Reference:
- Terraform Module Instantiation: terraform_module_instantiation.md (internal document)
Try the suggestions above, and if the issue persists, please provide more details or logs for further troubleshooting.