tleef
tleef

Reputation: 3594

Optional map as list

I'm working on a module that wraps the aws_dynamodb_table resource and passes through several of the arguments. I'm having issues with some of the optional blocks e.g. ttl. I've discovered that, despite only being able to be specified once, ttl is technically a list on the resource. However, since it can only be specified once, my module defines its ttl variable as a map. Additionally, ttl is optional on the resource so I would like it to be optional on my module as well.

This is what I have so far:

I define ttl as an optional map like so

variables.tf

... stuff ..
variable "ttl" {
  description = "(Optional) Defines ttl"
  type = "map"
  default = {}
}
... more stuff ...

I then assign it to the aws_dynamodb_table resource

main.tf

resource "aws_dynamodb_table" "default" {
  ... stuff ...
  ttl = "${length(keys(var.ttl)) > 0 ? list(var.ttl) : list()}"
  ... more stuff ...
}

With this approach I'm getting the following error:

conditional operator cannot be used with list values in:

${length(keys(var.ttl)) > 0 ? list(var.ttl) : list()}

Is this sort of thing impossible? It seems simple enough but I'm stumped.

Upvotes: 0

Views: 503

Answers (1)

iain
iain

Reputation: 39

I haven't tried the approach of a conditional operator for this. Instead I have the default set to false. This means if the user of the module doesn't pass any values for ttl then it won't be enabled.

The variable for the module looks like this:

variables.tf

variable "ttl_configuration" {
type = "map"
 default = {
   "attribute_name" = "TimeToExist"
   "enabled" = false
 }
}

Then inside the module assign this variable to ttl, ensuring to cast it as a list:

main.tf

resource "aws_dynamodb_table" "default" {
  ... stuff ...
  ttl = ["${var.ttl_configuration}"]
  ... more stuff ...
}

Another issue to note for TTL that I have just run into is changing the attribute_name from one attribute to another. Terraform doesn't let you just change it, so you will need to set the current one to false, then set it to true for your new one. This also means that with my approach above if you want to disable ttl on a table you must still pass the map in order to set the correct attribute name to false.

Upvotes: 1

Related Questions