The "count" and "for_each" meta-arguments are mutually-exclusive, only one should be used to be explicit about the number of resources to be created

Resource code:

resource "aws_kinesis_firehose_delivery_stream" "kinesis_firehose_to_s3" { 
    count = "${var.fh_destination == s3 ? 1 : 0}" 
    for_each = {for entity in var.entities : entity.name => entity} 
    name = test_firehose
}

Issue: I am not able to use the combination of both the below syntax in creating the terraform resource.. but separately working.

count = "${var.fh_destination == s3 ? 1 : 0}" 
for_each = {for entity in var.entities : entity.name => entity}

Requirements:

  1. It has to create this kinesis_firehose_to_s3 resource only when fh_destination= s3
count = "${var.fh_destination == s3 ? 1 : 0}
  1. It has to create this resource for each entity:
for_each = { for entity in var.entities : entity.name => entity}

Upvotes: 4

Views: 4936

Answers (1)

Martin Atkins
Martin Atkins

Reputation: 74064

I assume in your question when you said condition you really meant count, because condition is not a resource meta-argument in Terraform.

In your count expression you are choosing between having one instance or having zero instances based on some condition. In for_each we declare how many instances we want by changing how many elements there are in the for_each map, so the equivalent in for_each would be to choose between an empty map or a non-empty map depending on the same condition.

For example:

  for_each = (
    var.fh_destination == "s3" ?
    { for entity in var.entities : entity.name => entity } :
    {}
  )

With the above conditional expression, if var.fh_destination == "s3" then the result will have one element per element of var.entities, but otherwise the result will have zero elements and thus zero instances, creating the same result as the 0 arm of your conditional count.

Upvotes: 5

Related Questions