This error occurs when Terraform attempts to create an Elastic Load Balancer that already exists in AWS, usually due to state file mismatches or resource replacement ordering issues. The load balancer may exist in AWS but not be tracked in your Terraform state.
The DuplicateLoadBalancerName error means AWS already has a load balancer with the name you're trying to create. This typically happens when there's a mismatch between your Terraform state file and the actual AWS resources. The error can occur in several scenarios: the ELB was created but the state file wasn't updated, Terraform is attempting to replace an ELB with the same name without destroying the old one first, or multiple deployments are trying to create the same load balancer.
First, verify that the load balancer actually exists in AWS. Open the AWS console, navigate to EC2 > Load Balancers, and search for the load balancer name mentioned in the error.
aws elb describe-load-balancers --load-balancer-names my-elb-nameIf the load balancer exists in AWS, proceed to the next steps. If it doesn't exist, the issue may be different.
Verify whether the load balancer is tracked in your Terraform state:
terraform state list | grep aws_elb
terraform state show aws_elb.my_elb_nameIf the resource is NOT in the state but EXISTS in AWS, the load balancer is orphaned and you need to import it.
If the load balancer exists in AWS but not in Terraform state, import it using the load balancer name:
terraform import aws_elb.my_elb_name my-elb-nameOr in Terraform v1.5.0+, use an import block in your configuration:
import {
to = aws_elb.my_elb_name
id = "my-elb-name"
}Then run terraform apply to finalize the import.
If you don't want to keep the existing load balancer, delete it from AWS and re-run Terraform:
aws elb delete-load-balancer --load-balancer-name my-elb-name
terraform applyBe cautious with this approach if the load balancer is serving production traffic.
If you're modifying the load balancer configuration and getting replacement errors, ensure Terraform destroys the old resource before creating the new one. Add a lifecycle block to your load balancer resource:
resource "aws_elb" "example" {
name = "my-elb"
# ... other configuration ...
lifecycle {
create_before_destroy = false
}
}This ensures the old load balancer is destroyed first before the new one is created, preventing name collisions.
If running multiple environments or CI/CD pipelines, use unique names for each load balancer to prevent conflicts:
resource "aws_elb" "example" {
name = "my-elb-${var.environment}"
# Or use a timestamp/random suffix
# name = "my-elb-${random_string.lb_suffix.result}"
}This prevents naming collisions when multiple deployments run simultaneously.
State file corruption or synchronization issues can cause this error. If the above steps don't resolve the issue, try refreshing the Terraform state: terraform refresh or terraform apply -refresh=true. This will sync your state file with the current AWS resources. In rare cases, you may need to manually edit the state file or use terraform state rm to remove conflicting resources, but this should only be done as a last resort. For Classic Load Balancers (ELB), AWS also enforces uniqueness at the region level, so the same name cannot exist twice in one region. For Application/Network Load Balancers (ALB/NLB), DNS names are automatically generated, but the load balancer name must still be unique.
Error: Error installing helm release: cannot re-use a name that is still in use
How to fix "release name in use" error in Terraform with Helm
Error: Error creating GKE Cluster: BadRequest
BadRequest error creating GKE cluster in Terraform
Error: External program failed to produce valid JSON
External program failed to produce valid JSON
Error: Unsupported argument in child module call
How to fix "Unsupported argument in child module call" in Terraform
Error: network is unreachable
How to fix "network is unreachable" in Terraform