DEV Community

Marcin Piczkowski
Marcin Piczkowski

Posted on • Edited on

1

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

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more