How to fix Error: Error creating CodeBuild Project: ResourceAlreadyExistsException in Terraform

TerraformINTERMEDIATEHIGHUpdated Dec 31, 2025ยท 95 views

This error occurs when Terraform attempts to create a CodeBuild project that already exists in your AWS account. It typically happens when there's a mismatch between Terraform state and actual AWS resources.

Quick fix

Fixes Error: Error creating CodeBuild Project: ResourceAlreadyExistsException

aws codebuild batch-get-projects --names your-project-name --region us-east-1
  1. Check if the CodeBuild project exists in AWS
  2. Import the existing resource into Terraform state
  3. Verify the imported resource matches your configuration
  4. Delete the existing AWS resource if you want to recreate it
terraform โ€” error
aws codebuild batch-get-projects --names your-project-name --region us-east-1
Error: Error creating CodeBuild Project: ResourceAlreadyExistsException

What this error means

The ResourceAlreadyExistsException error indicates that AWS CodeBuild is rejecting the creation of a project because a project with that name already exists in the region. This commonly happens in three scenarios: 1. **State mismatch**: The CodeBuild project was created outside of Terraform (manually via AWS Console, AWS CLI, or another tool), but Terraform's state file doesn't know about it. When Terraform tries to create what it thinks is a new resource, AWS rejects it because the name is taken. 2. **Orphaned state**: The resource exists in AWS but Terraform's state file was lost, deleted, or corrupted. Terraform sees no record of it and attempts to recreate it. 3. **Multiple Terraform configurations**: Different Terraform configurations or workspaces are trying to manage the same CodeBuild project, causing a naming conflict.

Typical symptoms

  • Terraform apply fails with ResourceAlreadyExistsException during aws_codebuild_project creation
  • The CodeBuild project actually exists in the AWS Console, but Terraform doesn't recognize it
  • terraform plan shows '+' (create) for a resource that already exists in AWS
  • The error message includes the CodeBuild project name that's causing the conflict

Common causes

  1. The CodeBuild project was created manually or by another process outside of this Terraform configuration
  2. Terraform state file was lost, deleted, or reset, causing Terraform to lose track of previously managed resources
  3. Multiple Terraform configurations or workspaces are attempting to manage the same CodeBuild project name
  4. The project name is hardcoded without unique identifiers, causing conflicts when scaling or managing multiple environments
  5. A previous terraform apply succeeded but wasn't recorded in the state file correctly

How to fix "Error: Error creating CodeBuild Project: ResourceAlreadyExistsException"

1Check if the CodeBuild project exists in AWS

First, verify that the project actually exists in your AWS account and region using the AWS CLI:

bash
aws codebuild batch-get-projects --names your-project-name --region us-east-1

If the project is listed in the output, it exists. If not, the issue may be a different error. Note the exact project name from the error message and use that in the command.

2Import the existing resource into Terraform state

If the project exists and you want to manage it with Terraform, import it into your Terraform state:

bash
terraform import aws_codebuild_project.my_project your-project-name

Replace my_project with the local resource name in your Terraform configuration and your-project-name with the actual AWS CodeBuild project name. After importing, Terraform will track this resource and won't try to recreate it.

3Verify the imported resource matches your configuration

After importing, run terraform plan to see if there are any differences:

bash
terraform plan

If Terraform wants to modify the imported resource to match your configuration, review the changes carefully. You may need to update your .tf files to match the existing AWS configuration, or adjust the AWS resource to match your Terraform code.

4Delete the existing AWS resource if you want to recreate it

If the existing CodeBuild project is orphaned or no longer needed, delete it from AWS and let Terraform recreate it:

bash
aws codebuild delete-project --name your-project-name --region us-east-1

After deletion, run terraform apply to create the project through Terraform. Only do this if you're sure the project isn't in use by other services.

5Use unique naming to prevent future conflicts

Update your Terraform configuration to use unique project names, especially if managing multiple environments or projects:

hcl
resource "aws_codebuild_project" "example" {
  name          = "${var.project_prefix}-${var.environment}-codebuild"
  service_role  = aws_iam_role.codebuild_role.arn

  artifacts {
    type = "NO_ARTIFACTS"
  }

  environment {
    compute_type                = "BUILD_GENERAL1_SMALL"
    image                      = "aws/codebuild/standard:5.0"
    type                       = "LINUX_CONTAINER"
    image_pull_credentials_type = "CODEBUILD"
  }

  source {
    type      = "GITHUB"
    location  = "https://github.com/owner/repo.git"
  }
}

Use variables for naming to avoid hardcoded conflicts across different deployments.

6Check for state file issues and refresh if needed

If you've verified the project exists but Terraform still sees it as missing, refresh your Terraform state:

bash
terraform refresh

This reads the current state of all resources in AWS and updates your local state file. After refreshing, check terraform plan again to see if the conflict is resolved.

Advanced notes

Remote state management: Using remote state (S3, Terraform Cloud) with state locking prevents state file corruption and concurrent modifications that can cause this error.

Multiple environments: If managing multiple AWS accounts or regions, use separate Terraform workspaces or state files per environment to avoid naming collisions.

AWS resource cleanup: CloudFormation stacks or other IaC tools may also create CodeBuild projects. Ensure only one management tool (Terraform) is responsible for a given resource.

Project naming patterns: Consider using a naming convention like {app}-{environment}-{component} (e.g., myapp-prod-cicd) to minimize conflicts in shared AWS accounts.

Frequently asked questions

What does "Error: Error creating CodeBuild Project: ResourceAlreadyExistsExceptio" mean?
The ResourceAlreadyExistsException error indicates that AWS CodeBuild is rejecting the creation of a project because a project with that name already exists in the region. This commonly happens in three scenarios: 1. State mismatch: The CodeBuild project was created outside of Terraform (manually via AWS Console, AWS CLI, or another tool), but Terraform's state file doesn't know about it.
How do I fix "Error: Error creating CodeBuild Project: ResourceAlreadyExistsExceptio"?
This error occurs when Terraform attempts to create a CodeBuild project that already exists in your AWS account. It typically happens when there's a mismatch between Terraform state and actual AWS resources.
What causes "Error: Error creating CodeBuild Project: ResourceAlreadyExistsExceptio"?
The CodeBuild project was created manually or by another process outside of this Terraform configuration Terraform state file was lost, deleted, or reset, causing Terraform to lose track of previously managed resources Multiple Terraform configurations or workspaces are attempting to manage the same CodeBuild project name
Is the "Error: Error creating CodeBuild Project: ResourceAlreadyExistsExceptio" 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