Home > Net >  Terraform for_each argument is unsuitable error
Terraform for_each argument is unsuitable error

Time:08-20

I'm trying to get my terraform for perform a for_each argument, but getting the below error;

The given "for_each" argument value is unsuitable: the "for_each" argument must be a map, or set of strings, and you have provided a value of type tuple.

My code looks like;

sns.tf

resource "aws_sns_topic" "jobs-sns" {
  for_each                          = local.sns-topic
  sqs_success_feedback_sample_rate  = "100"
}

locals.tf

locals {
  sns-topic = [
    "jobs-sns00",
    "jobs-sns01", 
    "jobs-sns02",  
    "jobs-sns03",
    "jobs-sns04"
  ]

Almost there with the code, feels like a real simple thing I'm missing

Thanks

CodePudding user response:

There are two options. The first one is probably the one to make your code work faster. You would only need to do the following:

resource "aws_sns_topic" "jobs-sns" {
  for_each                          = toset(local.sns-topic)
  sqs_success_feedback_sample_rate  = "100"
}

Note that the built-in toset function is used [1] (set mentioned in the error). Alternatively, you could change your local variable a bit. Instead of this:

locals {
  sns-topic = [
    "jobs-sns00",
    "jobs-sns01", 
    "jobs-sns02",  
    "jobs-sns03",
    "jobs-sns04"
  ]
}

You would do something like (map mentioned in the error):

locals {
  sns-topic = {
    topic0 = "jobs-sns00",
    topic1 = "jobs-sns01", 
    topic2 = "jobs-sns02",  
    topic3 = "jobs-sns03",
    topic4 = "jobs-sns04"
  }

This is because the for_each meta-argument [1] is working with variables which are key value pairs. The one you have is a list of values. Make sure to understand how for_each works before using it.


[1] https://www.terraform.io/language/functions/toset

[2] https://www.terraform.io/language/meta-arguments/for_each

CodePudding user response:

I ended up changing the for_each in the sns.tf to;

for_each = toset( ["jobs-sns00", "jobs-sns01", "jobs-sns03", "jobs-sns04"] )

Worked ok and picked up the correct values

  • Related