How to fix Error: Error creating Secrets Manager Secret: ResourceExistsException in Terraform

TerraformINTERMEDIATEHIGHUpdated Dec 30, 2025· 171 views

This error occurs when Terraform attempts to create an AWS Secrets Manager secret with a name that already exists or is marked for deletion. AWS keeps deleted secrets in a recovery window (default 30 days) during which the name cannot be reused.

Quick fix

Fixes Error: Error creating Secrets Manager Secret: ResourceExistsException

aws secretsmanager list-secrets --include-planned-deletion --region us-east-1
  1. Check if the secret exists in AWS recovery
  2. Force delete the secret immediately
  3. Set recovery_window_in_days = 0 in Terraform
  4. Import the existing secret into Terraform state
terraform — error
aws secretsmanager list-secrets --include-planned-deletion --region us-east-1
Error: Error creating Secrets Manager Secret: ResourceExistsException

What this error means

AWS Secrets Manager prevents secret name reuse to protect against accidental overwrites. When you run 'terraform destroy' followed by 'terraform apply', the secret is not immediately deleted—it's marked for deletion during a recovery period. Attempting to create a secret with the same name during this period triggers a ResourceExistsException. This is a safety mechanism that prevents concurrent access or accidental data loss. The error can also occur if the secret was created manually in the AWS console or through another Terraform configuration, and Terraform's state file doesn't know about it.

Typical symptoms

  • Terraform apply fails with 'ResourceExistsException' error
  • Error occurs immediately after destroying and re-creating the same secret
  • Manually created secrets cause conflicts when using Terraform
  • CI/CD pipelines fail when deploying to multiple environments
  • Secret exists in AWS console but not in Terraform state file

Common causes

  1. Secret was destroyed recently and is still in AWS recovery window (default 30 days)
  2. Secret was created manually in AWS console, not via Terraform
  3. Another Terraform workspace or configuration created the secret
  4. Multiple pipelines or developers attempting to create the same secret simultaneously
  5. Terraform state was deleted or lost, causing Terraform to lose track of existing secrets

How to fix "Error: Error creating Secrets Manager Secret: ResourceExistsException"

1Check if the secret exists in AWS recovery

Use the AWS CLI to list all secrets including those marked for deletion:

bash
aws secretsmanager list-secrets --include-planned-deletion --region us-east-1

Look for your secret name in the output. If it appears with a "DeletedDate" field, it's in the recovery period.

2Force delete the secret immediately

If the secret is in recovery and you want to recreate it, force-delete it without the recovery window:

bash
aws secretsmanager delete-secret --secret-id YOUR_SECRET_NAME --force-delete-without-recovery --region us-east-1

This immediately removes the secret, allowing Terraform to create a new one with the same name.

3Set recovery_window_in_days = 0 in Terraform

Update your Terraform configuration to disable the recovery window for this secret:

hcl
resource "aws_secretsmanager_secret" "example" {
  name                    = "my-secret"
  recovery_window_in_days = 0
}

This setting forces immediate deletion without a recovery period, preventing this error in the future when you destroy and recreate the secret.

4Import the existing secret into Terraform state

If the secret already exists in AWS but not in your Terraform state, import it:

bash
terraform import aws_secretsmanager_secret.example arn:aws:secretsmanager:us-east-1:123456789012:secret:my-secret-AbCdE

Replace the ARN with your actual secret ARN (available from the AWS console or aws secretsmanager describe-secret command). This brings the existing resource under Terraform management.

5Use name_prefix instead of name

For secrets that need to be recreated frequently, consider using name_prefix instead of hardcoding name:

hcl
resource "aws_secretsmanager_secret" "example" {
  name_prefix = "my-secret-"
}

Terraform will generate unique names like "my-secret-abc123xyz", avoiding conflicts with pending-deletion secrets.

Advanced notes

AWS Secrets Manager enforces name uniqueness at the account and region level. The default recovery window is 30 days, but you can configure it between 7-30 days or set it to 0 for immediate deletion. In production environments, be cautious with recovery_window_in_days = 0 as it prevents recovery of accidentally deleted secrets. Consider using separate AWS accounts or regions for different environments to isolate secrets and avoid conflicts. For CI/CD pipelines, ensure only one deployment process manages each secret to prevent race conditions.

Frequently asked questions

What does "Error: Error creating Secrets Manager Secret: ResourceExistsException" mean?
AWS Secrets Manager prevents secret name reuse to protect against accidental overwrites. When you run 'terraform destroy' followed by 'terraform apply', the secret is not immediately deleted—it's marked for deletion during a recovery period. Attempting to create a secret with the same name during this period triggers a ResourceExistsException.
How do I fix "Error: Error creating Secrets Manager Secret: ResourceExistsException"?
This error occurs when Terraform attempts to create an AWS Secrets Manager secret with a name that already exists or is marked for deletion. AWS keeps deleted secrets in a recovery window (default 30 days) during which the name cannot be reused.
What causes "Error: Error creating Secrets Manager Secret: ResourceExistsException"?
Secret was destroyed recently and is still in AWS recovery window (default 30 days) Secret was created manually in AWS console, not via Terraform Another Terraform workspace or configuration created the secret
Is the "Error: Error creating Secrets Manager Secret: ResourceExistsException" 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