Reputation: 477
I've got the following variables defined:
variable "animals" {
description = "Animal S3 buckets"
default = {
cat = {
name = "cat-logs",
description = "Logs for cat bucket"
},
dog = {
name = "config-logs",
description = "Logs for dog bucket"
},
mouse = {
name = "mouse-logs",
description = "Logs for mouse bucket"
},
fish = {
name = "fish-logs",
description = "Logs for fish bucket"
}
}
#Queue
resource "aws_sqs_queue" "animal_queues" {
for_each = var.animals
name = "${each.value.name}"
redrive_policy = jsonencode({
deadLetterTargetArn = aws_sqs_queue.s3_replication_dl[each.key].arn
maxReceiveCount = 50
})
}
#DL Queue
resource "aws_sqs_queue" "animal_dl" {
for_each = var.animals
name = "${each.value.name}-dl"
}
I will say that I've run this successfully before when only "cat" and "dog" were in the variables. Currently, there is an sqs queue for cat and dog. Since then, I've added two new elements: "mouse" and "fish"
When running the code now, whether it is trying to apply or destroy, I get the following error:
Error: Invalid index
on main.tf line 303, in resource "aws_sqs_queue" "animal_queues":
303: deadLetterTargetArn = aws_sqs_queue.animal_dl[each.key].arn
|----------------
| aws_sqs_queue.animal_dl is object with 2 attributes
| each.key is "mouse"
| each.key is "fish"
The given key does not identify an element in this collection value.
Not sure what went wrong here as it worked before. Any guidance is much appreciated.
Upvotes: 0
Views: 717
Reputation: 728
I was able to reproduce the error & I was able to fix it ultimately by changing this line, deadLetterTargetArn = aws_sqs_queue.s3_replication_dl[each.key].arn
to deadLetterTargetArn = join("",["aws_sqs_queue.s3_replication_dl",each.key,".arn"])
I tested using Terraform v0.15.5 The join function was introduced in v0.12.0
This is the output I get once I used the join function... lmk if this is what you're expecting;
Terraform will perform the following actions:
# aws_sqs_queue.animal_queues["cat"] will be created
+ resource "aws_sqs_queue" "animal_queues" {
+ arn = (known after apply)
+ content_based_deduplication = false
+ deduplication_scope = (known after apply)
+ delay_seconds = 0
+ fifo_queue = false
+ fifo_throughput_limit = (known after apply)
+ id = (known after apply)
+ kms_data_key_reuse_period_seconds = (known after apply)
+ max_message_size = 262144
+ message_retention_seconds = 345600
+ name = "cat-logs"
+ name_prefix = (known after apply)
+ policy = (known after apply)
+ receive_wait_time_seconds = 0
+ redrive_policy = jsonencode(
{
+ deadLetterTargetArn = "aws_sqs_queue.s3_replication_dlcat.arn"
+ maxReceiveCount = 50
}
)
+ tags_all = (known after apply)
+ url = (known after apply)
+ visibility_timeout_seconds = 30
}
# aws_sqs_queue.animal_queues["dog"] will be created
+ resource "aws_sqs_queue" "animal_queues" {
+ arn = (known after apply)
+ content_based_deduplication = false
+ deduplication_scope = (known after apply)
+ delay_seconds = 0
+ fifo_queue = false
+ fifo_throughput_limit = (known after apply)
+ id = (known after apply)
+ kms_data_key_reuse_period_seconds = (known after apply)
+ max_message_size = 262144
+ message_retention_seconds = 345600
+ name = "config-logs"
+ name_prefix = (known after apply)
+ policy = (known after apply)
+ receive_wait_time_seconds = 0
+ redrive_policy = jsonencode(
{
+ deadLetterTargetArn = "aws_sqs_queue.s3_replication_dldog.arn"
+ maxReceiveCount = 50
}
)
+ tags_all = (known after apply)
+ url = (known after apply)
+ visibility_timeout_seconds = 30
}
# aws_sqs_queue.animal_queues["fish"] will be created
+ resource "aws_sqs_queue" "animal_queues" {
+ arn = (known after apply)
+ content_based_deduplication = false
+ deduplication_scope = (known after apply)
+ delay_seconds = 0
+ fifo_queue = false
+ fifo_throughput_limit = (known after apply)
+ id = (known after apply)
+ kms_data_key_reuse_period_seconds = (known after apply)
+ max_message_size = 262144
+ message_retention_seconds = 345600
+ name = "fish-logs"
+ name_prefix = (known after apply)
+ policy = (known after apply)
+ receive_wait_time_seconds = 0
+ redrive_policy = jsonencode(
{
+ deadLetterTargetArn = "aws_sqs_queue.s3_replication_dlfish.arn"
+ maxReceiveCount = 50
}
)
+ tags_all = (known after apply)
+ url = (known after apply)
+ visibility_timeout_seconds = 30
}
# aws_sqs_queue.animal_queues["mouse"] will be created
+ resource "aws_sqs_queue" "animal_queues" {
+ arn = (known after apply)
+ content_based_deduplication = false
+ deduplication_scope = (known after apply)
+ delay_seconds = 0
+ fifo_queue = false
+ fifo_throughput_limit = (known after apply)
+ id = (known after apply)
+ kms_data_key_reuse_period_seconds = (known after apply)
+ max_message_size = 262144
+ message_retention_seconds = 345600
+ name = "mouse-logs"
+ name_prefix = (known after apply)
+ policy = (known after apply)
+ receive_wait_time_seconds = 0
+ redrive_policy = jsonencode(
{
+ deadLetterTargetArn = "aws_sqs_queue.s3_replication_dlmouse.arn"
+ maxReceiveCount = 50
}
)
+ tags_all = (known after apply)
+ url = (known after apply)
+ visibility_timeout_seconds = 30
}
Plan: 4 to add, 0 to change, 0 to destroy.
Upvotes: 1