Search code examples
amazon-web-servicesterraformterraform-provider-aws

Specify an argument for one resource create by a loop - Terraform


It's possible to use a variable to add a non-default argument for a resource that is being created with a count loop?

For example:

resource "resource" "example" {
   count = length(var.resources)
   name = var.resources[count.index]
}

Where var.resources is:

["X", "Y", ...]

And now, I have the necessity to add and specific argument for one of this resources.

Resource "Y" will need a specific argument like " description = example"

It's possible? which is the best option? Use for_each instead of count and map var?


Solution

  • You could reorganize your var.resources into a map:

    
    variable "resources" {
      default = {
           "X" = {}
           "Y" = {description = "x description"}
      } 
    }
    

    then:

    resource "resource" "example" {
       for_each    = var.resources
       name        = each.key
       description = lookup(each.value, "description", null)
    }