This is my existing terraform module code for AWS Batch resources:
resource "aws_batch_compute_environment" "compute_environment" {
count = var.create_compute_environment ? 1 : 0
...
...
}
resource "aws_batch_job_queue" "job_queue" {
count = var.create_batch_job_queue ? 1 : 0
...
compute_environments = length(var.batch_job_queue_ce) == 0 ? [aws_batch_compute_environment.compute_environment[0].arn] : var.batch_job_queue_ce
}
This allows me to create an AWS Batch Compute environment and a Job queue. If my job queue has just 1 compute environment to be associated with, I don't have to define anything in the calling module and it associates with the corresponding compute environment created as part of the module. If there are more than 1, I pass a list of those in the variable batch_job_queue_ce.
Now, I'm trying to replace the compute_environments argument with the compute_environment_order as it is deprecated. Read here
I'm trying to add a dynamic block for compute_environment_order in my module:
resource "aws_batch_job_queue" "job_queue" {
count = var.create_batch_job_queue ? 1 : 0
...
dynamic "compute_environment_order" {
for_each = var.compute_environment
content {
order = compute_environment_order.value.order == "" ? 0 : compute_environment_order.value.order
compute_environment = compute_environment_order.value.compute_environment == "" ? aws_batch_compute_environment.analytics_platform_ce[0].arn : compute_environment_order.value.compute_environment
}
}
}
variable "compute_environment" {
description = "Compute environments to be associated with the job queue"
type = list(object({
order = number
compute_environment = string
}))
default = []
}
What I now want is to not define anything related to this dynamic block in the calling module, like it was the case earlier, if there is just 1 compute environment to be associated with the job queue, again as was the condition earlier.
In my calling submodule, if I don't pass anything for compute_environment variable or if I just pass compute_environment = [], it fails with the error:
│ Exactly one of these attributes must be configured: │ [compute_environments,compute_environment_order]
If I define a value like this in my calling module:
compute_environment = [
{
order = 0
compute_environment = module.dev_dsm_batch_sandbox.compute_environment_arn[0]
}
]
it works fine, but I would like to avoid doing this, for the reasons explained above.
I think I'm missing a point here regarding the use of dynamic blocks for such a use case. Would appreciate pointing me in the right direction.
compute_environment_order.value.order == ""in your conditional expression will always returnfalse, because""(empty string) can never be equal to a value ofnumbertype.