DEV Community

Marcin Piczkowski
Marcin Piczkowski

Posted on • Updated on

Terraform 0.12 "is empty tuple" error in module

I'm struggling to create a module with optional resources steered with "count" attribute.

Here is a project which demonstrates the problem.

There is an SQS module which has optional dead-letter queue attached with SQS redrive policy.

On first run when queue is created with DLQ everything is fine, but when I wanted to remove DLQ the issue appears when terraform plan is running:

Error: Invalid index

  on modules/sqs/sqs.tf line 67, in resource "aws_sqs_queue" "regular_queue_with_dl":
  67:   redrive_policy             = var.attach_dead_letter_config ? data.template_file.regular_queue_redrive_policy[count.index].rendered : null
    |----------------
    | count.index is 0
    | data.template_file.regular_queue_redrive_policy is empty tuple

Does anyone know how to properly create such module with optional resource in Terraform 0.12 or fix the issue I am facing?


EDIT: I asked the same question on stackoverflow and got the answer which solved my problem.

The solution was to conditionally assign redrive_policy to null if Lamdba should not use it, e.g.:

resource "aws_sqs_queue" "regular_queue" {
  redrive_policy = var.attach_dead_letter_config ? templatefile(
    "${path.module}/redrive_policy.tmpl", {
      # (...whatever you had in "vars" in the template_file data resource...)  
    },
  ) : null
...
}

In addition we can inline template file instead of creating a separate resource.

My updated example is here

Top comments (0)