How to fix Error: Error creating Backup Plan: AlreadyExistsException in Terraform
The AlreadyExistsException error occurs when Terraform tries to create an AWS Backup Plan that already exists in your AWS account. This happens when the resource exists outside of Terraform state or wasn't properly imported.
Quick fix
Fixes Error: Error creating Backup Plan: AlreadyExistsException
aws backup list-backup-plans --region us-east-1- Verify the backup plan exists in AWS
- Import the existing backup plan into Terraform state
- Delete the existing backup plan if it should not exist
- Use a unique backup plan name to avoid conflicts
aws backup list-backup-plans --region us-east-1Error: Error creating Backup Plan: AlreadyExistsException
What this error means
When you run `terraform apply`, AWS Backup returns an `AlreadyExistsException` error, which means Terraform is attempting to create a backup plan with a name that already exists in your AWS account. This typically indicates a mismatch between your Terraform configuration and the actual state of resources in AWS. The root cause is usually that the backup plan was created outside of Terraform (manually via the AWS Console, AWS CLI, or by another automation tool), but your Terraform configuration doesn't know about it. Terraform's state file doesn't contain a reference to this existing resource, so it tries to create it again, triggering the error. In some cases, this can also occur if a previous Terraform apply partially failed, leaving the resource in AWS but not properly recorded in your state.
Typical symptoms
- Terraform apply fails immediately when creating aws_backup_plan resource
- Error message contains 'AlreadyExistsException' or 'already exists'
- Manual verification confirms the backup plan exists in AWS Console
- Problem persists even after retrying terraform apply
Common causes
- Backup plan was created manually in AWS Console or via AWS CLI outside of Terraform
- Another automation tool or team member created the backup plan concurrently
- Previous Terraform apply partially failed, leaving the resource in AWS but not in state
- Terraform configuration was imported from another project with the same backup plan name
How to fix "Error: Error creating Backup Plan: AlreadyExistsException"
Use the AWS CLI to list existing backup plans and confirm the name:
aws backup list-backup-plans --region us-east-1Look for your backup plan name in the output. This confirms the resource exists in AWS but is not in Terraform state.
If you want to manage the existing backup plan with Terraform, import it first. Get the backup plan ID from the AWS Console or CLI output, then run:
terraform import aws_backup_plan.example <backup-plan-id>Replace example with your resource name in Terraform and <backup-plan-id> with the actual ID from AWS. This adds the resource to your state without modifying it.
If the backup plan was created by mistake and shouldn't exist, delete it via the AWS Console or CLI before running Terraform again:
aws backup delete-backup-plan --backup-plan-id <backup-plan-id>Wait a moment for the deletion to complete, then run terraform apply to create it via Terraform.
If you want to create a different backup plan, change the name attribute in your Terraform configuration to something unique:
resource "aws_backup_plan" "example" {
name = "unique-backup-plan-name-v2"
rule {
rule_name = "example-rule"
target_vault_name = aws_backup_vault.example.name
schedule = "cron(0 12 * * ? *)"
}
}Make sure the new name doesn't conflict with any existing backup plans.
Search your Terraform code for duplicate aws_backup_plan resources with the same name:
grep -r "aws_backup_plan" .If you find multiple definitions with the same name or one that creates the same backup plan, remove or rename one of them.
Sometimes the state file gets out of sync with AWS. Refresh your state to detect the existing resource:
terraform refreshThis updates your state file without making any changes. Check if the backup plan is now recognized. If it is, you may need to import it or adjust your configuration.
Advanced notes
If you're using Terraform workspaces or multiple state files, ensure you're working in the correct workspace. Different workspaces maintain separate state files, so a backup plan created in one workspace won't conflict with another.
When working with auto-scaling groups or other resources that create backup plans automatically, be cautious about naming conventions. Some AWS services may create backup plans with predictable names that could conflict with your Terraform-managed resources.
For production environments, consider implementing resource naming conventions and using Terraform modules to enforce consistent naming patterns across your infrastructure.
Frequently asked questions
- What does "Error: Error creating Backup Plan: AlreadyExistsException" mean?
- When you run terraform apply, AWS Backup returns an AlreadyExistsException error, which means Terraform is attempting to create a backup plan with a name that already exists in your AWS account. This typically indicates a mismatch between your Terraform configuration and the actual state of resources in AWS. The root cause is usually that the backup plan was created outside of Terraform (manually via the AWS Console, AWS CLI, or by another automation tool), but your Terraform configuration doesn't know about it.
- How do I fix "Error: Error creating Backup Plan: AlreadyExistsException"?
- The AlreadyExistsException error occurs when Terraform tries to create an AWS Backup Plan that already exists in your AWS account. This happens when the resource exists outside of Terraform state or wasn't properly imported.
- What causes "Error: Error creating Backup Plan: AlreadyExistsException"?
- Backup plan was created manually in AWS Console or via AWS CLI outside of Terraform Another automation tool or team member created the backup plan concurrently Previous Terraform apply partially failed, leaving the resource in AWS but not in state
- Is the "Error: Error creating Backup Plan: AlreadyExistsException" error serious?
- It is a medium-severity error โ more of a blocker than a risk. The steps above resolve it.
Official resources & further reading
- ๐Terraform Official Documentation
- ๐AWS Backup Plan - Terraform Registry(Official Docs)
- ๐
- ๐
- ๐Terraform Troubleshooting Workflow Guide(Official Docs)