The QueueNameExists error occurs when Terraform attempts to create an SQS queue with a name that already exists in your AWS account and region. SQS queue names must be unique within each account and region, so this error indicates either a previous queue creation, a recent deletion that is still cached, or a naming conflict with another service.
The QueueNameExists error means the SQS queue name you specified already exists in your AWS account within the same region. SQS queue names must be unique within each AWS account and region combination - you cannot have two queues with the same name in the same account and region. This constraint is enforced by the AWS SQS service and Terraform will fail resource creation when this condition is detected.
Check if the queue actually exists using the AWS CLI:
aws sqs list-queues --queue-name-prefix my-queue-name --region us-east-1Replace "my-queue-name" and "us-east-1" with your actual queue name and region. If the queue URL appears in the output, the queue exists.
If you own the queue and want Terraform to manage it, import it:
aws sqs get-queue-url --queue-name my-queue-name --region us-east-1Copy the QueueUrl from the output, then:
terraform import aws_sqs_queue.my_queue <queue-url>Replace "my_queue" with your resource name and use the actual queue URL.
Check if the queue is already tracked in your Terraform state:
terraform state list | grep sqs_queue
terraform state show aws_sqs_queue.my_queueIf the resource exists in state but references the wrong queue, remove and re-import it:
terraform state rm aws_sqs_queue.my_queue
terraform import aws_sqs_queue.my_queue <queue-url>Let Terraform generate a unique suffix for the queue name:
resource "aws_sqs_queue" "my_queue" {
name_prefix = "my-queue-"
}Terraform will create a queue like "my-queue-a1b2c3d4e5f6abcd". This avoids naming conflicts.
Make the queue name unique by including your AWS account ID or environment:
data "aws_caller_identity" "current" {}
resource "aws_sqs_queue" "my_queue" {
name = "my-queue-${data.aws_caller_identity.current.account_id}"
}Or include the environment:
resource "aws_sqs_queue" "my_queue" {
name = "my-queue-${var.environment}"
}If you no longer need the existing queue, delete it using the AWS CLI:
aws sqs delete-queue --queue-url <queue-url> --region us-east-1Then run Terraform apply:
terraform applyNote: AWS has a 60-second grace period after deletion before you can reuse the queue name.
If you just deleted a queue and are trying to recreate it with the same name, AWS requires a 60-second wait:
# Wait 60 seconds
sleep 60
# Then run Terraform apply
terraform applyThis grace period prevents accidental recreation issues and ensures consistency across AWS systems.
## SQS Queue Naming Rules
- Names must be 1-80 characters long
- Can contain lowercase letters, numbers, hyphens, and underscores
- FIFO queues must end with .fifo
- Names are case-sensitive
## 60-Second Grace Period
After deleting an SQS queue, its name is unavailable for 60 seconds. Attempting to recreate a queue with the same name within this window will fail with QueueNameExists. This is an AWS safety mechanism.
## Queue Deletion vs Queue Purge
- delete-queue: Removes the entire queue (frees up the name after 60s)
- purge-queue: Removes all messages but keeps the queue (name stays reserved)
If you only want to clear messages, use purge instead of delete.
## Regional Isolation
Queue names are unique per region per account. You can have a queue named "my-queue" in us-east-1 and a different queue with the same name in eu-west-1.
## Terraform Lifecycle Rules
For development/testing scenarios where you need to replace a queue:
resource "aws_sqs_queue" "my_queue" {
name = "my-queue"
lifecycle {
create_before_destroy = true
}
}This creates the new queue before destroying the old one, but you'll still need a unique name.
Error: Error rendering template: template not found
How to fix "template not found" error in Terraform
Error: Error generating private key
How to fix 'Error generating private key' in Terraform
Error creating Kubernetes Service: field is immutable
How to fix "field is immutable" errors in Terraform
Error: Error creating local file: open: permission denied
How to fix "Error creating local file: permission denied" in Terraform
Error: line endings have changed from CRLF to LF
Line endings have changed from CRLF to LF in Terraform