Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform - resource repeated multiple times

I have a terraform plan which consists of multiple aws_sqs_queue resources which more of less share the same config. Before I remove any repeating configs and use variables I wanted to run terraform plan to see if it works. However I keep running into -

module root: 1 error(s) occurred:

* data.template_file.ep_match_result_queues: resource repeated multiple times

Ive tried googling but there is not much information available. Any help will be much appreciated. My plan looks like this: (ignore extra braces, typos as this is just a skeleton to give you a general idea how the plan is sturctured):

data "template_file" "ep_match_result_queues" {
  template = "${var.namespace}-sm-ep"
}

resource "aws_sns_topic" "sns_topic_name" {
  name            = "ep_sm_match_result_topic${var.environment}"
  display_name    = ""
  policy          = <<POLICY
{
 #policy
}


resource "aws_sqs_queue" "queue1" {
 #config
}

resource "aws_sqs_queue" "queue2" {
 #config

    redrive_policy             = <<POLICY
{
 #policy
}
POLICY
}

resource "aws_sqs_queue" "queue3" {
#config
}

resource "aws_sqs_queue" "queue4" {
 #config

    redrive_policy             = <<POLICY
{
 #policy
}
POLICY
}

resource "aws_sqs_queue" "queue5" {
 #config
}

resource "aws_sqs_queue" "queue6" {
  #config

    redrive_policy             = <<POLICY
{
 #policy
}
POLICY
}

resource "aws_sqs_queue" "queue7" {
 #config
}

resource "aws_sqs_queue" "queue8" {
  #config

    redrive_policy             = <<POLICY
{
 #policy
}
POLICY
}

resource "aws_sqs_queue_policy" "queue_policy" {
  queue_url = [ ... ]
  policy    = "${data.aws_iam_policy_document.match_result_queues_policy.json}"
}

data "aws_iam_policy_document" "match_result_queues_policy" {
 #policy
}
like image 396
letsc Avatar asked Mar 09 '23 22:03

letsc


2 Answers

I think that error means you have declared the resource with the same name more than once, that's in the same tf file or in another tf file in the same directory where you are running terraform.

like image 99
deobieta Avatar answered Mar 20 '23 14:03

deobieta


You can use a variable to specify how many you want and then add that to the name of the Queue.

resource "aws_sqs_queue" "terraform_queue" {
  count  = "${var.queue_count}"
  name                      = "terraform-example-queue-${count.index}"
  delay_seconds             = 90
  max_message_size          = 2048
  message_retention_seconds = 86400
  receive_wait_time_seconds = 10
  redrive_policy            = "{\"deadLetterTargetArn\":\"${aws_sqs_queue.terraform_queue_deadletter.arn}\",\"maxReceiveCount\":4}"
}

https://www.terraform.io/intro/examples/count.html

like image 42
strongjz Avatar answered Mar 20 '23 15:03

strongjz