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.
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.
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.
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.
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