Luis
Luis

Reputation: 83

Terraform Conditional Attribute

Sorta stuck on this, I am trying to use the RDS module terraform-aws-modules.

And I want the end user to be able to pass along var.db_params or var.db_options to options = [] or parameters attribute, but if they don't pass anything then just default to null. This errors because the module is trying to do a for_each on "" or null which it can't.

Is there a way not to include the attributes for such if its null?

module "rds" {
  source = "terraform-aws-modules/rds/aws"

  identifier = var.name
  engine     = var.db_engine
  options    = var.db_options
  parameters = var.db_params
  # .....(removing all other code)
}

variable "db_params" {
  default = null
}

variable "db_options" {
  default = null
}

because the default is null i get:

Error: Invalid dynamic for_each value

var.options is ""
Cannot use a string value for_each. An iterable collection is required. 

Sorry for the short code, but i cannot copy paste from this workspace.

Upvotes: 1

Views: 1191

Answers (1)

Mark B
Mark B

Reputation: 200486

Instead of passing an empty string, or null, you should be passing an empty list:

variable "db_params" {
  default = []
}

variable "db_options" {
  default = []
}

Upvotes: 1

Related Questions