How to fix Error: Error creating ELB: DuplicateLoadBalancerName in Terraform
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.
Quick fix
Fixes DuplicateLoadBalancerName
aws elb describe-load-balancers --load-balancer-names my-elb-name- Check if the load balancer exists in AWS
- Check Terraform state for the resource
- Import the existing load balancer (if missing from state)
- Delete the orphaned load balancer if import isn't needed
aws elb describe-load-balancers --load-balancer-names my-elb-nameError: Error creating ELB: DuplicateLoadBalancerName
What this error means
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.
Typical symptoms
- Terraform apply fails with 'DuplicateLoadBalancerName' error message
- The load balancer exists in AWS console but not in terraform state
- Error occurs when modifying load balancer configuration (subnets, listeners, etc.)
- Multiple environments or CI/CD runs conflict due to naming collision
- Terraform attempts to replace the load balancer but fails before completion
Common causes
- State file is out of sync - the ELB exists in AWS but Terraform state doesn't know about it
- Load balancer replacement ordering - Terraform tries to create new ELB before destroying the old one
- Multiple concurrent deployments using the same load balancer name
- Previous Terraform run failed after creating the ELB but before saving to state
- Manual deletion of load balancer from AWS console while Terraform still expects it
How to fix "Error: Error creating ELB: DuplicateLoadBalancerName"
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.
Advanced notes
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.
Frequently asked questions
- What does "DuplicateLoadBalancerName" mean?
- 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.
- How do I fix "DuplicateLoadBalancerName"?
- 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.
- What causes "DuplicateLoadBalancerName"?
- State file is out of sync - the ELB exists in AWS but Terraform state doesn't know about it Load balancer replacement ordering - Terraform tries to create new ELB before destroying the old one Multiple concurrent deployments using the same load balancer name
- Is the "DuplicateLoadBalancerName" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.
Official resources & further reading
- ๐Terraform Official Documentation
- ๐